how to use property=value in CLI commons Library

2019-06-24 12:25发布

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条回答
狗以群分
2楼-- · 2019-06-24 12:47

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]);
}
查看更多
登录 后发表回答