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

Correctly handle allowUnknownOption in .parseOptions() #1946

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 40 additions & 18 deletions lib/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -1445,6 +1445,8 @@ Expecting one of '${allowedValues.join("', '")}'`);

// parse options
let activeVariadicOption = null;
let onlyKnownOptionsSoFar = true;
let reprocessedBySubcommand = false;
while (args.length) {
const arg = args.shift();

Expand Down Expand Up @@ -1510,30 +1512,50 @@ Expecting one of '${allowedValues.join("', '")}'`);
}
}

// Not a recognised option by this command.
// Might be a command-argument, or subcommand option, or unknown option, or help command or option.
// Not a recognised option by this command. Might be
// - a subcommand,
// - an option unknown to this command,
// - or a command-argument.

// An unknown option means further arguments also classified as unknown so can be reprocessed by subcommands.
if (maybeOption(arg)) {
dest = unknown;
}

// If using positionalOptions, stop processing our options at subcommand.
if ((this._enablePositionalOptions || this._passThroughOptions) && operands.length === 0 && unknown.length === 0) {
if (onlyKnownOptionsSoFar && !reprocessedBySubcommand) {
reprocessedBySubcommand = true; // reset to false later if not entering subcommand
const stopAtSubcommand = (
this._enablePositionalOptions || this._passThroughOptions
);
if (this._findCommand(arg)) {
operands.push(arg);
if (args.length > 0) unknown.push(...args);
break;
if (stopAtSubcommand) {
operands.push(arg);
unknown.push(...args);
break;
}
} else if (arg === this._helpCommandName && this._hasImplicitHelpCommand()) {
operands.push(arg);
if (args.length > 0) operands.push(...args);
break;
if (stopAtSubcommand) {
operands.push(arg, ...args);
break;
}
} else if (this._defaultCommandName) {
unknown.push(arg);
if (args.length > 0) unknown.push(...args);
break;
if (stopAtSubcommand) {
unknown.push(arg, ...args);
break;
}
} else {
reprocessedBySubcommand = false;
}
}
onlyKnownOptionsSoFar = false;

// Respect the fact that the working principle of allowUnknownOption is to treat unknown options as command-arguments.
const treatUnknownOptionAsCommandArgument = (
this._allowUnknownOption && !reprocessedBySubcommand
);
const isUnknownOption = (
maybeOption(arg) && !treatUnknownOptionAsCommandArgument
);

// An unknown option means further arguments also classified as unknown so can be reprocessed by subcommands.
if (isUnknownOption) {
dest = unknown;
}

// If using passThroughOptions, stop processing options at first command-argument.
if (this._passThroughOptions) {
Expand Down
8 changes: 8 additions & 0 deletions tests/command.allowUnknownOption.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,12 @@ describe('allowUnknownOption', () => {
program.parse(['node', 'test', 'sub', '-m']);
}).not.toThrow();
});

test('when specify unknown program option and allowUnknownOption then unknown option parsed as operand', () => {
const program = new commander.Command();
program
.allowUnknownOption();
const result = program.parseOptions(['-m']);
expect(result).toEqual({ operands: ['-m'], unknown: [] });
});
});
16 changes: 0 additions & 16 deletions tests/command.helpCommand.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,6 @@ describe('help command listed in helpInformation', () => {
expect(helpInformation).toMatch(/help \[command\]/);
});

test('when program has subcommands and specify only unknown option then display help', () => {
const program = new commander.Command();
program
.configureHelp({ formatHelp: () => '' })
.exitOverride()
.allowUnknownOption()
.command('foo');
let caughtErr;
try {
program.parse(['--unknown'], { from: 'user' });
} catch (err) {
caughtErr = err;
}
expect(caughtErr.code).toEqual('commander.help');
});

test('when program has subcommands and suppress help command then no help command', () => {
const program = new commander.Command();
program.addHelpCommand(false);
Expand Down