免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 51093 | 回复: 1
打印 上一主题 下一主题

[其他] 初学 go 也写一个 getopt.go 实现 [复制链接]

论坛徽章:
84
每日论坛发贴之星
日期:2015-12-29 06:20:00每日论坛发贴之星
日期:2016-01-16 06:20:00每周论坛发贴之星
日期:2016-01-17 22:22:00程序设计版块每日发帖之星
日期:2016-01-20 06:20:00每日论坛发贴之星
日期:2016-01-20 06:20:00程序设计版块每日发帖之星
日期:2016-01-21 06:20:00每日论坛发贴之星
日期:2016-01-21 06:20:00程序设计版块每日发帖之星
日期:2016-01-23 06:20:00程序设计版块每日发帖之星
日期:2016-01-31 06:20:00数据库技术版块每日发帖之星
日期:2016-01-16 06:20:00程序设计版块每日发帖之星
日期:2016-01-16 06:20:00程序设计版块每日发帖之星
日期:2016-01-14 06:20:00
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2019-04-02 14:32 |只看该作者 |倒序浏览
本帖最后由 yjh777 于 2019-04-04 22:14 编辑

see: https://github.com/tcler/cmdline-go

用法:
  1. git clone https://github.com/tcler/cmdline-go
  2. cd cmdline-go

  3. go get -u github.com/tcler/cmdline-go/cmdline
  4. go run main_cmdline.go -h -H -f file --file file2 -e 's/abc/xyz/'  -r -n  -s=A -oa=b -S ''  -i -x xfile --wenj=file3 --www -aa -vvv -S DD -- -0 -y
复制代码

论坛徽章:
84
每日论坛发贴之星
日期:2015-12-29 06:20:00每日论坛发贴之星
日期:2016-01-16 06:20:00每周论坛发贴之星
日期:2016-01-17 22:22:00程序设计版块每日发帖之星
日期:2016-01-20 06:20:00每日论坛发贴之星
日期:2016-01-20 06:20:00程序设计版块每日发帖之星
日期:2016-01-21 06:20:00每日论坛发贴之星
日期:2016-01-21 06:20:00程序设计版块每日发帖之星
日期:2016-01-23 06:20:00程序设计版块每日发帖之星
日期:2016-01-31 06:20:00数据库技术版块每日发帖之星
日期:2016-01-16 06:20:00程序设计版块每日发帖之星
日期:2016-01-16 06:20:00程序设计版块每日发帖之星
日期:2016-01-14 06:20:00
2 [报告]
发表于 2019-04-02 14:35 |只看该作者
本帖最后由 yjh777 于 2019-04-04 22:12 编辑
  1. $ go run main_cmdline.go -h -H -f file --file file2 -e 's/abc/xyz/'  -r -n  -s=A -oa=b -S ''  -i -x xfile --wenj=file3 --www -aa -vvv -S DD -- -0 -y
  2. Debug info:
  3. --------------------------------------------------------------------------------
  4. [-h -H -f file --file file2 -e s/abc/xyz/ -r -n -s=A -oa=b -S  -i -x xfile --wenj=file3 --www -aa -vvv -S DD -- -0 -y]
  5. Options group1:
  6.      -h -H --help              out put the usage info
  7.      -f -F --file {arg}        file to be parse
  8.      -o [arg]                  mount option
  9.      -v                        verbose output, -vvv means verbose level 3
  10.      -x <arg>                  dump binary file to text
  11.      -s <arg>                  enable smart mode

  12. Options group2:
  13.      -e {arg}                  sed -e option, will forward to child sed process
  14.      -r                        sed -r option, will forward to child sed process
  15.      -n                        sed -n option, will forward to child sed process

  16. Comments:
  17.     *  [arg] means arg is optional, need use --opt=arg to specify an argument
  18.        <arg> means arg is required, and -f a -f b will get the latest 'b'
  19.        {arg} means arg is required, and -f a -f b will get a list 'a b'

  20.     *  if arg is required, '--opt arg' is same as '--opt=arg'

  21.     *  '-opt' will be treated as:
  22.            '--opt'    if 'opt' is defined;
  23.            '-o -p -t' if 'opt' is undefined;
  24.            '-o -p=t'  if 'opt' is undefined and '-p' need an argument;

  25. opt(help) = []string{"set", "set"}
  26. opt(f) = []string{"file", "file2", "file3"}
  27. opt(s) = []string{"DD", "", "A"}
  28. opt(x) = []string{"xfile"}
  29. opt(v) = []string{"set", "set", "set"}
  30. opt(h) = []string{"set", "set"}
  31. opt(H) = []string{"set", "set"}
  32. opt(o) = []string{"a=b"}
  33. opt(F) = []string{"file", "file2", "file3"}
  34. opt(file) = []string{"file", "file2", "file3"}

  35. option: 'i' undefined
  36. option: 'www' undefined
  37. option: 'a' undefined
  38. option: 'a' undefined

  39. params: []string{"-0", "-y"}
  40. forward: []string{"-e s/abc/xyz/", "-r", "-n"}
  41. --------------------------------------------------------------------------------
  42. Usage: ...
  43. file list: []string{"file", "file2", "file3"}
  44. verbose level: 3
  45. smart mode: "DD"
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP