how to use property=value in CLI commons Library

怎甘沉沦 提交于 2019-12-13 13:21:23

问题


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


回答1:


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]);
}


来源:https://stackoverflow.com/questions/18611791/how-to-use-property-value-in-cli-commons-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!