Replies: 1 comment
-
|
Great question! The issue is that picocli validates all required parameters of the parent command before routing to a subcommand. Since your The idiomatic picocli fix is to make the parameter optional with @Command(name = "generator", subcommands = { ValidateCommand.class })
class Generator implements Runnable {
@Parameters(paramLabel = "FILE", arity = "0..1", description = "Excel input file")
private File xls;
@Spec CommandSpec spec;
@Override
public void run() {
ParseResult pr = spec.commandLine().getParseResult();
if (!pr.hasSubcommand() && xls == null) {
throw new ParameterException(spec.commandLine(),
"Missing required parameter: 'FILE'");
}
if (xls != null) {
// proceed with file
}
}
}The key is This is the recommended approach from the picocli documentation on subcommands and is used in many real-world picocli applications. Hope this helps! Let me know if you need clarification. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi,
I have a small application which reads a file, this is specified as in the main
@Commandclass:The application also has a subcommand, called
validate, which doesn't require that parameter, but takes a list of Files instead. However, when trying to execute the validate subcommand, i get the following error:I can call the help subcommand on
validate:But trying to call the help mixin via
-hreturns the main usage message:It seems either me or picocli is doing something wrong (I'm assuming it's me). Any suggestions?
(By making the
FILEparameter arity "0..1" and manually validating whether it's been passed to the main command, I can get it to work. That doesn't quite feel like it's correct though.)Beta Was this translation helpful? Give feedback.
All reactions