小编use*_*671的帖子

获取选项值始终返回"null"

我坚持以下情况.每当我尝试获取选项值时,它返回null.

这是代码片段:

    public static Options configureOptions() {      
    Option groupOption = Option.builder("g")
            .longOpt("group")
            .required(false)
            .desc("The group of the user.")
            .build();
    Options allOptions = new Options();
    allOptions.addOption(taskOption);

    return allOptions;
}

public static void main(String[] args) throws ParseException {

    Options options = configureOptions();
    CommandLineParser parser = new DefaultParser();
    CommandLine commands = parser.parse(options, args);
    if (commands.hasOption("group")) {
        System.out.println("group: " + commands.getOptionValue("group"));
    }
}
Run Code Online (Sandbox Code Playgroud)

并使用选项-g staff运行,然后输出始终null.

java -classpath rsa-1.0.0-SNAPSHOT.jar;c:\Users\user.m2\repository\commons-cli\commons-cli\1.3.1\commons-cli-1.3.1.jar Main -g staff

java command-line-interface apache-commons

5
推荐指数
1
解决办法
2101
查看次数