Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use property=value in CLI commons Library

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

like image 393
Michael Girgis Avatar asked Jan 13 '23 11:01

Michael Girgis


1 Answers

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]);
}
like image 153
Alexander Zaykov Avatar answered Jan 20 '23 23:01

Alexander Zaykov