Merge branch 'develop' into release/5.x
Before adoption: this commit came in with the history the project adopted, so no landing records it.
With 4.1 release
authored by John Gee · committed by John Gee · parents 80b3587, 4092956 · Browse files · 30 files·+154 added−83 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
30 files+154 added−83 removed
- tests:+16 added−0 removed
- other:+138 added−83 removed
Provenance
- Commit
491b2e4- Tree
6afe9cb- Author
John Gee <john@ruru.gen.nz>- Committer
John Gee <john@ruru.gen.nz>- Parent 1
80b3587- Parent 2
4092956
30 files changed+154 added−83 removed
modified:examples/custom-help+2 added-5 removed
1 #!/usr/bin/env node2 3-/**4- * Module dependencies.5- */3+// const program = require('commander'); // (normal include)4+const program = require('../'); // include commander in git clone of commander repo5 7-var program = require('../');8-6 program7 .version('0.0.1')8 .option('-f, --foo', 'enable some foo')9 .option('-b, --bar', 'enable some bar')16 unmodified linesmodified:examples/custom-help-description+2 added-5 removed
1 #!/usr/bin/env node2 3-/**4- * Module dependencies.5- */3+// const program = require('commander'); // (normal include)4+const program = require('../'); // include commander in git clone of commander repo5 7-var program = require('../');8-6 program7 .helpOption('-c, --HELP', 'custom help message')8 .option('-s, --sessions', 'add session support')9 .option('-t, --template <engine>', 'specify template engine (jade|ejs) [jade]', 'jade');9 unmodified linesmodified:examples/custom-version+2 added-5 removed
1 #!/usr/bin/env node2 3-/**4- * Module dependencies.5- */3+// const program = require('commander'); // (normal include)4+const program = require('../'); // include commander in git clone of commander repo5 7-var program = require('../');8-6 program7 .version('0.0.1', '-v, --VERSION', 'new version message')8 .option('-s, --sessions', 'add session support')9 .option('-t, --template <engine>', 'specify template engine (jade|ejs) [jade]', 'jade')1 unmodified linemodified:examples/defaults+2 added-5 removed
1 #!/usr/bin/env node2 3-/**4- * Module dependencies.5- */3+// const program = require('commander'); // (normal include)4+const program = require('../'); // include commander in git clone of commander repo5 7-var program = require('../');8-6 function list(val) {7 return val.split(',').map(Number);8 }9 10 unmodified linesmodified:examples/deploy+2 added-5 removed
1 #!/usr/bin/env node2 3-/**4- * Module dependencies.5- */3+// const program = require('commander'); // (normal include)4+const program = require('../'); // include commander in git clone of commander repo5 7-var program = require('../');8-6 program7 .version('0.0.1')8 .option('-C, --chdir <path>', 'change the working directory')9 .option('-c, --config <path>', 'set config path. defaults to ./deploy.conf')34 unmodified linesmodified:examples/description+2 added-5 removed
1 #!/usr/bin/env node2 3-/**4- * Module dependencies.5- */3+// const program = require('commander'); // (normal include)4+const program = require('../'); // include commander in git clone of commander repo5 7-var program = require('../');8-6 program7 .version('0.0.1')8 .description('Application simple description')9 .option('-f, --foo', 'enable some foo')5 unmodified linesmodified:examples/env+2 added-1 removed
1 #!/usr/bin/env node2 3-var program = require('../');3+// const program = require('commander'); // (normal include)4+const program = require('../'); // include commander in git clone of commander repo5 6 let cmdValue;7 let envValue;8 16 unmodified linesmodified:examples/express+2 added-5 removed
1 #!/usr/bin/env node2 3-/**4- * Module dependencies.5- */3+// const program = require('commander'); // (normal include)4+const program = require('../'); // include commander in git clone of commander repo5 7-var program = require('../');8-6 program7 .version('0.0.1')8 .option('-s, --sessions', 'add session support')9 .option('-t, --template <engine>', 'specify template engine (jade|ejs) [jade]', 'jade')6 unmodified linesmodified:examples/help+2 added-5 removed
1 #!/usr/bin/env node2 3-/**4- * Module dependencies.5- */3+// const program = require('commander'); // (normal include)4+const program = require('../'); // include commander in git clone of commander repo5 7-var program = require('../');8-6 program7 .version('0.0.1')8 .option('-f, --foo', 'enable some foo')9 .option('-b, --bar', 'enable some bar')4 unmodified linesmodified:examples/options-common.js+2 added-1 removed
18 unmodified lines19 // $ pizza-options --pizza-type=cheese20 // pizza details:21 // - cheese22 23-const commander = require('../');23+// const commander = require('commander'); // (normal include)24+const commander = require('../'); // include commander in git clone of commander repo25 const program = new commander.Command();26 27 program28 .option('-d, --debug', 'output extra debugging')9 unmodified linesmodified:examples/options-custom-processing.js+2 added-1 removed
15 unmodified lines16 // [ 'a', 'b', 'c' ]17 // $ custom --list x,y,z18 // [ 'x', 'y', 'z' ]19 20-const commander = require('../');20+// const commander = require('commander'); // (normal include)21+const commander = require('../'); // include commander in git clone of commander repo22 const program = new commander.Command();23 24 function myParseInt(value, dummyPrevious) {25 // parseInt takes a string and a radix29 unmodified linesmodified:examples/options-defaults.js+2 added-1 removed
9 unmodified lines10 // cheese: blue11 // $ pizza-options --cheese stilton12 // cheese: stilton13 14-const commander = require('../');14+// const commander = require('commander'); // (normal include)15+const commander = require('../'); // include commander in git clone of commander repo16 const program = new commander.Command();17 18 program19 .option('-c, --cheese <type>', 'Add the specified type of cheese', 'blue');4 unmodified linesmodified:examples/options-flag-or-value.js+2 added-1 removed
11 unmodified lines12 // add cheese13 // $ pizza-options --cheese mozzarella14 // add cheese type mozzarella15 16-const commander = require('../');16+// const commander = require('commander'); // (normal include)17+const commander = require('../'); // include commander in git clone of commander repo18 const program = new commander.Command();19 20 program21 .option('-c, --cheese [type]', 'Add cheese with optional type');6 unmodified linesmodified:examples/options-negatable.js+2 added-1 removed
13 unmodified lines14 // You ordered a pizza with sauce and blue cheese15 // $ pizza-options --no-sauce --no-cheese16 // You ordered a pizza with no sauce and no cheese17 18-const commander = require('../');18+// const commander = require('commander'); // (normal include)19+const commander = require('../'); // include commander in git clone of commander repo20 const program = new commander.Command();21 22 program23 .option('--no-sauce', 'Remove sauce')7 unmodified linesmodified:examples/options-required.js+2 added-1 removed
8 unmodified lines9 //10 // $ pizza11 // error: required option '-c, --cheese <type>' not specified12 13+// const commander = require('commander'); // (normal include)13-const commander = require('..'); // For running direct from git clone of commander repo14+const commander = require('../'); // include commander in git clone of commander repo15 const program = new commander.Command();16 17 program18 .requiredOption('-c, --cheese <type>', 'pizza must have cheese');2 unmodified linesmodified:examples/pizza+2 added-5 removed
1 #!/usr/bin/env node2 3-/**4- * Module dependencies.5- */3+// const program = require('commander'); // (normal include)4+const program = require('../'); // include commander in git clone of commander repo5 7-var program = require('../');8-6 program7 .version('0.0.1')8 .description('An application for pizzas ordering')9 .option('-p, --peppers', 'Add peppers')15 unmodified linesmodified:examples/pm+2 added-1 removed
1 #!/usr/bin/env node2 3-var program = require('..');3+// const program = require('commander'); // (normal include)4+const program = require('../'); // include commander in git clone of commander repo5 6 program7 .version('0.0.1')8 .description('Fake package manager')21 unmodified linesmodified:examples/pm-install+2 added-1 removed
1 #!/usr/bin/env node2 3-var program = require('..');3+// const program = require('commander'); // (normal include)4+const program = require('../'); // include commander in git clone of commander repo5 6 program7 .option('-f, --force', 'force installation')8 .parse(process.argv);14 unmodified linesmodified:examples/storeOptionsAsProperties-action.js+2 added-1 removed
14 unmodified lines15 // $ node storeOptionsAsProperties-action.js --name foo show --action jump16 // jump17 // foo18 19-const commander = require('../');19+// const commander = require('commander'); // (normal include)20+const commander = require('../'); // include commander in git clone of commander repo21 const program = new commander.Command();22 23 program24 .storeOptionsAsProperties(false) // <--- change behaviour17 unmodified linesmodified:examples/storeOptionsAsProperties-opts.js+2 added-1 removed
12 unmodified lines13 // $ node storeOptionsAsProperties-opts.js --name foo show --action jump14 // jump15 // foo16 17-const commander = require('../');17+// const commander = require('commander'); // (normal include)18+const commander = require('../'); // include commander in git clone of commander repo19 const program = new commander.Command();20 21 program22 .storeOptionsAsProperties(false); // <--- change behaviour17 unmodified linesmodified:examples/storeOptionsAsProperties-problem.js+2 added-1 removed
14 unmodified lines15 // $ node storeOptionsAsProperties-problem.js --name foo show --action jump16 // jump17 // foo18 19-const commander = require('../');19+// const commander = require('commander'); // (normal include)20+const commander = require('../'); // include commander in git clone of commander repo21 const program = new commander.Command();22 23 program24 .name('my-program-name')12 unmodified linesmodified:tests/command.action.test.js+16 added
111 unmodified lines112 program.parse(['node', 'test']);113 114 expect(actionMock).toHaveBeenCalledWith(undefined, program);115 });116+117+test('when action is async then can await parseAsync', async() => {118+ let asyncFinished = false;119+ async function delay() {120+ await new Promise(resolve => setTimeout(resolve, 100));121+ asyncFinished = true;122+ };123+ const program = new commander.Command();124+ program125+ .action(delay);126+127+ const later = program.parseAsync(['node', 'test']);128+ expect(asyncFinished).toBe(false);129+ await later;130+ expect(asyncFinished).toBe(true);131+});modified:typings/commander-tests.ts+6 added
131 unmodified lines132 });133 134 program.parse(process.argv);135 136+program.parseAsync(process.argv).then(() => {137+ console.log('parseAsync success');138+}).catch(err => {139+ console.log('parseAsync failed');140+});141+142 console.log('stuff');modified:typings/index.d.ts+11 added-2 removed
195 unmodified lines196 */197 allowUnknownOption(arg?: boolean): Command;198 199 /**200- * Parse `argv`, settings options and invoking commands when defined.200+ * Parse `argv`, setting options and invoking commands when defined.201 *202- * @returns {Command} for chaining202+ * @returns Command for chaining203 */204 parse(argv: string[]): Command;205 206 /**207+ * Parse `argv`, setting options and invoking commands when defined.208+ * 209+ * Use parseAsync instead of parse if any of your action handlers are async. Returns a Promise.210+ *211+ * @returns Promise212+ */213+ parseAsync(argv: string[]): Promise<any>;214+215+ /**216 * Parse options from `argv` returning `argv` void of these options.217 */218 parseOptions(argv: string[]): commander.ParseOptionsResult;219 92 unmodified linesmodified:CHANGELOG.md+8 added-2 removed
5 unmodified lines6 and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). (Format adopted after v3.0.0.)7 8 <!-- markdownlint-disable MD024 -->9 10-## [4.1.0] (date goes here)10+## [4.1.0] (2020-01-06)11 12 ### Added13 14-- two routines to change how option values are handled, and avoid name clashes with command properties ([#1102])14+- two routines to change how option values are handled, and eliminate name clashes with command properties ([#933] [#1102])15 - see storeOptionsAsProperties and passCommandToAction in README16+- `.parseAsync` to use instead of `.parse` if supply async action handlers ([#806] [#1118])17 18 ### Fixed19 20 - Remove trailing blanks from wrapped help text ([#1096])4 unmodified lines25 - extend security coverage for Commander 2.x to 2020-02-0326 - improvements to README27 - improvements to TypeScript definition documentation28 - move old versions out of main CHANGELOG29+- removed explicit use of `ts-node` in tests30 31 ## [4.0.1] (2019-11-12)32 33 ### Fixed350 unmodified lines384 [#599]: https://github.com/tj/commander.js/issues/599385 [#611]: https://github.com/tj/commander.js/issues/611386 [#697]: https://github.com/tj/commander.js/issues/697387 [#795]: https://github.com/tj/commander.js/issues/795388+[#806]: https://github.com/tj/commander.js/issues/806389 [#915]: https://github.com/tj/commander.js/issues/915390 [#938]: https://github.com/tj/commander.js/issues/938391 [#963]: https://github.com/tj/commander.js/issues/963392 [#974]: https://github.com/tj/commander.js/issues/974393 [#980]: https://github.com/tj/commander.js/issues/980394 [#987]: https://github.com/tj/commander.js/issues/987395 [#990]: https://github.com/tj/commander.js/issues/990396 [#991]: https://github.com/tj/commander.js/issues/991397+[#993]: https://github.com/tj/commander.js/issues/993398 [#999]: https://github.com/tj/commander.js/issues/999399 [#1010]: https://github.com/tj/commander.js/pull/1010400 [#1018]: https://github.com/tj/commander.js/pull/1018401 [#1026]: https://github.com/tj/commander.js/pull/102611 unmodified lines413 [#1081]: https://github.com/tj/commander.js/pull/1081414 [#1091]: https://github.com/tj/commander.js/pull/1091415 [#1096]: https://github.com/tj/commander.js/pull/1096416 [#1102]: https://github.com/tj/commander.js/pull/1102417+[#1118]: https://github.com/tj/commander.js/pull/1118418 419 [Unreleased]: https://github.com/tj/commander.js/compare/master...develop420+[4.1.0]: https://github.com/tj/commander.js/compare/v4.0.1..v4.1.0421 [4.0.1]: https://github.com/tj/commander.js/compare/v4.0.0..v4.0.1422 [4.0.0]: https://github.com/tj/commander.js/compare/v3.0.2..v4.0.0423 [4.0.0-1]: https://github.com/tj/commander.js/compare/v4.0.0-0..v4.0.0-1424 [4.0.0-0]: https://github.com/tj/commander.js/compare/v3.0.2...v4.0.0-04 unmodified linesmodified:index.js+39 added-17 removed
115 unmodified lines116 117 /**118 * Initialize a new `Command`.119 *120- * @param {String} name120+ * @param {String} [name]121 * @api public122 */123 124 function Command(name) {5 unmodified lines130 this._name = name || '';131 this._optionValues = {};132 this._storeOptionsAsProperties = true; // backwards compatible by default133 this._passCommandToAction = true; // backwards compatible by default134+ this._actionResults = [];135 136 this._helpFlags = '-h, --help';137 this._helpDescription = 'output usage information';138 this._helpShortFlag = '-h';191 unmodified lines330 331 var parsed = self.parseOptions(unknown);332 333 // Output help if necessary333- outputHelpIfNecessary(self, parsed.unknown);334+ outputHelpIfRequested(self, parsed.unknown);335 self._checkForMissingMandatoryOptions();336 337 // If there are still any unknown options, then we simply338 // die, unless someone asked for help, in which case we give it29 unmodified lines368 if (args.length > expectedArgsCount) {369 actionArgs.push(args.slice(expectedArgsCount));370 }371 371- fn.apply(self, actionArgs);372+ const actionResult = fn.apply(self, actionArgs);373+ // Remember result in case it is async. Assume parseAsync getting called on root.374+ let rootCommand = self;375+ while (rootCommand.parent) {376+ rootCommand = rootCommand.parent;377+ }378+ rootCommand._actionResults.push(actionResult);379 };380 var parent = this.parent || this;381 if (parent === this) {382 parent.on('program-command', listener);233 unmodified lines616 return this._optionValues[key];617 };618 619 /**613- * Parse `argv`, settings options and invoking commands when defined.620+ * Parse `argv`, setting options and invoking commands when defined.621 *622 * @param {Array} argv623 * @return {Command} for chaining624 * @api public76 unmodified lines701 return result;702 };703 704 /**705+ * Parse `argv`, setting options and invoking commands when defined.706+ *707+ * Use parseAsync instead of parse if any of your action handlers are async. Returns a Promise.708+ *709+ * @param {Array} argv710+ * @return {Promise}711+ * @api public712+ */713+Command.prototype.parseAsync = function(argv) {714+ this.parse(argv);715+ return Promise.all(this._actionResults);716+};717+718+/**719 * Execute a sub-command executable.720 *721 * @param {Array} argv722 * @param {Array} args723 * @param {Array} unknown703- * @param {String} specifySubcommand724+ * @param {String} executableFile725 * @api private726 */727 728 Command.prototype.executeSubCommand = function(argv, args, unknown, executableFile) {171 unmodified lines900 this.emit('program-command', args, unknown);901 this.emit('command:*', args, unknown);902 }903 } else {883- outputHelpIfNecessary(this, unknown);904+ outputHelpIfRequested(this, unknown);905+906 // If there were no args and we have unknown options,907 // then they are extraneous and we need to error.908 if (unknown.length > 0 && !this.defaultExecutable) {909 this.unknownOption(unknown[0]);44 unmodified lines954 * Parse options from `argv` returning `argv`955 * void of these options.956 *957 * @param {Array} argv936- * @return {Array}958+ * @return {{args: Array, unknown: Array}}959 * @api public960 */961 962 Command.prototype.parseOptions = function(argv) {103 unmodified lines1066 1067 /**1068 * `Option` is missing an argument, but received `flag` or nothing.1069 *1048- * @param {String} option1049- * @param {String} flag1070+ * @param {Option} option1071+ * @param {String} [flag]1072 * @api private1073 */1074 1075 Command.prototype.optionMissingArgument = function(option, flag) {9 unmodified lines1085 1086 /**1087 * `Option` does not have a value, and is a mandatory option.1088 *1067- * @param {String} option1089+ * @param {Option} option1090 * @api private1091 */1092 1093 Command.prototype.missingMandatoryOptionValue = function(option) {63 unmodified lines1157 /**1158 * Set the description to `str`.1159 *1160 * @param {String} str1139- * @param {Object} argsDescription1161+ * @param {Object} [argsDescription]1162 * @return {String|Command}1163 * @api public1164 */1165 28 unmodified lines1194 1195 /**1196 * Set / get the command usage `str`.1197 *1176- * @param {String} str1198+ * @param {String} [str]1199 * @return {String|Command}1200 * @api public1201 */1202 14 unmodified lines1217 1218 /**1219 * Get or set the name of the command1220 *1199- * @param {String} str1221+ * @param {String} [str]1222 * @return {String|Command}1223 * @api public1224 */1225 335 unmodified lines1561 return wrap(str, width, indent);1562 }1563 1564 /**1543- * Output help information if necessary1565+ * Output help information if help flags specified1566 *1545- * @param {Command} command to output help for1546- * @param {Array} array of options to search for -h or --help1567+ * @param {Command} cmd - command to output help for1568+ * @param {Array} options - array of options to search for -h or --help1569 * @api private1570 */1571 1550-function outputHelpIfNecessary(cmd, options) {1572+function outputHelpIfRequested(cmd, options) {1573 options = options || [];1574 1575 for (var i = 0; i < options.length; i++) {1576 if (options[i] === cmd._helpLongFlag || options[i] === cmd._helpShortFlag) {80 unmodified linesmodified:package-lock.json+1 added-1 removed
1 {2 "name": "commander",3- "version": "4.1.0-0",3+ "version": "4.1.0",4 "lockfileVersion": 1,5 "requires": true,6 "dependencies": {7 "@babel/code-frame": {6224 unmodified linesmodified:package.json+1 added-1 removed
1 {2 "name": "commander",3- "version": "4.1.0-0",3+ "version": "4.1.0",4 "description": "the complete solution for node.js command-line programs",5 "keywords": [6 "commander",7 "command",34 unmodified linesmodified:Readme_zh-CN.md+15 added-1 removed
31 unmodified lines32 - [自定义事件监听](#%e8%87%aa%e5%ae%9a%e4%b9%89%e4%ba%8b%e4%bb%b6%e7%9b%91%e5%90%ac)33 - [零碎知识](#%e9%9b%b6%e7%a2%8e%e7%9f%a5%e8%af%86)34 - [避免选项命名冲突](#避免选项命名冲突)35 - [TypeScript](#typescript)36- - [Node 选项例如 `--harmony`](#node-%e9%80%89%e9%a1%b9%e4%be%8b%e5%a6%82---harmony)36+ - [Node 选项例如 --harmony](#node-%e9%80%89%e9%a1%b9%e4%be%8b%e5%a6%82---harmony)37 - [Node 调试](#node-%e8%b0%83%e8%af%95)38 - [重载退出(exit)处理](#%e9%87%8d%e8%bd%bd%e9%80%80%e5%87%baexit%e5%a4%84%e7%90%86)39 - [例子](#%e4%be%8b%e5%ad%90)40 - [许可证](#%e8%ae%b8%e5%8f%af%e8%af%81)330 unmodified lines371 372 program.parse(process.argv)373 ```374 375+你可以自行实现一个`async`操作处理程序,同时调用`.parseAsync`代替`.parse`。376+377+```js378+async function run() { /* 在这里编写代码 */ }379+380+async function main() {381+ program382+ .command('run')383+ .action(run);384+ await program.parseAsync(process.argv);385+}386+```387+388+389 当一个命令在命令行上被使用时,它的选项必须是合法的。使用任何未知的选项会报错。然而如果一个基于操作的命令没有定义任何操作,那么这些选项是不合法的。390 391 定义配置选项可以随着调用 `.command()` 传递。392 为 `opts.noHelp` 指定 `true` 则该命令不会出现生成的帮助输出里。307 unmodified linesmodified:Readme.md+15 added-2 removed
9 unmodified lines10 Read this in other languages: English | [简体中文](./Readme_zh-CN.md)11 12 - [Commander.js](#commanderjs)13 - [Installation](#installation)14- - [Declaring _program_ variable](#declaring-program-variable)14+ - [Declaring program variable](#declaring-program-variable)15 - [Options](#options)16 - [Common option types, boolean and value](#common-option-types-boolean-and-value)17 - [Default option value](#default-option-value)18 - [Other option types, negatable boolean and flag|value](#other-option-types-negatable-boolean-and-flagvalue)13 unmodified lines32 - [Custom event listeners](#custom-event-listeners)33 - [Bits and pieces](#bits-and-pieces)34 - [Avoiding option name clashes](#avoiding-option-name-clashes)35 - [TypeScript](#typescript)36- - [Node options such as `--harmony`](#node-options-such-as---harmony)36+ - [Node options such as --harmony](#node-options-such-as---harmony)37 - [Node debugging](#node-debugging)38 - [Override exit handling](#override-exit-handling)39 - [Examples](#examples)40 - [License](#license)335 unmodified lines376 377 program.parse(process.argv)378 ```379 380+You may supply an `async` action handler, in which case you call `.parseAsync` rather than `.parse`.381+382+```js383+async function run() { /* code goes here */ }384+385+async function main() {386+ program387+ .command('run')388+ .action(run);389+ await program.parseAsync(process.argv);390+}391+```392+393 A command's options on the command line are validated when the command is used. Any unknown options will be reported as an error. However, if an action-based command does not define an action, then the options are not validated.394 395 Configuration options can be passed with the call to `.command()`. Specifying `true` for `opts.noHelp` will remove the command from the generated help output.396 317 unmodified lines