Check for missing required options after check for displaying help
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by John Gee · committed by John Gee · parent 6bfc580 · Browse files · 2 files·+68 added−8 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+68 added−8 removed
- tests:+52 added−0 removed
- other:+16 added−8 removed
Provenance
- Commit
36082ba- Tree
4d365ee- Author
John Gee <john@ruru.gen.nz>- Committer
John Gee <john@ruru.gen.nz>- Parent
6bfc580
2 files changed+68 added−8 removed
modified:tests/options.mandatory.test.js+52 added
1 const commander = require('../');2 3 // Assuming mandatory options behave as normal options apart from the mandatory aspect, not retesting all behaviour.4+// Likewise, not redoing all tests on subcommand after testing on program.5 6 describe('required program option with mandatory value specified', () => {7 test('when program has required value specified then value as specified', () => {8 const program = new commander.Command();215 unmodified lines224 program.parse(['node', 'test']);225 }).not.toThrow();226 });227 });228+229+describe('missing mandatory option but help requested', () => {230+ // Optional. Use internal knowledge to suppress output to keep test output clean.231+ let writeSpy;232+233+ beforeAll(() => {234+ writeSpy = jest.spyOn(process.stdout, 'write').mockImplementation(() => { });235+ });236+237+ afterEach(() => {238+ writeSpy.mockClear();239+ });240+241+ afterAll(() => {242+ writeSpy.mockRestore();243+ });244+245+ test('when program has required option not specified and --help then help', () => {246+ const program = new commander.Command();247+ program248+ .exitOverride()249+ .requiredOption('--cheese <type>', 'cheese type');250+251+ let caughtErr;252+ try {253+ program.parse(['node', 'test', '--help']);254+ } catch (err) {255+ caughtErr = err;256+ }257+258+ expect(caughtErr.code).toEqual('commander.helpDisplayed');259+ });260+261+ test('when program has required option not specified and subcommand --help then help', () => {262+ const program = new commander.Command();263+ program264+ .exitOverride()265+ .requiredOption('--cheese <type>', 'cheese type')266+ .command('sub')267+ .action(() => {});268+269+ let caughtErr;270+ try {271+ program.parse(['node', 'test', 'sub', '--help']);272+ } catch (err) {273+ caughtErr = err;274+ }275+276+ expect(caughtErr.code).toEqual('commander.helpDisplayed');277+ });278+});modified:index.js+16 added-8 removed
322 unmodified lines323 var parsed = self.parseOptions(unknown);324 325 // Output help if necessary326 outputHelpIfNecessary(self, parsed.unknown);327+ self._checkForMissingMandatoryOptions();328 329 // If there are still any unknown options, then we simply330 // die, unless someone asked for help, in which case we give it331 // to them, and then we die.231 unmodified lines563 var result = this.parseArgs(this.args, parsed.unknown);564 565 if (args[0] === 'help' && args.length === 1) this.help();566 567+ // Note for future: we could return early if we found an action handler in parseArgs, as none of following code needed?568+569 // <cmd> --help570 if (args[0] === 'help') {571 args[0] = args[1];572 args[1] = this._helpLongFlag;573+ } else {574+ // If calling through to executable subcommand we could check for help flags before failing,575+ // but a somewhat unlikely case since program options not passed to executable subcommands.576+ // Wait for reports to see if check needed and what usage pattern is.577+ this._checkForMissingMandatoryOptions();578 }579 580 // executable sub-commands581 // (Debugging note for future: args[0] is not right if an action has been called)257 unmodified lines839 * @api private840 */841 842 Command.prototype._checkForMissingMandatoryOptions = function() {843+ // Walk up hierarchy so can call from action handler after checking for displaying help.835- const self = this;836- this.options.forEach((anOption) => {837- if (anOption.mandatory && (self[anOption.attributeName()] === undefined)) {838- self.missingMandatoryOptionValue(anOption);839- }840- });844+ for (var cmd = this; cmd; cmd = cmd.parent) {845+ cmd.options.forEach((anOption) => {846+ if (anOption.mandatory && (cmd[anOption.attributeName()] === undefined)) {847+ cmd.missingMandatoryOptionValue(anOption);848+ }849+ });850+ }851 };852 853 /**854 * Parse options from `argv` returning `argv`70 unmodified lines925 // arg926 args.push(arg);927 }928 919- this._checkForMissingMandatoryOptions();920-929 return { args: args, unknown: unknownOptions };930 };931 932 /**618 unmodified lines