Skip to content
Myr

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

4 files+77 added−8 removed

added:tests/command.createOption.test.js+42 added
modified:typings/commander-tests.ts+4 added-1 removed
133 unmodified lines
134const requiredOptionThis9: commander.Command = program.requiredOption('-v, --verbose', 'verbosity that can be increased', increaseVerbosity, 0);135const requiredOptionThis10: commander.Command = program.requiredOption('-c, --collect <value>', 'repeatable value', collect, []);136const requiredOptionThis11: commander.Command = program.requiredOption('-l, --list <items>', 'comma separated list', commaSeparatedList);137138// createOption139const createOption1: commander.Option = program.createOption('a, --alpha');140const createOption2: commander.Option = program.createOption('a, --alpha', 'description');141142// addOption143const addOptionThis: commander.Command = program.addOption(new commander.Option('-s,--simple'));144145// storeOptionsAsProperties
91 unmodified lines
237});238239// createCommand240237const createInstance1: commander.Command = program.createCommand();241const createInstance2: commander.Command = program.createCommand('name');242243class MyCommand extends commander.Command {244 createCommand(name?: string): MyCommand {
84 unmodified lines
modified:typings/index.d.ts+9 added
321 unmodified lines
322 /** @deprecated since v7, instead use choices or a custom function */323 requiredOption(flags: string, description: string, regexp: RegExp, defaultValue?: string | boolean): this;324325 /**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 */331332 createOption(flags: string, description?: string): Option;333334 /**335 * Add a prepared Option.336 *337 * See .option() and .requiredOption() for creating and attaching an option in a single call.338 */
225 unmodified lines
modified:index.js+22 added-7 removed
56 unmodified lines
57 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 lines
912 }913 };914915 /**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 */925926 createOption(flags, description) {927 return new Option(flags, description);928 };929930 /**931 * Add an option.932 *933 * @param {Option} option934 * @return {Command} `this` command for chaining
70 unmodified lines
1005 *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 lines
1069 * // 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 lines
1086 *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 lines
1734 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