Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache Commons CLI - option type and default value

How can I give a CLI Option a type - such as int or Integer? (Later, how can I get the parsed value with a single function call?)

How can I give a CLI Option a default value? Such that CommandLine.getOptionValue() or the function call mentioned above return that value unless one is specified on the command line?

like image 793
aib Avatar asked Apr 07 '11 18:04

aib


4 Answers

EDIT: Default values are now supported. See answer https://stackoverflow.com/a/14309108/1082541 below.

As Brent Worden already mentioned, default values are not supported.

I had issues with using Option.setType too. I always got a null pointer exception when calling getParsedOptionValue on an option with type Integer.class. Because the documentation was not really helpful I looked into the source code.

Looking at the TypeHandler class and the PatternOptionBuilder class you can see that Number.class must be used for int or Integer.

And here is a simple example:

CommandLineParser cmdLineParser = new PosixParser();

Options options = new Options();
options.addOption(OptionBuilder.withLongOpt("integer-option")
                      .withDescription("description")
                      .withType(Number.class)
                      .hasArg()
                      .withArgName("argname")
                      .create());

try {
    CommandLine cmdLine = cmdLineParser.parse(options, args);

    int value = 0; // initialize to some meaningful default value
    if (cmdLine.hasOption("integer-option")) {
        value = ((Number)cmdLine.getParsedOptionValue("integer-option")).intValue();
    }

    System.out.println(value);
} catch (ParseException e) {
    e.printStackTrace();
}

Keep in mind that value can overflow if a number is provided which does not fit into an int.

like image 199
Patrick Spettel Avatar answered Nov 16 '22 02:11

Patrick Spettel


I do not know if not working or if added recently but getOptionValue() has an overloaded version that accepts a default (String) value

like image 23
Mr_and_Mrs_D Avatar answered Nov 16 '22 04:11

Mr_and_Mrs_D


The OptionBuilder is deprecated in version 1.3 & 1.4 and Option.Builder doesn't seem to have a direct function to set the type. There is a function for the Option class called setType. You can a retrieve a converted value with the function CommandLine.getParsedOptionValue. Not sure why it's not part of the builder anymore. It requires some code like this now:

    options = new Options();

    Option minOpt = Option.builder("min").hasArg().build();
    minOpt.setType(Number.class);
    options.addOption(minOpt);

and reading it:

    String testInput = "-min 14";
    String[] splitInput = testInput.split("\\s+");

    CommandLine cmd =  CLparser.parse(options, splitInput);
    System.out.println(cmd.getParsedOptionValue("min")); 

which would give a variable of type Long

like image 2
ramin Avatar answered Nov 16 '22 03:11

ramin


CLI does not support default values. Any unset option results in getOptionValue returning null.

You can specify option types using the Option.setType method and extract the parsed option value as that type using CommandLine.getParsedOptionValue

like image 1
Brent Worden Avatar answered Nov 16 '22 04:11

Brent Worden