duandai3964 2017-10-10 18:37
浏览 121
已采纳

在GoLang CLI程序中传递长参数的最佳方法及其局限性

I am writing a CLI program in GO and need to be able to pass a rather long list of arguments to the main function. At the moment I am using the standard way of passing arguments (space separated values) and then retrieve 'em using os.Args[index].

So my questions are:

  1. Is there a limit to the number of arguments one can pass?
  2. Is there a limit to the length of a string argument?
  3. Is there any other way of achieving this and if so how?
  • 写回答

1条回答 默认 最新

  • dongxingqiu7943 2017-10-10 18:47
    关注
    1. No, as far as go is concerned. Command line args are parsed into a []string, so at this level it's just a matter of how much memory is available. But, the comments below are correct. Your system will have its own lower limits that you'll hit first.
    2. Same as above.
    3. If you find yourself passing tons for args for every run:
      • if they don't change much, do they make sense as sane defaults for your flags?
      • if they don't make sense as defaults, consider moving to reading them from a config file. CSV, YAML, whatever.

    Without knowing more, I'd recommend a the config file. They make it easy to track different configs/runs, easier to edit than a long list of cmd line args, and you can always write it so that args on the cmd line overwrite values in the config.

    Check out awesome-go for libraries that already do this, specifically under the command-line and configuration sections.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图