Apache Commons CLI - 选项类型和默认值

时间:2022-05-09 17:07:35

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?)

如何为CLI Option提供类型 - 例如int或Integer? (稍后,如何通过单个函数调用获取解析后的值?)

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?

如何为CLI Option提供默认值?这样,除非在命令行中指定了一个,否则上面提到的CommandLine.getOptionValue()或函数调用会返回该值?

4 个解决方案

#1


45  

EDIT: Default values are now supported. See answer https://*.com/a/14309108/1082541 below.

编辑:现在支持默认值。请参阅下面的答案https://*.com/a/14309108/1082541。

As Brent Worden already mentioned, default values are not supported.

正如Brent Worden已经提到的,不支持默认值。

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.

我也遇到了使用Option.setType的问题。在类型为Integer.class的选项上调用getParsedOptionValue时,我总是得到一个空指针异常。因为文档不是真的有用,所以我查看了源代码。

Looking at the TypeHandler class and the PatternOptionBuilder class you can see that Number.class must be used for int or Integer.

查看TypeHandler类和PatternOptionBuilder类,您可以看到Number.class必须用于int或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.

请记住,如果提供的数字不适合int,则值可能会溢出。

#2


24  

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

我不知道如果不工作或最近添加,但getOptionValue()有一个重载版本,接受默认(字符串)值

#3


1  

CLI does not support default values. Any unset option results in getOptionValue returning null.

CLI不支持默认值。任何未设置的选项都会导致getOptionValue返回null。

You can specify option types using the Option.setType method and extract the parsed option value as that type using CommandLine.getParsedOptionValue

您可以使用Option.setType方法指定选项类型,并使用CommandLine.getParsedOptionValue将解析的选项值提取为该类型

#4


1  

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:

OptionBuilder在版本1.3和1.4中已弃用,而Option.Builder似乎没有直接函数来设置类型。 Option类有一个名为setType的函数。您可以使用CommandLine.getParsedOptionValue函数检索转换后的值。不知道为什么它不再是建筑师的一部分了。它现在需要一些像这样的代码:

    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

这将给出Long类型的变量

#1


45  

EDIT: Default values are now supported. See answer https://*.com/a/14309108/1082541 below.

编辑:现在支持默认值。请参阅下面的答案https://*.com/a/14309108/1082541。

As Brent Worden already mentioned, default values are not supported.

正如Brent Worden已经提到的,不支持默认值。

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.

我也遇到了使用Option.setType的问题。在类型为Integer.class的选项上调用getParsedOptionValue时,我总是得到一个空指针异常。因为文档不是真的有用,所以我查看了源代码。

Looking at the TypeHandler class and the PatternOptionBuilder class you can see that Number.class must be used for int or Integer.

查看TypeHandler类和PatternOptionBuilder类,您可以看到Number.class必须用于int或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.

请记住,如果提供的数字不适合int,则值可能会溢出。

#2


24  

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

我不知道如果不工作或最近添加,但getOptionValue()有一个重载版本,接受默认(字符串)值

#3


1  

CLI does not support default values. Any unset option results in getOptionValue returning null.

CLI不支持默认值。任何未设置的选项都会导致getOptionValue返回null。

You can specify option types using the Option.setType method and extract the parsed option value as that type using CommandLine.getParsedOptionValue

您可以使用Option.setType方法指定选项类型,并使用CommandLine.getParsedOptionValue将解析的选项值提取为该类型

#4


1  

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:

OptionBuilder在版本1.3和1.4中已弃用,而Option.Builder似乎没有直接函数来设置类型。 Option类有一个名为setType的函数。您可以使用CommandLine.getParsedOptionValue函数检索转换后的值。不知道为什么它不再是建筑师的一部分了。它现在需要一些像这样的代码:

    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

这将给出Long类型的变量