免费注册 查看新帖 |

Chinaunix

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

cmdline 一个命令行解析库 java 实现 [复制链接]

论坛徽章:
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-23 10:20 |只看该作者 |倒序浏览
本帖最后由 yjh777 于 2019-04-23 10:21 编辑

初学 java 写个命令行解析库练练手,

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

特色: inspired by https://github.com/tcler/getopt.tcl
  - 支持 --long 写成 -long
  - 支持选项参数列表类型
  - 自动生成 Usage 信息,并保证输出顺序跟选项生命列表一致
  - 支持多个选项名(不只是一个 short 一个 long)  # 这个需求很小众,用来兼容相同功能但使用不同option name的程序
  - 提供 forward 类型的选项 # 用来传递选项给 sub command
  - 提供 hide 属性,不在 Usage 中出现 # 用来向用户隐藏某些选项


demo:
  1. import java.util.Arrays;
  2. import java.util.ArrayList;
  3. import com.github.tcler.cmdline.*;

  4. class CmdlineTest {
  5.         public static void main(String[] argv) {
  6.                 if (argv.length < 1) {
  7.                         System.out.println("argv is emplty, use test argv instead:");
  8.                         argv = new String[]{ "-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" };
  9.                 }
  10.                 System.out.printf("argv: %s\n", Arrays.toString(argv));
  11.                 System.out.printf("----------------------------------------------------------------\n\n");
  12.                 ArrayList<Option> optionList = new ArrayList<Option>();
  13.                 optionList.add(new Option("Options group1:"));
  14.                 optionList.add(new Option("help h H 帮助", Option.ArgType.N, "print this help"));
  15.                 optionList.add(new Option("file f", Option.ArgType.M, "specify file"));
  16.                 optionList.add(new Option("conf c", Option.ArgType.Y, "config file"));
  17.                 optionList.add(new Option("o", Option.ArgType.O, "mount option", ""));
  18.                 optionList.add(new Option("v", Option.ArgType.N, "verbose output, -vvv means verbose level 3"));
  19.                 optionList.add(new Option("x", Option.ArgType.Y, "dump binary file to text"));
  20.                 optionList.add(new Option("s", Option.ArgType.Y, "enable smart mode"));
  21.                 optionList.add(new Option("S", Option.ArgType.Y, "", "s", true));

  22.                 optionList.add(new Option("\nOptions group2:"));
  23.                 optionList.add(new Option("e", Option.ArgType.M, "sed -e option, will forward to child sed process", true));
  24.                 optionList.add(new Option("r", Option.ArgType.N, "sed -r option, will forward to child sed process", true));
  25.                 optionList.add(new Option("n", Option.ArgType.N, "sed -n option, will forward to child sed process", true));

  26.                 Cmdline cl = new Cmdline(optionList, argv);

  27.                 if (cl.hasOption("help")) {
  28.                         cl.getUsage(optionList);
  29.                 }
  30.                 if (cl.hasOption("file")) {
  31.                         System.out.println("opt(file): " + cl.getOptionArgList("file").toString());
  32.                 }
  33.                 if (cl.hasOption("conf")) {
  34.                         System.out.println("opt(conf): " + cl.getOptionArgString("conf"));
  35.                 }
  36.                 if (cl.hasOption("o")) {
  37.                         System.out.println("opt(o): " + cl.getOptionArgString("o"));
  38.                 }

  39.                 if (cl.hasOption("v")) {
  40.                         int verboselevel = cl.getOptionNumber("v");
  41.                         System.out.println("opt(v): " + verboselevel);
  42.                 }
  43.                 if (cl.hasOption("s")) {
  44.                         System.out.println("opt(s): " + cl.getOptionArgString("s"));
  45.                 }

  46.                 System.out.println("invalidOptions: " + cl.getInvalidOptions().toString());
  47.                 System.out.println("forwardOptions: " + cl.getForwardOptions().toString());
  48.                 System.out.println("args: " + cl.getArgs().toString());
  49.         }
  50. }
复制代码



论坛徽章:
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-23 10:26 |只看该作者
本帖最后由 yjh777 于 2019-04-23 11:14 编辑
  1. $ java -jar cmdline.jar
  2. argv is emplty, use test argv instead:
  3. argv: [-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]
  4. ----------------------------------------------------------------

  5. Options group1:
  6.      -h -H --help --帮助         print this help
  7.      -f --file {arg}           specify file
  8.      -c --conf <arg>           config file
  9.      -o [arg]                  mount option
  10.      -v                        verbose output, -vvv means verbose level 3
  11.      -x <arg>                  dump binary file to text
  12.      -s <arg>                  enable smart mode

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

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

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

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

  26. opt(file): [file, file2]
  27. opt(o): a=b
  28. opt(v): 3
  29. opt(s): DD
  30. invalidOptions: [option: 'i' undefined, option: 'wenj=file3' undefined, option: 'www' undefined, option: 'a' undefined, option: 'a' undefined]
  31. forwardOptions: [-e s/abc/xyz/, -r, -n]
  32. args: [-0, -y]

复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP