Java -help选项中的Apache Commons CLI多参数值名称
我使用Apache Commons CLI解析命令行参数 我正在寻找一种在帮助中显示多个参数值名称的方法。 下面是选项“startimport”的一个参数的示例: 当我使用-help时,它会打印出:Java -help选项中的Apache Commons CLI多参数值名称,java,command-line,arguments,command-line-interface,apache-commons-cli,Java,Command Line,Arguments,Command Line Interface,Apache Commons Cli,我使用Apache Commons CLI解析命令行参数 我正在寻找一种在帮助中显示多个参数值名称的方法。 下面是选项“startimport”的一个参数的示例: 当我使用-help时,它会打印出: -startimport <environment> Description 解析这两个参数不是问题,但我希望“-help”中有以下输出: startimport说明 但目前我只会得到: startimport <firstArg seco
-startimport <environment> Description
解析这两个参数不是问题,但我希望“-help”中有以下输出:
startimport说明
但目前我只会得到:
startimport <firstArg secondArg> Description
startimport说明
那个问题有合适的解决办法吗?我用了一种顽皮的方法来解决这个问题
OptionBuilder.hasArgs(3);
OptionBuilder.withArgName("hostname> <community> <oid");
OptionBuilder.withDescription("spans switch topology. Mutually exclusive with -s");
Option my_a = OptionBuilder.create("a");
OptionBuilder.hasArgs(3);
OptionBuilder.withArgName(“主机名>)我找到了一种方法来解决这个问题,并且我认为我会分享,因为这是谷歌引导我研究的页面之一。这段代码是使用Commons CLI 1.2编写的
Option searchApp = OptionBuilder.withArgName("property> <value")
.withValueSeparator(' ')
.hasArgs(2)
.withLongOpt("test")
.withDescription("This is a test description.")
.create("t");
可以在如下代码中检索参数的字符串[]:
java -jar program.jar -t id 5
Options options = new Options();
options.addOption(searchApp);
PosixParser parser = new PosixParser();
CommandLine cmd = parser.parse( options, args);
String[] searchArgs = cmd.getOptionValues("t");
然后可以使用searchArgs[0]
和searchArgs[1]
检索这些值
Option searchApp = OptionBuilder.withArgName("property> <value")
.withValueSeparator(' ')
.hasArgs(2)
.withLongOpt("test")
.withDescription("This is a test description.")
.create("t");
-t,--test <property> <value> This is a test description.
java -jar program.jar -t id 5
Options options = new Options();
options.addOption(searchApp);
PosixParser parser = new PosixParser();
CommandLine cmd = parser.parse( options, args);
String[] searchArgs = cmd.getOptionValues("t");