Skip to content
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

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion Readme.md
Expand Up @@ -546,7 +546,7 @@ subcommand is specified ([example](./examples/defaultCommand.js)).

You can add alternative names for a command with `.alias()`. ([example](./examples/alias.js))

For safety, `.addCommand()` does not automatically copy the inherited settings from the parent command. There is a helper routine `.copyInheritedSettings()` for copying the settings when they are wanted.
For safety, `.addCommand()` does not automatically copy the inherited settings from the parent command. There is a helper routine `.copyInheritedSettings()` for copying the settings when they are wanted. The copying is done recursively, so the settings are inherited by the entire subcommand hierarchy.

### Command-arguments

Expand Down
4 changes: 4 additions & 0 deletions lib/command.js
Expand Up @@ -106,6 +106,10 @@ class Command extends EventEmitter {
this._showHelpAfterError = sourceCommand._showHelpAfterError;
this._showSuggestionAfterError = sourceCommand._showSuggestionAfterError;

this.commands.forEach(command => {
command.copyInheritedSettings(this);
Copy link
Collaborator

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:

Suggested change
command.copyInheritedSettings(this);
command.copyInheritedSettings(sourceCommand);

Copy link
Contributor Author

@aweebit aweebit Aug 1, 2023

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.

});

return this;
}

Expand Down
22 changes: 22 additions & 0 deletions tests/command.copySettings.test.js
Expand Up @@ -18,6 +18,28 @@ test('when add subcommand with .command() then calls copyInheritedSettings from
expect(copySettingMock).toHaveBeenCalledWith(program);
});

test('when copyInheritedSettings on command with subcommand hierarchy then copies recursively', () => {
const source = new commander.Command();
const cmd = new commander.Command();

const descendants = Array.from({ length: 4 }, (_, i) => (
new commander.Command(String(i))
));
const parents = [cmd, cmd, descendants[0], descendants[1]];
descendants.forEach((descendant, i) => {
descendant.copyInheritedSettings = jest.fn().mockImplementation(
descendant.copyInheritedSettings
);
parents[i].addCommand(descendant);
});

cmd.copyInheritedSettings(source);
descendants.forEach((descendant, i) => {
const copySettingMock = descendant.copyInheritedSettings;
expect(copySettingMock).toHaveBeenCalledWith(parents[i]);
});
});

describe('copyInheritedSettings property tests', () => {
test('when copyInheritedSettings then copies outputConfiguration(config)', () => {
const source = new commander.Command();
Expand Down