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

Check for conflicts in parent commands #1711

Merged
merged 5 commits into from Mar 25, 2022
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
20 changes: 17 additions & 3 deletions lib/command.js
Expand Up @@ -946,6 +946,7 @@ Expecting one of '${allowedValues.join("', '")}'`);

// Not checking for help first. Unlikely to have mandatory and executable, and can't robustly test for help flags in external command.
this._checkForMissingMandatoryOptions();
this._checkForConflictingOptions();

// executableFile and executableDir might be full path, or just a name
let executableFile = subcommand._executableFile || `${this._name}-${subcommand._name}`;
Expand Down Expand Up @@ -1294,7 +1295,7 @@ Expecting one of '${allowedValues.join("', '")}'`);

/**
* Display an error message if a mandatory option does not have a value.
* Lazy calling after checking for help flags from leaf subcommand.
* Called after checking for help flags in leaf subcommand.
*
* @api private
*/
Expand All @@ -1311,11 +1312,11 @@ Expecting one of '${allowedValues.join("', '")}'`);
}

/**
* Display an error message if conflicting options are used together.
* Display an error message if conflicting options are used together in this.
*
* @api private
*/
_checkForConflictingOptions() {
_checkForConflictingLocalOptions() {
const definedNonDefaultOptions = this.options.filter(
(option) => {
const optionKey = option.attributeName();
Expand All @@ -1340,6 +1341,19 @@ Expecting one of '${allowedValues.join("', '")}'`);
});
}

/**
* Display an error message if conflicting options are used together.
* Called after checking for help flags in leaf subcommand.
*
* @api private
*/
_checkForConflictingOptions(cmd) {
shadowspawn marked this conversation as resolved.
Show resolved Hide resolved
// Walk up hierarchy so can call in subcommand after checking for displaying help.
for (let cmd = this; cmd; cmd = cmd.parent) {
cmd._checkForConflictingLocalOptions();
}
}

/**
* Parse options from `argv` removing known options,
* and return argv split into operands and unknown arguments.
Expand Down
@@ -1,3 +1,4 @@
const path = require('path');
const commander = require('../');

describe('command with conflicting options', () => {
Expand All @@ -7,7 +8,8 @@ describe('command with conflicting options', () => {
program
.exitOverride()
.configureOutput({
writeErr: () => {}
writeErr: () => {},
writeOut: () => {}
})
.command('foo')
.addOption(new commander.Option('-s, --silent', "Don't print anything").env('SILENT'))
Expand Down Expand Up @@ -253,4 +255,57 @@ describe('command with conflicting options', () => {
program.parse('node test.js bar --a --b'.split(' '));
}).toThrow("error: option '--b' cannot be used with option '--a'");
});

test('when conflict on program calling action subcommand then throw conflict', () => {
const { program } = makeProgram();
let exception;

program
.addOption(new commander.Option('--black'))
.addOption(new commander.Option('--white').conflicts('black'));

try {
program.parse('--white --black foo'.split(' '), { from: 'user' });
} catch (err) {
exception = err;
}
expect(exception).not.toBeUndefined();
expect(exception.code).toBe('commander.conflictingOption');
});

test('when conflict on program calling action subcommand with help then show help', () => {
const { program } = makeProgram();
let exception;

program
.addOption(new commander.Option('--black'))
.addOption(new commander.Option('--white').conflicts('black'));

try {
program.parse('--white --black foo --help'.split(' '), { from: 'user' });
} catch (err) {
exception = err;
}
expect(exception).not.toBeUndefined();
expect(exception.code).toBe('commander.helpDisplayed');
});

test('when conflict on program calling external subcommand then throw conflict', () => {
const { program } = makeProgram();
let exception;

program
.addOption(new commander.Option('--black'))
.addOption(new commander.Option('--white').conflicts('black'));
const pm = path.join(__dirname, './fixtures/pm');
program.command('ext', 'external command', { executableFile: pm });

try {
program.parse('--white --black ext'.split(' '), { from: 'user' });
} catch (err) {
exception = err;
}
expect(exception).not.toBeUndefined();
expect(exception.code).toBe('commander.conflictingOption');
});
});