尝试使用
Apache Commons Command Line Interface 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
为什么“mysecondarg”没有被捕获? 如果我将 .optionalArg(true) 行更改为 .hasArg(true),则捕获第二个参数,但整个想法是能够选择将第二个参数排除在外。
看来除了 hasOptionalArgs 之外,您还需要设置 numberOfArgs 才能正常工作。
还有另一个 parse() 方法,它采用第三个参数选项,称为 stopAtNonOption。
将 stopAtNonOption 设置为 false 会导致解析失败,并在到达未知参数时抛出异常。
我发现解析器在到达未知参数时停止解析。
Apache Command-CLI 过于冗长,导致误解和问题。您可以使用方便的包装纸:
<dependency>
<groupId>com.github.bogdanovmn.cmdline</groupId>
<artifactId>cmdline-app</artifactId>
<version>3.0.0</version>
</dependency>
原始代码将如下所示(按您的预期工作):
import com.github.bogdanovmn.cmdline.CmdLineAppBuilder;
public class TestCommandLine {
public static void main(String[] args) throws Exception {
new CmdLineAppBuilder(args)
.withArg("R", "Enter this required argument").required()
.withArg("O", "Enter this argument if you want to")
.withEntryPoint(options -> {
// The R arg is required, we shouldn't check it is specified
System.out.println("First argument is " + options.get("R"));
if (options.has("O")) {
String secondargument = options.get("O");
if (secondargument == null) {
// Will never go here
System.out.println("Second argument is NULL");
} else{
// should end up here if optional argument is provided
System.out.println("Second argument is " + secondargument);
}
}
})
.build().run();
}
}
如果命令行参数为“-R myfirstarg -O”,它将抛出运行时异常:
java.lang.RuntimeException: Missing argument for option: O