30

I am writing command line application in Java and I've chosen Apache Commons CLI to parse input arguments.

Let's say I have two required options (ie. -input and -output). I create new Option object and set required flag. For now it's all good. But I have third, not required option, ie. -help. With settings that I've mentioned, when user wants to show help (use -help option) it says "-input and -output" are required. Is there any way to implement this (via Commons CLI API, not simple if (!hasOption) throw new XXXException()).

Emmanuel Bourg
  • 9,601
  • 3
  • 48
  • 76
pavel
  • 525
  • 2
  • 7
  • 14

2 Answers2

36

In this situation you have to define two sets of options and parse the command line twice. The first set of options contains the options that precede the required group (typically --help and --version), and the second set contains all the options.

You start by parsing the first set of options, and if no match is found, you go on with the second set.

Here is an example:

Options options1 = new Options();
options1.add(OptionsBuilder.withLongOpt("help").create("h"));
options1.add(OptionsBuilder.withLongOpt("version").create());

// this parses the command line but doesn't throw an exception on unknown options
CommandLine cl = new DefaultParser().parse(options1, args, true);

if (!cl.getOptions().isEmpty()) {

    // print the help or the version there.

} else {
    OptionGroup group = new OptionGroup();
    group.add(OptionsBuilder.withLongOpt("input").hasArg().create("i"));
    group.add(OptionsBuilder.withLongOpt("output").hasArg().create("o"));
    group.setRequired(true);

    Options options2 = new Options();
    options2.addOptionGroup(group);

    // add more options there.

    try {
        cl = new DefaultParser().parse(options2, args);

        // do something useful here.

    } catch (ParseException e) {
        // print a meaningful error message here.
    }
}
Forage
  • 2,726
  • 2
  • 18
  • 20
Emmanuel Bourg
  • 9,601
  • 3
  • 48
  • 76
  • Ok. But how can I tell that I haven't found the match? ("and if no match is found")? If I set required flag I get ParseExeception, but when I pass option that is not in available I get ParseException too :( How can I differentiate this cases? – pavel May 29 '12 at 12:36
  • You know if you got a match when `commandline.getOptions()` isn't empty. – Emmanuel Bourg May 29 '12 at 12:51
  • Ok. It is possible solution. But what about handling unsupported options? Available options are -help, -input, -output. User puts -test. Then I have to check first set (with HELP) to determine whether help was found or not. If not or UnrecognizedOptionException was thrown then I need to parse second set of options? So simply try { parsefirstSet() if (not_found) parseSecondSet() catch(ParserException) { parseSecondSet() }? Am I missing something or it is what you suggested. Thanks for help btw :) – pavel May 29 '12 at 19:52
  • I added an example, let me know if this needs more clarifications. – Emmanuel Bourg May 29 '12 at 20:41
  • 1
    It is more or less what I was thinking about. I was trying to avoid double catch (for first and second parsing) - thought it'll look bad. But I think there's no way to do it. In my opinion Commons Cli implementation should allow to create groups (mutually exclusive) and then add group to another group. So simply group(help, group(in, out)) would do the trick. Thanks for example. Take care! – pavel May 29 '12 at 21:13
  • 1
    Its important to note that by now OptionsBuilder is deprecated and options.builder should be used. – Beginner May 03 '17 at 14:24
  • 1
    In this case, how do you use the HelpFormatter? – Snackoverflow Jul 27 '17 at 12:49
  • 1
    @Karl-AnderoMere for the formatter you use an OptionGroup with all the options – Emmanuel Bourg Jul 28 '17 at 11:25
0

There is a fluent wrapper for the commons-cli library: https://github.com/bogdanovmn/java-cmdline-app

The help option is built-in. Also there are a few convenient features. For example, if you must to specify one of two options:

new CmdLineAppBuilder(args)
// Optional argument
.withArg("input", "input description")
.withArg("output", "output description")

// "input" or "output" must be specified
.withAtLeastOneRequiredOption("input", "output")

.withEntryPoint(
    cmdLine -> {
        ...
    }
).build().run();
Elegant.Obj
  • 131
  • 1
  • 12