Feature createOption (#1380)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* Add createOption (like createCommand and createHelp) * Add tests * Add TypeScript * option description is optional
authored by John Gee · committed by GitHub · parent 033e6d9 · Browse files · 4 files·+77 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
4 files+77 added−8 removed
- tests:+42 added−0 removed
- other:+35 added−8 removed
Provenance
- Commit
b1d984b- Tree
9100176- Author
John Gee <john@ruru.gen.nz>- Committer
GitHub <noreply@github.com>- Parent
033e6d9
4 files changed+77 added−8 removed
added:tests/command.createOption.test.js+42 added
123456789101112131415161718192021222324252627282930313233343536373839404142const commander = require('../');
class MyOption extends commander.Option { constructor(flags, description) { super(flags, description); this.myProperty = 'MyOption'; };}
class MyCommand extends commander.Command { createOption(flags, description) { return new MyOption(flags, description); };}
test('when override createOption then used for option()', () => { const program = new MyCommand(); program.option('-a, --alpha'); expect(program.options.length).toEqual(1); expect(program.options[0].myProperty).toEqual('MyOption');});
test('when override createOption then used for requiredOption()', () => { const program = new MyCommand(); program.requiredOption('-a, --alpha'); expect(program.options.length).toEqual(1); expect(program.options[0].myProperty).toEqual('MyOption');});
test('when override createOption then used for version()', () => { const program = new MyCommand(); program.version('1.2.3'); expect(program.options.length).toEqual(1); expect(program.options[0].myProperty).toEqual('MyOption');});
test('when override createOption then used for help option in visibleOptions', () => { const program = new MyCommand(); const visibleOptions = program.createHelp().visibleOptions(program); expect(visibleOptions.length).toEqual(1); expect(visibleOptions[0].myProperty).toEqual('MyOption');});modified:typings/commander-tests.ts+4 added-1 removed
133 unmodified lines134 const requiredOptionThis9: commander.Command = program.requiredOption('-v, --verbose', 'verbosity that can be increased', increaseVerbosity, 0);135 const requiredOptionThis10: commander.Command = program.requiredOption('-c, --collect <value>', 'repeatable value', collect, []);136 const requiredOptionThis11: commander.Command = program.requiredOption('-l, --list <items>', 'comma separated list', commaSeparatedList);137 138+// createOption139+const createOption1: commander.Option = program.createOption('a, --alpha');140+const createOption2: commander.Option = program.createOption('a, --alpha', 'description');141+142 // addOption143 const addOptionThis: commander.Command = program.addOption(new commander.Option('-s,--simple'));144 145 // storeOptionsAsProperties91 unmodified lines237 });238 239 // createCommand240 237-const createInstance1: commander.Command = program.createCommand();241 const createInstance2: commander.Command = program.createCommand('name');242 243 class MyCommand extends commander.Command {244 createCommand(name?: string): MyCommand {84 unmodified linesmodified:typings/index.d.ts+9 added
321 unmodified lines322 /** @deprecated since v7, instead use choices or a custom function */323 requiredOption(flags: string, description: string, regexp: RegExp, defaultValue?: string | boolean): this;324 325 /**326+ * Factory routine to create a new unattached option.327+ *328+ * See .option() for creating an attached option, which uses this routine to329+ * create the option. You can override createOption to return a custom option.330+ */331+332+ createOption(flags: string, description?: string): Option;333+334+ /**335 * Add a prepared Option.336 *337 * See .option() and .requiredOption() for creating and attaching an option in a single call.338 */225 unmodified linesmodified:index.js+22 added-7 removed
56 unmodified lines57 const showLongHelpFlag = cmd._hasHelpOption && !cmd._findOption(cmd._helpLongFlag);58 if (showShortHelpFlag || showLongHelpFlag) {59 let helpOption;60 if (!showShortHelpFlag) {61- helpOption = new Option(cmd._helpLongFlag, cmd._helpDescription);61+ helpOption = cmd.createOption(cmd._helpLongFlag, cmd._helpDescription);62 } else if (!showLongHelpFlag) {63- helpOption = new Option(cmd._helpShortFlag, cmd._helpDescription);63+ helpOption = cmd.createOption(cmd._helpShortFlag, cmd._helpDescription);64 } else {65- helpOption = new Option(cmd._helpFlags, cmd._helpDescription);65+ helpOption = cmd.createOption(cmd._helpFlags, cmd._helpDescription);66 }67 visibleOptions.push(helpOption);68 }69 if (this.sortOptions) {842 unmodified lines912 }913 };914 915 /**916+ * Factory routine to create a new unattached option.917+ *918+ * See .option() for creating an attached option, which uses this routine to919+ * create the option. You can override createOption to return a custom option.920+ *921+ * @param {string} flags922+ * @param {string} [description]923+ * @return {Option} new option924+ */925+926+ createOption(flags, description) {927+ return new Option(flags, description);928+ };929+930+ /**931 * Add an option.932 *933 * @param {Option} option934 * @return {Command} `this` command for chaining70 unmodified lines1005 *1006 * @api private1007 */1008 _optionEx(config, flags, description, fn, defaultValue) {994- const option = new Option(flags, description);1009+ const option = this.createOption(flags, description);1010 option.makeOptionMandatory(!!config.mandatory);1011 if (typeof fn === 'function') {1012 option.default(defaultValue).argParser(fn);1013 } else if (fn instanceof RegExp) {55 unmodified lines1069 * // optional argument1070 * program.option('-c, --cheese [type]', 'add cheese [marble]');1071 *1072 * @param {string} flags1058- * @param {string} description1073+ * @param {string} [description]1074 * @param {Function|*} [fn] - custom option processing function or default value1075 * @param {*} [defaultValue]1076 * @return {Command} `this` command for chaining1077 */8 unmodified lines1086 *1087 * The `flags` string contains the short and/or long flags, separated by comma, a pipe or space.1088 *1089 * @param {string} flags1075- * @param {string} description1090+ * @param {string} [description]1091 * @param {Function|*} [fn] - custom option processing function or default value1092 * @param {*} [defaultValue]1093 * @return {Command} `this` command for chaining1094 */639 unmodified lines1734 if (str === undefined) return this._version;1735 this._version = str;1736 flags = flags || '-V, --version';1737 description = description || 'output the version number';1723- const versionOption = new Option(flags, description);1738+ const versionOption = this.createOption(flags, description);1739 this._versionOptionName = versionOption.attributeName();1740 this.options.push(versionOption);1741 this.on('option:' + versionOption.name(), () => {1742 process.stdout.write(str + '\n');374 unmodified lines