-
Notifications
You must be signed in to change notification settings - Fork 429
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
Inherited positional parameter gets overridden by default value if placed after subcommand #1250
Comments
The shows two calls that should normally be equivalent, but that have different end results, due to the incorrect handling of the "initialized" parameter state.
Another problem is that the help message itself puts the inherited positional parameter at the end, even though this is the only way that it does NOT work. So another option would be to move it back to before the subcommand in the usage/help message. |
Thanks for raising this! |
The shows two calls that should normally be equivalent, but that have different end results, due to the incorrect handling of the "initialized" parameter state.
If it's at all helpful, I'll share the findings from my debugging process: In the case where the positional parameter is set before the subcommand, the In the second case, the subcommand creates its own I initially tried to fix it by sharing one single |
@danielthegray Yes, that is helpful and your analysis makes a lot of sense. Thank you again for the test case, that is very helpful. (I've marked it as |
I had a chance to look at this. So, instead of calling private void addToInitialized(ArgSpec argSpec, Set<ArgSpec> initialized) {
initialized.add(argSpec);
CommandSpec parent = argSpec.command().parent();
while (argSpec.inherited() && parent != null) {
initialized.add(parent.findOriginal(argSpec));
parent = parent.parent();
}
} Then we still need to implement Come to think of it, I wonder why this problem does not occur for named options, or did I miss this in testing too? @danielthegray Would you be interested in providing a pull request for this? |
@remkop Sounds good! I will give this a try and submit a pull request as soon as I can. Thanks for your input! I'll also look into what happens with named options :) |
@remkop I implemented the described solution, but some other unit tests now fail. The one that most clearly shows it is:
which throws the following exception:
which is because of this check inside
So, the initialized is actually used as well to prevent overwriting the option twice inside the same evaluation context (of the subcommand I assume). Should I separate this into two cases perhaps? Will a mixin with a positional parameter clash with this? Another option I can think of is to change the |
At first glance, that exception looks correct: I need to go over the old tickets related to inherited options to see if this scenario was discussed. If not, your PR may be fine as is. |
Potentially related older tickets:
None of these issue discuss this scenario of end users setting an inherited option in the top-level command and then specifying it again on a subcommand. I think it is fine to treat this as a bug. I will raise a separate ticket for that, and your PR will fix both these issues. |
If a positional parameter is defined on a root command, with an
INHERIT
scope, and with a default value, the positional parameter value is only preserved correctly if it is written before the subcommand. If written after the subcommand, the default value gets overwritten. This should not happen, as it has already been initialized in the call.I have prepared a pull request with a test which showcases this failure.
The text was updated successfully, but these errors were encountered: