How to use property = value in the CLI library

I am trying to use OptionBuilder.withArgName( "property=value" )

If my parameter is called status and my command line is:

 --status p=11 s=22 

He will be able to identify the first argument, which is 11, and he cannot 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 the help in advance.

+4
source share
1 answer

You can access the passed properties by simply modifying the passed command-line options

 --status p=11 --status s=22 

or with your short syntax

 -sp=11 -ss=22 

In this case, you can access your properties simply using 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 strictly use syntax, you can manually parse property = value pairs. In this case, you must remove the .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]); } 
+8
source

Source: https://habr.com/ru/post/1500498/


All Articles