Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache Commons CLI muliple argument value names in the -help option

I use Apache Commons CLI for parsing command line arguments.

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

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

When I use -help it prints out:

-startimport <environment>                    Description

Thatfs fine. But what if I want to use two arguments?

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

Parsing the two arguments is not the problem but I want the following output in the "-help":

startimport <firstArg> <secondArg>                    Description

But currently I would just get:

startimport <firstArg secondArg>                    Description

Is there a proper solution for that problem?

like image 292
Metalhead89 Avatar asked Aug 03 '12 11:08

Metalhead89


People also ask

What is argument option in Apache Commons CLI?

Apache Commons CLI - Argument Option. A Argument option is represented on a command line by its name and its corresponding value. For example, if option is present then user has to pass its value. Consider the following example, if we're printing logs to some file we want user to enter name of the log file with the argument option logFile.

How to print usage guide of command line arguments in Apache Commons CLI?

Apache Commons CLI provides HelpFormatter class to print the usage guide of command line arguments. See the example given below − Run the file and see the result. java CLITester usage: CLITester -g,--gui Show GUI Application -n <arg> No. of copies to print -p,--print Send print request to printer.

How to parse the command line arguments in commandlineparser?

The parse methods of CommandLineParser are used to parse the command line arguments. There may be several implementations of the CommandLineParser interface, the recommended one is the DefaultParser . CommandLineParser parser = new DefaultParser (); CommandLine cmd = parser.parse (options, args); Now we need to check if the t option is present.

How to get the argument value of a C option?

This specifies that the c option requires an argument value. If the required option argument value is specified on the command line it is returned, otherwise null is returned. The getOptionValue methods of CommandLine are used to retrieve the argument values of options.


2 Answers

I found a way to solve this problem in a way that behaves correctly, and thought I'd share because this is one of the pages Google led me to while researching. 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:

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

It can be used from the command line like this:

java -jar program.jar -t id 5

and a String[] of the arguments can be retrieved in code like this:

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 retrieve the values with searchArgs[0] and searchArgs[1].

like image 138
macacollins Avatar answered Oct 13 '22 11:10

macacollins


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");

It appears correctly in the help now. Though I am not sure if this has consequences though.

like image 26
Bertrand Brompton Avatar answered Oct 13 '22 12:10

Bertrand Brompton