java -help 选项中的 Apache Commons CLI 多个参数值名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11795015/
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 06:24:04  来源:igfitidea点击:

Apache Commons CLI muliple argument value names in the -help option

javacommand-lineargumentscommand-line-interfaceapache-commons-cli

提问by Metalhead89

I use Apache Commons CLI for parsing command line arguments.

我使用 Apache Commons CLI 来解析命令行参数。

I am looking for a way to display multiple argument value names in the help. Here is an example for one argument of the option "startimport":

我正在寻找一种在帮助中显示多个参数值名称的方法。这是选项“startimport”的一个参数的示例:

Option startimport = OptionBuilder
                .withArgName("environment")
                .hasArg()
                .withDescription(
                        "Description")
                .create("startimport");

When I use -help it prints out:

当我使用 -help 时,它会打印出:

-startimport <environment>                    Description

Thatfs fine. But what if I want to use two arguments?

那很好。但是如果我想使用两个参数怎么办?

Option startimport = OptionBuilder
                .withArgName("firstArg secondArg")
                .hasArgs(2)
                .withDescription("Description")
                .create("startimport ");

Parsing the two arguments is not the problem but I want the following output in the "-help":

解析两个参数不是问题,但我希望在“-help”中有以下输出:

startimport <firstArg> <secondArg>                    Description

But currently I would just get:

但目前我只会得到:

startimport <firstArg secondArg>                    Description

Is there a proper solution for that problem?

是否有针对该问题的适当解决方案?

回答by macacollins

I found a way to solve this problem in a way that behaves correctly, and thought I'd share because this is one of the pages Google led me to while researching. This code was written using Commons CLI 1.2.

我找到了一种以正确行为的方式解决这个问题的方法,并认为我会分享,因为这是谷歌在研究时引导我访问的页面之一。此代码是使用 Commons CLI 1.2 编写的。

Option searchApp = OptionBuilder.withArgName("property> <value")
            .withValueSeparator(' ')
            .hasArgs(2)
            .withLongOpt("test")
            .withDescription("This is a test description.")
            .create("t");

The help message looks like:

帮助消息如下所示:

-t,--test <property> <value>    This is a test description.

It can be used from the command line like this:

它可以从命令行使用,如下所示:

java -jar program.jar -t id 5

and a String[] of the arguments can be retrieved in code like this:

并且可以在代码中检索参数的 String[] ,如下所示:

Options options = new Options();
options.addOption(searchApp);
PosixParser parser = new PosixParser();
CommandLine cmd = parser.parse( options, args);
String[] searchArgs = cmd.getOptionValues("t");

Then you can retrieve the values with searchArgs[0]and searchArgs[1].

然后您可以使用searchArgs[0]和检索值searchArgs[1]

回答by Bertrand Brompton

I used a naughty way to solve this problem.

我用了一个顽皮的方法来解决这个问题。

    OptionBuilder.hasArgs(3);
    OptionBuilder.withArgName("hostname> <community> <oid");
    OptionBuilder.withDescription("spans switch topology. Mutually exclusive with -s");
    Option my_a = OptionBuilder.create("a");

It appears correctly in the help now. Though I am not sure if this has consequences though.

它现在在帮助中正确显示。虽然我不确定这是否会产生后果。