-
Notifications
You must be signed in to change notification settings - Fork 424
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
Required ArgGroup combined with DefaultValueProvider issue #1848
Comments
The following changes fix this issue in our application: Add method private boolean containsRequiredOptions(ArgGroupSpec argGroupSpec) {
for ( OptionSpec option : argGroupSpec.options() ) {
if ( option.required() ) { return true; }
}
return false;
} Call this new method in the if (missing.validate() && missing.multiplicity().min > 0 && containsRequiredOptions(missing) ) { I'm not sure though whether there's any valid use case for having |
Actually, the above didn't work well for a nested tree of The following updates to // In GroupMatchContainer::validate:
// if (missing.validate() && missing.multiplicity().min > 0 && containsRequiredOptionsOrSubgroups(missing) ) {
private boolean containsRequiredOptionsOrSubgroups(ArgGroupSpec argGroupSpec) {
return containsRequiredOptions(argGroupSpec) || containsRequiredSubgroups(argGroupSpec);
}
private boolean containsRequiredOptions(ArgGroupSpec argGroupSpec) {
for ( OptionSpec option : argGroupSpec.options() ) {
if ( option.required() ) { return true; }
}
return false;
}
private boolean containsRequiredSubgroups(ArgGroupSpec argGroupSpec) {
for ( ArgGroupSpec subgroup : argGroupSpec.subgroups() ) {
if ( subgroup.exclusive() ) {
// Only return true if all of the subgroups contain required options or subgroups
boolean result = true;
for ( ArgGroupSpec subsubgroup : subgroup.subgroups() ) {
result &= containsRequiredOptionsOrSubgroups(subsubgroup);
}
return result && containsRequiredOptions(subgroup);
} else {
return containsRequiredOptionsOrSubgroups(subgroup);
}
}
return false;
} The |
@remkop Any comments on this? Because of this issue, our project still needs to use a patched version of the |
The last attempt that handles both options and subgroups looks good. Can you provide a PR with tests? |
Hey @remkop. Sorry for the delay here, but I'll be submitting a PR soon with the fix from @rsenden and the associated unit test.
|
Hello @remkop and @rsenden . |
@wtfacoconut @rsenden I will try to get to this soon. |
Roger that, take your time. 🙂 |
Hello @remkop. Apologies, but on further testing, we're finding out that the PR we submitted (#2030) for this issue doesn't cover all scenarios. I'd like to request for this issue to be reopened. We've created a new fix for which we'll be submitting a new PR for. I'm just evaluating the existing unit test to see if it's sufficient before raising the next PR. |
Absolutely, no worries. Just a heads-up that I'll be traveling from Saturday for 2 weeks and I won't be taking my pc, so please be patient. 😅 |
No worries! Enjoy your trip and bring me back a t-shirt. 😉 |
Hi @wtfacoconut and @rsenden, it looks like #2030 has caused a regression: when an arg-group has multiplicity=1, then picocli should require that at least one of the options in the group is specified. This conflicts with your expectations in the description for this ticket. One idea is to revert the changes made in #2030, and satisfy your requirements by changing your application: change the Thoughts? |
Hi @wtfacoconut, thanks for the quick reply.
I am not sure that the problem is really related to required options/parameters: I am not sure how that squares with the description of this ticket (#1848), where you don't want that error (because the required option has a default value). To be honest I am not sure yet what the solution is. One idea is to model the multiplicity of the But it does appear that #2030 introduced a regression, and some change is needed to make the test in #947 pass again. |
Hello @remkop, I'm fully back on line now. I used the unit test that you provided in #947 to reproduce the issue that you mentioned. Are you saying that you're "not sure yet what the solution is" because there are some potentially deeper issues/implications, or something? Also, in the cotext of the unit test that you wrote for #947, would it be a reasonable expectation to have ArgGroups with a multiplicity of 1, recognize default values for options (provided by either an annotation or default value provider class), so that it doesn't cause an error about a missing option/value? |
Sorry for the late response. First (and this is an aside), looking at a question in an earlier comment:
This is not expected behaviour. I don't see why options/positional params in a group should not be able to get default values via Back to the main topic: I just pushed a change to improve the solution introduced in [4b41a21] by adding support that allows detecting whether an |
We have an ArgGroup defined like the following:
This usually works as expected, with picocli requiring at least the
--url
option and the other options being optional, with a synopsis like the following:(--url=<url> [--proxy-host=<proxyHost>] [--proxy-port=<proxyPort>] [--proxy-user=<proxyUser>] [--proxy-password]... [-k])
However, if we configure a global default value provider (in our case retrieving default values from environment variables), the synopsis changes to the following, indicating that
--url
is now optional:([--url=<url>] [--proxy-host=<proxyHost>] [--proxy-port=<proxyPort>] [--proxy-user=<proxyUser>] [--proxy-password]... [-k])
Due to
multiplicitly = "1"
on theArgGroup
, picocli still requires at least one of the options in theArgGroup
to be specified on the command line, even though the required--url
option is already being satisfied through the default value provider.Any suggestions on how to get this fixed?
The text was updated successfully, but these errors were encountered: