Skip to content

Commit

Permalink
Check for conflicts in parent commands (#1711)
Browse files Browse the repository at this point in the history
* Rename test file

* Add test for confilcts in parent command of called command

* Check for conflicts up hierarchy of commands

* Suppress help output from test

* Remove bogus parameter
  • Loading branch information
shadowspawn authored Mar 25, 2022
1 parent 7d7a674 commit bf205d1
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 4 deletions.
20 changes: 17 additions & 3 deletions lib/command.js
Original file line number Diff line number Diff line change
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() {
// 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
Original file line number Diff line number Diff line change
@@ -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');
});
});

0 comments on commit bf205d1

Please sign in to comment.