Java Apache CLI可选命令行参数不工作

Java Apache CLI可选命令行参数不工作,java,command-line-interface,apache-commons-cli,Java,Command Line Interface,Apache Commons Cli,尝试使用ApacheCommons命令行界面1.3.1对于必需的参数来说效果很好,但似乎删除了任何可选参数。有人能发现我下面的代码有问题吗 import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.CommandLineParser; import org.apache.commons.cli.DefaultParser; import org.apache.commons.cli.Option; impor

尝试使用ApacheCommons命令行界面1.3.1对于必需的参数来说效果很好,但似乎删除了任何可选参数。有人能发现我下面的代码有问题吗

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;

public class TestCommandLine {

    public static void main(String[] args) {

        // *****  test with command line arguments -R myfirstarg -O mysecondarg  *****
        // *****  the second arg is not being captured                          *****

        System.out.println("Number of Arguments : " + args.length);

        String commandline = "";
        for (String arg : args) {
            commandline = commandline + (arg + " ");
        }
        commandline.trim();
        System.out.println("Command-line arguments: " + commandline);

        // create Options object
        Options options = new Options();
        options.addOption("R", true, "Enter this required argument");
        Option optionalargument = Option.builder("O")
                .optionalArg(true)   // if I change this line to .hasArg(true) it works, but then is not optional
                .desc("Enter this argument if you want to")
                .build();
        options.addOption(optionalargument);

        // initialize variables used with command line arguments
        String firstargument = null;
        String secondargument = null;


        CommandLineParser parser = new DefaultParser();
        try {
            // parse the command line arguments
            CommandLine cmd = parser.parse( options, args );

            firstargument = cmd.getOptionValue("R");
            secondargument = cmd.getOptionValue("O");

            if(cmd.hasOption("R")){
                if(firstargument == null){
                    System.out.println("Must provide the first argument  ...  exiting...");
                    System.exit(0);
                }
                else {
                    System.out.println("First argument is " + firstargument);
                }
            }
            if(cmd.hasOption("O")) {
                // optional argument
                if (secondargument == null){
                    System.out.println("Second argument is NULL");
                }
                else{
                    // should end up here if optional argument is provided, but it doesn't happen
                    System.out.println("Second argument is " + secondargument);
                }
            }

        }
        catch( ParseException exp ) {
            // oops, something went wrong
            System.err.println( "Parsing failed.  Reason: " + exp.getMessage() );
        }
    }

}
上述代码的输出为:

Number of Arguments : 4
Command-line arguments: -R myfirstarg -O mysecondarg 
First argument is myfirstarg
Second argument is NULL
为什么“我的第二个”没有被抓获?
如果我将.optionalArg(true)行更改为.hasArg(true),则会捕获第二个参数,但整个想法是可以选择性地将第二个参数保留在外。

除了hasOptionalArgs之外,似乎还需要设置numberOfArgs,以使其正常工作。

还有另一个parse()方法,该方法接受称为stopAtNonOption的第三个参数选项

将stopAtNonOption设置为false将导致解析失败,并在到达未知参数时引发异常


我发现解析器在到达未知参数时会停止解析。

如果你也为它设置参数的数量会怎么样?@ohshazbot:我不确定我是否理解这个问题,但是如果我指定必须为-O选项提供参数,那么代码就可以工作——只要用户提供参数。但在实际应用程序中,用户需要能够选择-O,然后可选地提供参数或不提供参数。生成器上有一个附加标志,用于在有/没有参数时不设置,而是用于设置参数的数量。它是numberOfArgs@ohshzabot:我认为这没有帮助,因为参数的数量是可变的。实际上,我有大约20种不同的命令行标志,有些是必需的,有必需的参数,有些是可选的,有必需的参数,也有可选的参数。@ohshzabot:我说得太快了:我添加了一行“.numberOfArgs(1)”,这使它起作用了!谢谢你的帮助。不确定它是否应该这样工作,即是否打算指定“numberOfArgs”,但似乎是这样。如果你回答我,我可以把它标为接受。谢谢