Skip to content
Myr

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

30 files+154 added−83 removed

modified:examples/custom-help+2 added-5 removed
1#!/usr/bin/env node23/**4 * Module dependencies.5 */3// const program = require('commander'); // (normal include)4const program = require('../'); // include commander in git clone of commander repo57var program = require('../');86program7 .version('0.0.1')8 .option('-f, --foo', 'enable some foo')9 .option('-b, --bar', 'enable some bar')
16 unmodified lines
modified:examples/custom-help-description+2 added-5 removed
1#!/usr/bin/env node23/**4 * Module dependencies.5 */3// const program = require('commander'); // (normal include)4const program = require('../'); // include commander in git clone of commander repo57var program = require('../');86program7 .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 lines
modified:examples/custom-version+2 added-5 removed
1#!/usr/bin/env node23/**4 * Module dependencies.5 */3// const program = require('commander'); // (normal include)4const program = require('../'); // include commander in git clone of commander repo57var program = require('../');86program7 .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 line
modified:examples/defaults+2 added-5 removed
1#!/usr/bin/env node23/**4 * Module dependencies.5 */3// const program = require('commander'); // (normal include)4const program = require('../'); // include commander in git clone of commander repo57var program = require('../');86function list(val) {7 return val.split(',').map(Number);8}9
10 unmodified lines
modified:examples/deploy+2 added-5 removed
1#!/usr/bin/env node23/**4 * Module dependencies.5 */3// const program = require('commander'); // (normal include)4const program = require('../'); // include commander in git clone of commander repo57var program = require('../');86program7 .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 lines
modified:examples/description+2 added-5 removed
1#!/usr/bin/env node23/**4 * Module dependencies.5 */3// const program = require('commander'); // (normal include)4const program = require('../'); // include commander in git clone of commander repo57var program = require('../');86program7 .version('0.0.1')8 .description('Application simple description')9 .option('-f, --foo', 'enable some foo')
5 unmodified lines
modified:examples/env+2 added-1 removed
1#!/usr/bin/env node23var program = require('../');3// const program = require('commander'); // (normal include)4const program = require('../'); // include commander in git clone of commander repo56let cmdValue;7let envValue;8
16 unmodified lines
modified:examples/express+2 added-5 removed
1#!/usr/bin/env node23/**4 * Module dependencies.5 */3// const program = require('commander'); // (normal include)4const program = require('../'); // include commander in git clone of commander repo57var program = require('../');86program7 .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 lines
modified:examples/help+2 added-5 removed
1#!/usr/bin/env node23/**4 * Module dependencies.5 */3// const program = require('commander'); // (normal include)4const program = require('../'); // include commander in git clone of commander repo57var program = require('../');86program7 .version('0.0.1')8 .option('-f, --foo', 'enable some foo')9 .option('-b, --bar', 'enable some bar')
4 unmodified lines
modified:examples/options-common.js+2 added-1 removed
18 unmodified lines
19// $ pizza-options --pizza-type=cheese20// pizza details:21// - cheese2223const commander = require('../');23// const commander = require('commander'); // (normal include)24const commander = require('../'); // include commander in git clone of commander repo25const program = new commander.Command();2627program28 .option('-d, --debug', 'output extra debugging')
9 unmodified lines
modified:examples/options-custom-processing.js+2 added-1 removed
15 unmodified lines
16// [ 'a', 'b', 'c' ]17// $ custom --list x,y,z18// [ 'x', 'y', 'z' ]1920const commander = require('../');20// const commander = require('commander'); // (normal include)21const commander = require('../'); // include commander in git clone of commander repo22const program = new commander.Command();2324function myParseInt(value, dummyPrevious) {25 // parseInt takes a string and a radix
29 unmodified lines
modified:examples/options-defaults.js+2 added-1 removed
9 unmodified lines
10// cheese: blue11// $ pizza-options --cheese stilton12// cheese: stilton1314const commander = require('../');14// const commander = require('commander'); // (normal include)15const commander = require('../'); // include commander in git clone of commander repo16const program = new commander.Command();1718program19 .option('-c, --cheese <type>', 'Add the specified type of cheese', 'blue');
4 unmodified lines
modified:examples/options-flag-or-value.js+2 added-1 removed
11 unmodified lines
12// add cheese13// $ pizza-options --cheese mozzarella14// add cheese type mozzarella1516const commander = require('../');16// const commander = require('commander'); // (normal include)17const commander = require('../'); // include commander in git clone of commander repo18const program = new commander.Command();1920program21 .option('-c, --cheese [type]', 'Add cheese with optional type');
6 unmodified lines
modified:examples/options-negatable.js+2 added-1 removed
13 unmodified lines
14// You ordered a pizza with sauce and blue cheese15// $ pizza-options --no-sauce --no-cheese16// You ordered a pizza with no sauce and no cheese1718const commander = require('../');18// const commander = require('commander'); // (normal include)19const commander = require('../'); // include commander in git clone of commander repo20const program = new commander.Command();2122program23 .option('--no-sauce', 'Remove sauce')
7 unmodified lines
modified:examples/options-required.js+2 added-1 removed
8 unmodified lines
9//10// $ pizza11// error: required option '-c, --cheese <type>' not specified1213// const commander = require('commander'); // (normal include)13const commander = require('..'); // For running direct from git clone of commander repo14const commander = require('../'); // include commander in git clone of commander repo15const program = new commander.Command();1617program18 .requiredOption('-c, --cheese <type>', 'pizza must have cheese');
2 unmodified lines
modified:examples/pizza+2 added-5 removed
1#!/usr/bin/env node23/**4 * Module dependencies.5 */3// const program = require('commander'); // (normal include)4const program = require('../'); // include commander in git clone of commander repo57var program = require('../');86program7 .version('0.0.1')8 .description('An application for pizzas ordering')9 .option('-p, --peppers', 'Add peppers')
15 unmodified lines
modified:examples/pm+2 added-1 removed
1#!/usr/bin/env node23var program = require('..');3// const program = require('commander'); // (normal include)4const program = require('../'); // include commander in git clone of commander repo56program7 .version('0.0.1')8 .description('Fake package manager')
21 unmodified lines
modified:examples/pm-install+2 added-1 removed
1#!/usr/bin/env node23var program = require('..');3// const program = require('commander'); // (normal include)4const program = require('../'); // include commander in git clone of commander repo56program7 .option('-f, --force', 'force installation')8 .parse(process.argv);
14 unmodified lines
modified:examples/storeOptionsAsProperties-action.js+2 added-1 removed
14 unmodified lines
15// $ node storeOptionsAsProperties-action.js --name foo show --action jump16// jump17// foo1819const commander = require('../');19// const commander = require('commander'); // (normal include)20const commander = require('../'); // include commander in git clone of commander repo21const program = new commander.Command();2223program24 .storeOptionsAsProperties(false) // <--- change behaviour
17 unmodified lines
modified:examples/storeOptionsAsProperties-opts.js+2 added-1 removed
12 unmodified lines
13// $ node storeOptionsAsProperties-opts.js --name foo show --action jump14// jump15// foo1617const commander = require('../');17// const commander = require('commander'); // (normal include)18const commander = require('../'); // include commander in git clone of commander repo19const program = new commander.Command();2021program22 .storeOptionsAsProperties(false); // <--- change behaviour
17 unmodified lines
modified:examples/storeOptionsAsProperties-problem.js+2 added-1 removed
14 unmodified lines
15// $ node storeOptionsAsProperties-problem.js --name foo show --action jump16// jump17// foo1819const commander = require('../');19// const commander = require('commander'); // (normal include)20const commander = require('../'); // include commander in git clone of commander repo21const program = new commander.Command();2223program24 .name('my-program-name')
12 unmodified lines
modified:tests/command.action.test.js+16 added
111 unmodified lines
112 program.parse(['node', 'test']);113114 expect(actionMock).toHaveBeenCalledWith(undefined, program);115});116117test('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);126127 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 lines
132});133134program.parse(process.argv);135136program.parseAsync(process.argv).then(() => {137 console.log('parseAsync success');138}).catch(err => {139 console.log('parseAsync failed');140});141142console.log('stuff');
modified:typings/index.d.ts+11 added-2 removed
195 unmodified lines
196 */197 allowUnknownOption(arg?: boolean): Command;198199 /**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;205206 /**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>;214215 /**216 * Parse options from `argv` returning `argv` void of these options.217 */218 parseOptions(argv: string[]): commander.ParseOptionsResult;219
92 unmodified lines
modified:CHANGELOG.md+8 added-2 removed
5 unmodified lines
6and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). (Format adopted after v3.0.0.)78<!-- markdownlint-disable MD024 -->910## [4.1.0] (date goes here)10## [4.1.0] (2020-01-06)1112### Added1314- 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])1718### Fixed1920- Remove trailing blanks from wrapped help text ([#1096])
4 unmodified lines
25- 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 tests3031## [4.0.1] (2019-11-12)3233### Fixed
350 unmodified lines
384[#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/1026
11 unmodified lines
413[#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/1118418419[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-0
4 unmodified lines
modified:index.js+39 added-17 removed
115 unmodified lines
116117/**118 * Initialize a new `Command`.119 *120 * @param {String} name120 * @param {String} [name]121 * @api public122 */123124function Command(name) {
5 unmodified lines
130 this._name = name || '';131 this._optionValues = {};132 this._storeOptionsAsProperties = true; // backwards compatible by default133 this._passCommandToAction = true; // backwards compatible by default134 this._actionResults = [];135136 this._helpFlags = '-h, --help';137 this._helpDescription = 'output usage information';138 this._helpShortFlag = '-h';
191 unmodified lines
330331 var parsed = self.parseOptions(unknown);332333 // Output help if necessary333 outputHelpIfNecessary(self, parsed.unknown);334 outputHelpIfRequested(self, parsed.unknown);335 self._checkForMissingMandatoryOptions();336337 // If there are still any unknown options, then we simply338 // die, unless someone asked for help, in which case we give it
29 unmodified lines
368 if (args.length > expectedArgsCount) {369 actionArgs.push(args.slice(expectedArgsCount));370 }371371 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 lines
616 return this._optionValues[key];617};618619/**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 public
76 unmodified lines
701 return result;702};703704/**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 */713Command.prototype.parseAsync = function(argv) {714 this.parse(argv);715 return Promise.all(this._actionResults);716};717718/**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 */727728Command.prototype.executeSubCommand = function(argv, args, unknown, executableFile) {
171 unmodified lines
900 this.emit('program-command', args, unknown);901 this.emit('command:*', args, unknown);902 }903 } else {883 outputHelpIfNecessary(this, unknown);904 outputHelpIfRequested(this, unknown);905906 // 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 lines
954 * 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 */961962Command.prototype.parseOptions = function(argv) {
103 unmodified lines
10661067/**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 */10741075Command.prototype.optionMissingArgument = function(option, flag) {
9 unmodified lines
10851086/**1087 * `Option` does not have a value, and is a mandatory option.1088 *1067 * @param {String} option1089 * @param {Option} option1090 * @api private1091 */10921093Command.prototype.missingMandatoryOptionValue = function(option) {
63 unmodified lines
1157/**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 lines
11941195/**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 lines
12171218/**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 lines
1561 return wrap(str, width, indent);1562}15631564/**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 */15711550function outputHelpIfNecessary(cmd, options) {1572function outputHelpIfRequested(cmd, options) {1573 options = options || [];15741575 for (var i = 0; i < options.length; i++) {1576 if (options[i] === cmd._helpLongFlag || options[i] === cmd._helpShortFlag) {
80 unmodified lines
modified: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 lines
modified: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 lines
modified:Readme_zh-CN.md+15 added-1 removed
31 unmodified lines
32 - [自定义事件监听](#%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 lines
371372program.parse(process.argv)373```374375你可以自行实现一个`async`操作处理程序,同时调用`.parseAsync`代替`.parse`。376377```js378async function run() { /* 在这里编写代码 */ }379380async function main() {381 program382 .command('run')383 .action(run);384 await program.parseAsync(process.argv);385}386```387388389当一个命令在命令行上被使用时,它的选项必须是合法的。使用任何未知的选项会报错。然而如果一个基于操作的命令没有定义任何操作,那么这些选项是不合法的。390391定义配置选项可以随着调用 `.command()` 传递。392为 `opts.noHelp` 指定 `true` 则该命令不会出现生成的帮助输出里。
307 unmodified lines
modified:Readme.md+15 added-2 removed
9 unmodified lines
10Read this in other languages: English | [简体中文](./Readme_zh-CN.md)1112- [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 lines
32 - [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 lines
376377program.parse(process.argv)378```379380You may supply an `async` action handler, in which case you call `.parseAsync` rather than `.parse`.381382```js383async function run() { /* code goes here */ }384385async function main() {386 program387 .command('run')388 .action(run);389 await program.parseAsync(process.argv);390}391```392393A 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.394395Configuration 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