how to use property=value in CLI commons Library

3.3k views Asked by At

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

There are 1 answers

2
Alexander Zaykov On BEST ANSWER

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