-
Notifications
You must be signed in to change notification settings - Fork 422
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[ideas] MultiOption Validation #1410
Comments
You can achieve the desired outcome by throwing a See the user manual section on custom validation: https://picocli.info/#_custom_validation |
Thx to pointing this 🙏 (I will be able to remove my custom I feel there is maybe still good point in ideas above.
e.g : public class mySection implements ValidatableArgGroup{
@Option(names = { "-a", "--option-a" }, defaultValue = "Default A", description = "Should be 4th")
private String a;
@Option(names = { "-b", "--option-b" }, defaultValue = "Default B", description = "Should be 3rd")
private String b;
public void validate(CommandSpec cmd, ArgGroup ArgGroup) throws ParameterException {
// Do some validation
if (!valid)
throw new ArgGroupParameterException(cmd, Argroup, "this is not valid because ....");
}
} and then can be reusable like this : Command(name = "myCommand",
mixinStandardHelpOptions = true,
description = "A command with not well ordered option.",
sortOptions = false)
public class Example {
@ArgGroup(validate = false,
heading = "%n@|italic " //
+ "A reused Section with custom validation." //
+ "|@%n")
public mySection reusedSection = new mySection();
public static void main(String... args) {
Example example = new Example();
int exitCode = new CommandLine(example).execute(args);
System.exit(exitCode);
}
} There is nothing blocking to me here but I feel this kind of modification could make the API better and fit very well with #1407. |
Just to precise that I need a custom if (ex instanceof /*InvalidOptionsException*/ MultiParameterException) {
writer.printf("%n");
Help help = cmd.getHelpFactory().create(cmd.getCommandSpec(), cmd.getColorScheme());
Layout layout = help.createDefaultLayout();
for (String optionName : ((/*InvalidOptionsException*/ MultiParameterException) ex).getOptions()) {
OptionSpec option = cmd.getCommandSpec().findOption(optionName);
if (option != null) {
layout.addOption(option, help.createDefaultParamLabelRenderer());
}
}
writer.print(layout.toString());
} |
You can obtain the group via |
Overall, I feel that picocli as a library should be very minimalistic about validation. The ArgGroup validation idea is interesting, I can see that. But then for picocli to implement just that, without also providing something similar for general Commands, would be imbalanced/incomplete. So we are back where we started: I think picocli should not provide validation API, but instead have documentation that shows how applications can do validation in different scenarios. If you found a new scenario or a way to do things with the existing API that belongs in the docs or in the |
Good discussions, many thanks! |
Thx to
ITypeConverter
we have a very good way to do validation for one option input. 👍Coupled with a #1407 it is even possible to display only the help usage corresponding to the error.
But in some cases we need more complex multi option validation mechanism. Especially because using ArgGroup to build complex rules could be quickly verbose and reveal some corner case issue. ( e.g. #1409 )
When I create my commands I would like be able to make it inherit some kind of
Validable
interface with avalidate()
method which will be called before therun()
orcall()
method (and obviously after the args parsing)The
validate()
raise exception if there was any invalid input and will be handled byIParameterExceptionHandler
.The exception could concern several options and maybe
IParameterExceptionHandler
wanted to know each options are concerns to display target help usage (e.g. display only help for option in error or maybe only the ArgGroup in error)So we probably need a kind of
MultiParameterException
.We can imagine that we could also make validation as ArgGroup level by making it implement
Validable
too.If an exception is raised, it could be catch in a ArgGroupParameterException. (something like this)
As this mechanism does not exist for now, I workaround this by using run() or call() like if it was a validate method. Consequently, I need to execute my command out of the Command definition class but in my use case this is not an issue.
Then I create an
IExecutionExceptionHandler
which acts like aIParameterExceptionHandler
.I share some code :
Example of validations :
The text was updated successfully, but these errors were encountered: