如何在CLI commons库中使用property = value [英] how to use property=value in CLI commons Library
问题描述
我想使用 OptionBuilder.withArgName(property = value)
被称为状态,我的命令行是:
- status p = 11 s = 22
它只能识别第一个参数是11,并且无法识别第二个参数...
选项status = OptionBuilder.withLongOpt(status)
.withArgName(property = value)
.hasArgs
.withValueSeparator()
.withDescription(Get the status)
.create('s');
options.addOption(status);感谢您提前寻求帮助 解决方案您可以通过简单修改传递的命令行选项访问传递的属性
- status p = 11 --status s = 22
或使用您的简短语法
-sp = 11 -ss = 22
$ b b
在这种情况下,您只需使用代码
即可访问您的属性。
if(cmd.hasOption ){
属性props = cmd.getOptionProperties(status);
System.out.println(props.getProperty(p));
System.out.println(props.getProperty(t));
}
如果您需要严格使用语法,可以手动解析您的属性=值对。
在这种情况下,你应该删除.withValueSeparator()调用,然后使用
String [] propvalues = cmd。 getOptionValues(status);
for(String propvalue:propvalues){
String [] values = propvalue.split(=);
System.out.println(values [0] +:+ values [1]);
}
I am trying to use the OptionBuilder.withArgName( "property=value" )
If my Option is called status and my command line was:
--status p=11 s=22
It only succeeds to identify the first argument which is 11 and it fails to identify the second argument...
Option status = OptionBuilder.withLongOpt("status")
.withArgName( "property=value" )
.hasArgs(2)
.withValueSeparator()
.withDescription("Get the status")
.create('s');
options.addOption(status);
Thanks for help in advance
解决方案 You can access to passed properties using simple modification of passed command line options
--status p=11 --status s=22
or with your short syntax
-s p=11 -s s=22
In this case you can access to your properties simply with code
if (cmd.hasOption("status")) {
Properties props = cmd.getOptionProperties("status");
System.out.println(props.getProperty("p"));
System.out.println(props.getProperty("t"));
}
If you need to use your syntax strictly, you can manually parse your property=value pairs.
In this case you should remove .withValueSeparator() call, and then use
String [] propvalues = cmd.getOptionValues("status");
for (String propvalue : propvalues) {
String [] values = propvalue.split("=");
System.out.println(values[0] + " : " + values[1]);
}
这篇关于如何在CLI commons库中使用property = value的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!