-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Make copyInheritedSettings()
recursive
#1922
Make copyInheritedSettings()
recursive
#1922
Conversation
I had not thought of the situation when adding a tree. To some extent |
@@ -106,6 +106,10 @@ class Command extends EventEmitter { | |||
this._showHelpAfterError = sourceCommand._showHelpAfterError; | |||
this._showSuggestionAfterError = sourceCommand._showSuggestionAfterError; | |||
|
|||
this.commands.forEach(command => { | |||
command.copyInheritedSettings(this); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Somewhat cosmetic after the copy, but I suggest:
command.copyInheritedSettings(this); | |
command.copyInheritedSettings(sourceCommand); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wrote this
here because if later some logic is added to prevent copying values for settings that had been manually set before calling copyInheritedSettings()
, we want the values to be copied to the subcommands from this
rather than from sourceCommand
.
At the risk of making the Use case: want to change behavior of an entire command hierarchy when the root command is returned from a function with all subcommands already registered (like in https://stackoverflow.com/questions/76896860 first referenced in #1917 (comment)), for example for testing purposes (setting Even with changes introduced in this PR, the Could also dump this PR if the suggestion is accepted. The change would be non-breaking then. |
Currently Given it is easy enough for author to implement exactly what they want, and I think a different approach would be better for the The apply-settings-after-creating-program situation might be handled by something like:
|
It's a good thing this PR was kept open long enough for me to come up with a better idea :)) I think just adding support for the call with no arguments is great because the suggested behavior is likely what most users really want when they use |
Pull Request
Problem
Pretty much all the inherited settings are such that it is only really meaningful for them to have the same values across the entire command hierarchy. However,
copyInheritedSettings()
only copies them at one level instead of doing it recursively. Because of that, the intended usage pattern is currently that the user adds subsubcommands only after the subcommand was added to its parent if the parent's inherited settings are meddled with (as clarified here).Demo
Solution
Make
copyInheritedSettings()
recursive.ChangeLog
Changed
.copyInheritedSettings()
recursive (settings are now inherited by the entire command hierarchy)