JAVA基础 - 使用CommandLine解析命令行参数

超、凢脫俗 2023-10-12 11:22 111阅读 0赞

它是什么

commons-cli 是一个强大而的开源的命令行参数传递与解析的解决方案,用于接收MAIN方法的args参数。可以通过设置短选项(即选项名简写)、长选项(即全写)、设置是否需要携带选项参数(指定为 false 时,表示此选项不带参数,即为布尔选项)和描述信息来定义参数选项。
官网:https://commons.apache.org/proper/commons-cli/index.html

请先阅读该博文:
CENTOS下的命令行参数:https://blog.csdn.net/goodjava2007/article/details/131083116

编码步骤

(1)定义参数

  1. Options options = new Options();

(2)解析参数

  1. CommandLine cmd = new BasicParser().parse(options, args);
  2. // 或者
  3. CommandLine cmd = new DefaultParser().parse(options, args);
  4. // 或者
  5. CommandLine cmd = new PosixParser().parse(options, args);
  6. // 或者
  7. CommandLine cmd = new GnuParser().parse(options, args);

(3)获取参数

  1. String db = cmd.getOptionValue("d")

参数风格










































序号 风格类型 参数描述 解析器
1 POSIX风格参数 以“-”开头的单个字符的POSIX风格的参数,如:tar -zxvf foo.tar.gz PosixParser
2 GNU风格参数 以“- -”后接选项关键字的GNU风格的参数,GNU风格兼容POSIX风格,如:du - -human-readable - -max-depth=1 GnuParser
3 JAVA风格参数 以“-D”开头的参数,如:java -Datlas.log.file=import-hive.log -Dlog4j.configuration=atlas-hive-import-log4j.xml DefaultParser
4 短选项参数 以“-”开头的单个字符参数,即:横杠+参数名+空格+参数值(空格也可以不带),如:import-hive.sh -dmallx -tmallx_order 或者 import-hive.sh -d mallx -t mallx_order 都可以 DefaultParser / BasicParser
5 长选项参数 以“-”开头的多个字符参数,如:ant -projecthelp DefaultParser / BasicParser

注:以上表格说明的是参数风格,至于某个参数后面是否带参数对应的值,需要在选项的代码中进行设置。

如何使用

① 依赖引入
  1. <dependency>
  2. <groupId>commons-cli</groupId>
  3. <artifactId>commons-cli</artifactId>
  4. <version>1.4</version>
  5. </dependency>
② 示例代码
  1. public class MyCommandLine {
  2. public static void main(String[] args) {
  3. MyCommandLine mcl = new MyCommandLine();
  4. mcl.defaultParser(args);
  5. }
  6. private void defaultParser(String[] args) {
  7. Options options = new Options();
  8. try {
  9. // 1.1 构造参数
  10. options.addOption("help", "如何使用mcl指令");
  11. options.addOption("d", "database", true, "指定数据库名");
  12. options.addOption("t", "table", true, "指定表名");
  13. options.addOption("s", "size", true, "指定文件的大小");
  14. options.addOption("f", "filename", true, "指定文件的全路径名");
  15. options.addOption("failOnError", false, "指定出现错误是是否停止");
  16. Option property = Option.builder("D")
  17. .argName("property=value")
  18. .hasArgs()
  19. .valueSeparator('=')
  20. .desc("指定KEY=VALUE形式的参数")
  21. .build();
  22. options.addOption(property);
  23. // 1.2 解析
  24. CommandLine cmd = new DefaultParser().parse(options, args);
  25. // 1.3 取值
  26. boolean failOnError = cmd.hasOption("failOnError");
  27. String db = cmd.getOptionValue("d"); // 数据库
  28. String table = cmd.getOptionValue("t"); // 表
  29. String size = cmd.getOptionValue("s"); // 表大小
  30. String file = cmd.getOptionValue("f"); // 文件
  31. String log = cmd.getOptionProperties("D").getProperty("atlas.log");
  32. // 以下仅仅用于测试
  33. // 输出USAGE
  34. System.out.println(getHelp(options));
  35. // 输出参数
  36. System.out.println(String.format("The db is: %s, table is: %s, size is : %s, file is: %s, log is %s", db, table, size, file, log));
  37. } catch (Exception e) {
  38. } finally {
  39. }
  40. }
  41. private String getHelp(Options options) {
  42. HelpFormatter helper = new HelpFormatter();
  43. ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
  44. PrintWriter printWriter = new PrintWriter(byteArrayOutputStream);
  45. helper.printHelp(printWriter, HelpFormatter.DEFAULT_WIDTH, "mcl -help", null,
  46. options, HelpFormatter.DEFAULT_LEFT_PAD, HelpFormatter.DEFAULT_DESC_PAD, null);
  47. printWriter.flush();
  48. String help = new String(byteArrayOutputStream.toByteArray());
  49. printWriter.close();
  50. return help;
  51. }
  52. }
③ 示例测试

在IDEA中进行参数设置,如下:

  1. "Program arguments" 输入框中如下设置:
  2. -d mallx -tmallx_order -s1000 -Datlas.log=import-hive.log -f D:\02-工作空间\06-代码空间\04-gitee\rills-atlas-2.1.0-rc3\README.txt

在这里插入图片描述

④ 执行结果
  1. usage: mcl -help
  2. -d,--database <arg> 指定数据库名
  3. -D <property=value> 指定KEY=VALUE形式的参数
  4. -f,--filename <arg> 指定文件的全路径名
  5. -failOnError 指定出现错误是是否停止
  6. -help 如何使用mcl指令
  7. -s,--size <arg> 指定文件的大小
  8. -t,--table <arg> 指定表名
  9. The db is: mallx, table is: mallx_order, size is : 1000, file is: D:\02-工作空间\06-代码空间\04-gitee\rills-atlas-2.1.0-rc3\README.txt, log is import-hive.log
⑤ 官方示例

COMMONS CLI 官方示例:https://commons.apache.org/proper/commons-cli/usage.html

发表评论

表情:
评论列表 (有 0 条评论,111人围观)

还没有评论,来说两句吧...

相关阅读