Using Picocli, how do I require a positional argument and then optional arguments depending on the value of the positional argument
See original GitHub issueI 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());
}
}
}
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (6 by maintainers)
Top Results From Across the Web
Using Picocli, how do I require a positional argument and then ...
It sounds like you want to create a command with subcommands. You can do this in picocli by either marking a method with...
Read more >CommandLine.Parameters (picocli 4.7.0 API)
For single-value parameters, setting arity = "0..1" makes a positional parameter optional, while setting arity = "1" makes it required. Required parameters that ......
Read more >picocli - a mighty tiny command line interface
Command line arguments can be separated into options and positional parameters. Options have a name, positional parameters are usually the values that follow ......
Read more >Quick Guide - Picocli
Any command line arguments that are not subcommands, options or option parameters are interpreted as positional parameters. Use the (zero-based) ...
Read more >CommandLine.Option (picocli 4.7.0 API)
Returns the default value of this option, before splitting and type conversion. ... By default, all options and positional parameters are included in...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Having a look at your code, I realise that none of your classes has a @Command annonation. These annotations are used to declare classes as commands. This is not only true for the main command, but also for subcommands. And yes, derived from your description, I guess subcommands are what you are looking for. Please have a look at the Subcommands documentation. Also the picocli codebase has a minimal working example on subcommands.
Hopefully, the code below will do what you need:
I hope this example can serve as a starting point. Don’t hesitate to ask again once you have further questions!
Great, thank you!