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 all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 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;
cmd._checkForBrokenPassThrough();

return this;
}

Expand Down Expand Up @@ -735,12 +737,20 @@ 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();
return this;
}

/**
* @api private
*/

_checkForBrokenPassThrough() {
if (this.parent && this._passThroughOptions && !this.parent._enablePositionalOptions) {
throw new Error(`passThroughOptions cannot be used for '${this._name}' without turning on enablePositionalOptions for parent command(s)`);
}
}

/**
* 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