Apache Commons CLI decrypts argument names with -help options

I use the Apache Commons CLI to parse command line arguments.

I am looking for a way to display multiple argument value names in the help. The following is an example of a single argument to the "startimport" option:

Option startimport = OptionBuilder .withArgName("environment") .hasArg() .withDescription( "Description") .create("startimport"); 

When I use -help, it prints:

 -startimport <environment> Description 

It's good. But what if I want to use two arguments?

 Option startimport = OptionBuilder .withArgName("firstArg secondArg") .hasArgs(2) .withDescription("Description") .create("startimport "); 

Parsing two arguments is not a problem, but I want to get the following result in "-help":

 startimport <firstArg> <secondArg> Description 

But for now, I would just get:

 startimport <firstArg secondArg> Description 

Is there a suitable solution for this problem?

+8
java command-line command-line-interface arguments apache-commons-cli
source share
2 answers

I used a naughty way to solve this problem.

  OptionBuilder.hasArgs(3); OptionBuilder.withArgName("hostname> <community> <oid"); OptionBuilder.withDescription("spans switch topology. Mutually exclusive with -s"); Option my_a = OptionBuilder.create("a"); 

The prompt will appear correctly. Although I'm not sure that this has consequences, though.

+9
source share

I found a way to solve this problem in a way that behaves correctly, and I thought that I would share it, because this is one of the pages Google brought me to during the research. This code was written using Commons CLI 1.2.

 Option searchApp = OptionBuilder.withArgName("property> <value") .withValueSeparator(' ') .hasArgs(2) .withLongOpt("test") .withDescription("This is a test description.") .create("t"); 

The help message looks like this:

 -t,--test <property> <value> This is a test description. 

It can be used from the command line as follows:

 java -jar program.jar -t id 5 

and the string [] of arguments can be restored in the code as follows:

 Options options = new Options(); options.addOption(searchApp); PosixParser parser = new PosixParser(); CommandLine cmd = parser.parse( options, args); String[] searchArgs = cmd.getOptionValues("t"); 

Then you can get the values ​​using searchArgs[0] and searchArgs[1] .

+24
source share

All Articles