Commons CLI required groups

烂漫一生 提交于 2019-11-27 12:48:35

问题


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()).


回答1:


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.
    }
}


来源:https://stackoverflow.com/questions/10798208/commons-cli-required-groups

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!