我有一种情况field1
,我需要三个强制参数(field2
和可以输入)。field3
command name
create
list
有些命令会有参数,有些则没有。我该如何处理?
我尝试了以下操作,但出现错误:
ArgGroup has no options or positional parameters, and no subgroups
public class CliParserArgs {
@Option(names = {"--field1"}, required = true)
private String field1;
@Option(names = {"--field2"}, required = true)
String field2;
@Option(names={"--field3"}, required = true)
String field3;
@Option(names = {"-h", "--help"}, usageHelp = true) boolean help;
class Create {
private final String val;
public Create(final String val) {
this.val = val;
}
}
class ListObjects {
private final String val;
public ListObjects(final String val) {
this.val = val;
}
}
@ArgGroup(heading = "Command", exclusive = true, multiplicity = "1")
Create create;
ListObjects listObjects;
public static void main(String[] args) {
CliParserArgs cliParserArgs = new CliParserArgs();
CommandLine cmd = new CommandLine(cliParserArgs);
CommandLine.ParseResult parseResult = cmd.parseArgs(args);
System.err.println("parse results: " + parseResult.matchedArgs().toString());
try {
if (cmd.isUsageHelpRequested()) {
cmd.usage(System.out);
}
} catch (CommandLine.ParameterException e) {
System.err.println("error: " + e.getMessage());
System.err.println(e.getStackTrace());
}
}
}