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

addCommonOption for options common to subcommands #1670

Closed
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
31 changes: 31 additions & 0 deletions lib/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class Command extends EventEmitter {
this.commands = [];
/** @type {Option[]} */
this.options = [];
/** @type {Option[]} */
this.commonOptions = []; // for subcommands
this.parent = null;
this._allowUnknownOption = false;
this._allowExcessArguments = true;
Expand Down Expand Up @@ -107,6 +109,21 @@ class Command extends EventEmitter {
return this;
}

/**
* Copy common options from sourceCommand
*
* (Used internally when adding a command using `.command()`.)
*
* @param {Command} sourceCommand
* @return {Command} `this` command for chaining
*/
copyCommonOptions(sourceCommand) {
sourceCommand.commonOptions.forEach((commonOption) => {
this.addOption(commonOption);
});
return this;
}

/**
* Define a command.
*
Expand Down Expand Up @@ -154,6 +171,7 @@ class Command extends EventEmitter {
this.commands.push(cmd);
cmd.parent = this;
cmd.copyInheritedSettings(this);
cmd.copyCommonOptions(this);

if (desc) return this;
return cmd;
Expand Down Expand Up @@ -574,6 +592,19 @@ Expecting one of '${allowedValues.join("', '")}'`);
return this;
}

/**
* Add a common option for subcommands created using .command().
*
* (See also .copyCommonOptions() for manual copying.)
*
* @param {Option} option
* @return {Command} `this` command for chaining
*/
addCommonOption(option) {
this.commonOptions.push(option);
return this;
}

/**
* Internal implementation shared by .option() and .requiredOption()
*
Expand Down
13 changes: 13 additions & 0 deletions tests/command.chain.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ describe('Command methods that should return this for chaining', () => {
expect(result).toBe(program);
});

test('when call .addCommonOption() then returns this', () => {
const program = new Command();
const result = program.addCommonOption(new Option('-e'));
expect(result).toBe(program);
});

test('when call .option() then returns this', () => {
const program = new Command();
const result = program.option('-e');
Expand Down Expand Up @@ -203,6 +209,13 @@ describe('Command methods that should return this for chaining', () => {
expect(result).toBe(cmd);
});

test('when call .copyCommonOptions() then returns this', () => {
const program = new Command();
const cmd = new Command();
const result = cmd.copyCommonOptions(program);
expect(result).toBe(cmd);
});

test('when set .nameFromFilename() then returns this', () => {
const program = new Command();
const result = program.nameFromFilename('name');
Expand Down