-
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
Variable Interpolation appears to ignore required = true
#676
Comments
Thanks for raising this! I’ll try to take a more detailed look later today. |
I pushed a fix for this to master. |
I can confirm that interpolated value is And to reiterate, I don't specifically want to use |
@ifedorenko Thanks for the confirmation. I've been able to reproduce the validation issue. This is a bug introduced by the variable interpolation: picocli should not consider options required if a default value exists. The current code simply checks that the Your use case certainly sounds reasonable and I imagine it is fairly common for applications to have environment variables that can be overridden with command line options. It would be nice if we can use the |
@ifedorenko I've pushed a fix and an additional test to master. I believe the validation should work correctly now, but please verify. Don't hesitate to reopen this or open additional tickets if there's any problem. The fix will be included in 4.0-alpha-3. Thanks again for raising this! |
Yes, I can confirm current master correctly fails with |
From 4.0-alpha-3, you will be able to customize the error message with a custom final CommandLine commandLine = new CommandLine(new MyApp());
IParameterExceptionHandler handler = new IParameterExceptionHandler() {
IParameterExceptionHandler defaultHandler = commandLine.getParameterExceptionHandler();
public int handleParseException(ParameterException ex, String[] args) throws Exception {
if ("Missing required option '--mypath=<path>'".equals(ex.getMessage())) {
CommandLine cmd = ex.getCommandLine();
cmd.getErr().println(ex.getMessage() + ", alternatively specify environment variable MYPATH");
cmd.usage(cmd.getErr());
return cmd.getCommandSpec().exitCodeOnInvalidInput();
}
return defaultHandler.handleParseException(ex, args);
}
};
int exitCode = commandLine
.setParameterExceptionHandler(handler)
.execute(args); |
FYI: this is a good use case for demonstrating custom parameter exception handlers. I added a slightly more robust variation of the above to the |
As a user, I expect the code below to trigger
MissingParameterException
if neither--mypath
parameter nor$MYPATH
environment variable are set at runtime. Currently the parameter value is set tonull
.Note that I don't specifically care to use
defaultValue
annotation attribute, I just need a way to populate parameter value from--mypath
or$MYPATH
.The text was updated successfully, but these errors were encountered: