I have a situation where I need to have three mandatory arguments (field1
, field2
and field3
. I then want the user to enter a command name
(mandatory, values can be create
, list
, etc). The command name must be entered, and must be singular (only one of them can be entered).
Some of the commands will have arguments, some of them will not. How do I handle that?
I tried the following, but I get an error:
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());
}
}
}