42

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?

Emmanuel Bourg
  • 9,601
  • 3
  • 48
  • 76
aib
  • 45,516
  • 10
  • 73
  • 79

5 Answers5

51

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.

Community
  • 1
  • 1
Patrick Spettel
  • 710
  • 5
  • 8
  • 9
    Thanks for the example, this is what I needed. However, I have decided against CLI: it's too much work. Maybe it's just me, but I find it self-defeating when you have to handle common cases like that. With enough setup code, I should be able to just say `int foo = getOption("foo")` and have it default to 42 if anything goes wrong. – aib May 11 '11 at 03:03
  • 1
    Yes, you are right. I also think the library should handle this stuff. Which option parsing library can you recommend instead?? – Patrick Spettel May 11 '11 at 08:20
  • I'm new to the Java world. This was the first one I tried, and I know of no others. Perhaps you should post this as a question? – aib May 11 '11 at 12:59
  • 1
    Thx for pointing out the `Number.class` thing. I naively expected `Integer.class` to work...! – 0xbe5077ed Aug 07 '15 at 00:23
31

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

Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
2

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

ramin
  • 928
  • 8
  • 12
1

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

Brent Worden
  • 10,624
  • 7
  • 52
  • 57
  • I'm aware of the existence of setType() and getParsedOptionValue(), but have no idea how to use them. Could you give me a small example? – aib Apr 08 '11 at 04:35
0

One can use other definition of

getOptionValue:
public String getOptionValue(String opt, String defaultValue)

and wrap your default value to string.

Example:

String checkbox = line.getOptionValue("cb", String.valueOf(false));

output: false

it worked for me

Word Rearranger
  • 1,306
  • 1
  • 16
  • 25