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

Add missing check for broken passThrough #1937

Merged
merged 6 commits into from
Aug 5, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
18 changes: 15 additions & 3 deletions lib/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,8 @@ class Command extends EventEmitter {

this.commands.push(cmd);
cmd.parent = this;
this._checkForBrokenPassThrough(cmd, this);

return this;
}

Expand Down Expand Up @@ -735,12 +737,22 @@ Expecting one of '${allowedValues.join("', '")}'`);
*/
passThroughOptions(passThrough = true) {
this._passThroughOptions = !!passThrough;
if (!!this.parent && passThrough && !this.parent._enablePositionalOptions) {
throw new Error('passThroughOptions can not be used without turning on enablePositionalOptions for parent command(s)');
}
this._checkForBrokenPassThrough(this, this.parent);
return this;
}

/**
* @param {Command} command
* @param {Command | null} parent
* @api private
*/

_checkForBrokenPassThrough(command, parent) {
aweebit marked this conversation as resolved.
Show resolved Hide resolved
if (parent && command._passThroughOptions && !parent._enablePositionalOptions) {
throw new Error(`passThroughOptions cannot be used for '${command._name}' without turning on enablePositionalOptions for parent command${parent._name ? ` '${parent._name}'` : ''}`);
aweebit marked this conversation as resolved.
Show resolved Hide resolved
}
}

/**
* Whether to store option values as properties on command object,
* or store separately (specify false). In both cases the option values can be accessed using .opts().
Expand Down
24 changes: 18 additions & 6 deletions tests/command.positionalOptions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -362,13 +362,25 @@ describe('program with action handler and positionalOptions and subcommand', ()

// ------------------------------------------------------------------------------

test('when program not positional and turn on passthrough in subcommand then error', () => {
const program = new commander.Command();
const sub = program.command('sub');
describe('broken passThrough', () => {
test('when program not positional and turn on passThroughOptions in subcommand then error', () => {
const program = new commander.Command();
const sub = program.command('sub');

expect(() => {
sub.passThroughOptions();
}).toThrow();
});

expect(() => {
sub.passThroughOptions();
}).toThrow();
test('when program not positional and add subcommand with passThroughOptions then error', () => {
const program = new commander.Command();
const sub = new commander.Command('sub')
.passThroughOptions();

expect(() => {
program.addCommand(sub);
}).toThrow();
});
});

// ------------------------------------------------------------------------------
Expand Down