Skip to content
Myr

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

2 files+68 added−8 removed

modified:tests/options.mandatory.test.js+52 added
1const commander = require('../');23// 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.56describe('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 lines
224 program.parse(['node', 'test']);225 }).not.toThrow();226 });227});228229describe('missing mandatory option but help requested', () => {230 // Optional. Use internal knowledge to suppress output to keep test output clean.231 let writeSpy;232233 beforeAll(() => {234 writeSpy = jest.spyOn(process.stdout, 'write').mockImplementation(() => { });235 });236237 afterEach(() => {238 writeSpy.mockClear();239 });240241 afterAll(() => {242 writeSpy.mockRestore();243 });244245 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');250251 let caughtErr;252 try {253 program.parse(['node', 'test', '--help']);254 } catch (err) {255 caughtErr = err;256 }257258 expect(caughtErr.code).toEqual('commander.helpDisplayed');259 });260261 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(() => {});268269 let caughtErr;270 try {271 program.parse(['node', 'test', 'sub', '--help']);272 } catch (err) {273 caughtErr = err;274 }275276 expect(caughtErr.code).toEqual('commander.helpDisplayed');277 });278});
modified:index.js+16 added-8 removed
322 unmodified lines
323 var parsed = self.parseOptions(unknown);324325 // Output help if necessary326 outputHelpIfNecessary(self, parsed.unknown);327 self._checkForMissingMandatoryOptions();328329 // 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 lines
563 var result = this.parseArgs(this.args, parsed.unknown);564565 if (args[0] === 'help' && args.length === 1) this.help();566567 // Note for future: we could return early if we found an action handler in parseArgs, as none of following code needed?568569 // <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 }579580 // executable sub-commands581 // (Debugging note for future: args[0] is not right if an action has been called)
257 unmodified lines
839 * @api private840 */841842Command.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};852853/**854 * Parse options from `argv` returning `argv`
70 unmodified lines
925 // arg926 args.push(arg);927 }928919 this._checkForMissingMandatoryOptions();920929 return { args: args, unknown: unknownOptions };930};931932/**
618 unmodified lines