java Commons CLI 所需的组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10798208/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 02:37:00  来源:igfitidea点击:

Commons CLI required groups

javacommand-line-interfaceapache-commonsapache-commons-cli

提问by pavel

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

我正在用 Java 编写命令行应用程序,我选择了 Apache Commons CLI 来解析输入参数。

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

假设我有两个必需的选项(即 -input 和 -output)。我创建了新的 Option 对象并设置了 required 标志。现在一切都很好。但我有第三个,不是必需的选项,即。-帮助。使用我提到的设置,当用户想要显示帮助(使用 -help 选项)时,它会说“-input 和 -output”是必需的。有什么方法可以实现这个(通过Commons CLI API,不是简单的if(!hasOption)抛出新的XXXException())。

回答by Emmanuel Bourg

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 --helpand --version), and the second set contains all the options.

在这种情况下,您必须定义两组选项并解析命令行两次。第一组选项包含位于所需组之前的选项(通常为--help--version),第二组包含所有选项。

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

回答by Elegant.Obj

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

commons-cli 库有一个流畅的包装器: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();