Merge branch 'develop' into release/7.x
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 · parents 9f5aabc, 2f4cd3d · Browse files · 7 files·+77 added−69 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
7 files+77 added−69 removed
- tests:+1 added−1 removed
- other:+76 added−68 removed
Provenance
- Commit
e5512a4- Tree
9aa905d- Author
John Gee <john@ruru.gen.nz>- Committer
John Gee <john@ruru.gen.nz>- Parent 1
9f5aabc- Parent 2
2f4cd3d
7 files changed+77 added−69 removed
modified:docs/options-taking-varying-arguments.md+3 added-22 removed
8 unmodified lines9 - [Alternative: Put options last](#alternative-put-options-last)10 - [Alternative: Use options instead of command-arguments](#alternative-use-options-instead-of-command-arguments)11 - [Combining short options, and options taking arguments](#combining-short-options-and-options-taking-arguments)12 - [Combining short options as if boolean](#combining-short-options-as-if-boolean)13- - [Terminology](#terminology)13 14 Certain options take a varying number of arguments:15 16 ```js4 unmodified lines21 ```22 23 This page uses examples with options taking 0 or 1 arguments, but the discussions also apply to variadic options taking more arguments.24 25+For information about terms used in this document see: [terminology](./terminology.md)26+27 ## Parsing ambiguity28 29 There is a potential downside to be aware of. If a command has both30 command-arguments and options with varying option-arguments, this introduces a parsing ambiguity which may affect the user of your program.109 unmodified lines140 $ cook -i -t scrambled141 technique: scrambled142 ingredient: cheese143 ```144+145 ## Combining short options, and options taking arguments146 147 Multiple boolean short options can be combined after a single `-`, like `ls -al`. You can also include just148 a single short option which might take a value, as any following characters will49 unmodified lines198 ```js199 .combineFlagAndOptionalValue(true) // `-v45` is treated like `--vegan=45`, this is the default behaviour200 .combineFlagAndOptionalValue(false) // `-vl` is treated like `-v -l`201 ```200-201-## Terminology202-203-_Work in progress: this section may move to the main README, or a page of its own._204-205-The command line arguments are made up of options, option-arguments, commands, and command-arguments.206-207-| Term | Explanation |208-| --- | --- |209-| option | an argument which is a `-` followed by a character, or `--` followed by a word (or hyphenated words), like `-s` or `--short` |210-| option-argument| some options can take an argument |211-| command | a program or command can have subcommands |212-| command-argument | argument for the command (and not an option or option-argument) |213-214-For example:215-216-```sh217-my-utility command -o --option option-argument command-argument-1 command-argument-2218-```219-220-In other references options are sometimes called flags, and command-arguments are sometimes called positional arguments or operands.added:docs/terminology.md+18 added
123456789101112131415161718# Terminology
The command line arguments are made up of options, option-arguments, commands, and command-arguments.
| Term | Explanation || --- | --- || option | an argument which is a `-` followed by a character, or `--` followed by a word (or hyphenated words), like `-s` or `--short` || option-argument| some options can take an argument || command | a program or command can have subcommands || command-argument | argument for the command (and not an option or option-argument) |
For example:
```shmy-utility command -o --option option-argument command-argument-1 command-argument-2```
In other references options are sometimes called flags, and command-arguments are sometimes called positional arguments or operands.added:examples/options-boolean-or-value.js+27 added
123456789101112131415161718192021222324252627#!/usr/bin/env node
// This is used as an example in the README for:// Other option types, flag|value// You can specify an option which functions as a flag but may also take a value (declared using square brackets).//// Example output pretending command called pizza-options (or try directly with `node options-flag-or-value.js`)//// $ pizza-options// no cheese// $ pizza-options --cheese// add cheese// $ pizza-options --cheese mozzarella// add cheese type mozzarella
// const commander = require('commander'); // (normal include)const commander = require('../'); // include commander in git clone of commander repoconst program = new commander.Command();
program .option('-c, --cheese [type]', 'Add cheese with optional type');
program.parse(process.argv);
if (program.cheese === undefined) console.log('no cheese');else if (program.cheese === true) console.log('add cheese');else console.log(`add cheese type ${program.cheese}`);deleted:examples/options-flag-or-value.js-27 removed
123456789101112131415161718192021222324252627#!/usr/bin/env node
// This is used as an example in the README for:// Other option types, flag|value// You can specify an option which functions as a flag but may also take a value (declared using square brackets).//// Example output pretending command called pizza-options (or try directly with `node options-flag-or-value.js`)//// $ pizza-options// no cheese// $ pizza-options --cheese// add cheese// $ pizza-options --cheese mozzarella// add cheese type mozzarella
// const commander = require('commander'); // (normal include)const commander = require('../'); // include commander in git clone of commander repoconst program = new commander.Command();
program .option('-c, --cheese [type]', 'Add cheese with optional type');
program.parse(process.argv);
if (program.cheese === undefined) console.log('no cheese');else if (program.cheese === true) console.log('add cheese');else console.log(`add cheese type ${program.cheese}`);modified:tests/command.help.test.js+1 added-1 removed
217 unmodified lines218 .arguments('<file>')219 .helpOption(false)220 .description('description', { file: 'input source' });221 const helpInformation = program.helpInformation();222- expect(helpInformation).toMatch(/Arguments:\n\n +file +input source/); // [sic], extra line222+ expect(helpInformation).toMatch(/Arguments:\n +file +input source/); // [sic], extra line223 });modified:index.js-1 removed
1704 unmodified lines1705 const width = this.padWidth();1706 const columns = process.stdout.columns || 80;1707 const descriptionWidth = columns - width - 5;1708 desc.push('Arguments:');1709- desc.push('');1709 this._args.forEach((arg) => {1710 desc.push(' ' + pad(arg.name, width) + ' ' + wrap(argsDescription[arg.name] || '', descriptionWidth, width + 4));1711 });1712 desc.push('');360 unmodified linesmodified:Readme.md+28 added-18 removed
13 unmodified lines14 - [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)18+ - [Other option types, negatable boolean and boolean|value](#other-option-types-negatable-boolean-and-booleanvalue)19 - [Extra option features](#extra-option-features)20 - [Custom option processing](#custom-option-processing)21 - [Required option](#required-option)22 - [Variadic option](#variadic-option)24 unmodified lines47 - [Examples](#examples)48 - [Support](#support)49 - [Commander for enterprise](#commander-for-enterprise)50 51+For information about terms used in this document see: [terminology](./docs/terminology.md)52+53 ## Installation54 55 ```bash56 npm install commander26 unmodified lines83 Multiple short flags may optionally be combined in a single argument following the dash: boolean flags, followed by a single option taking a value (possibly followed by the value).84 For example `-a -b -p 80` may be written as `-ab -p80` or even `-abp80`.85 86 You can use `--` to indicate the end of the options, and any remaining arguments will be used without being interpreted.85-This is particularly useful for passing options through to another86-command, like: `do -- git --version`.87 88-Options on the command line are not positional, and can be specified before or after other command arguments.88+Options on the command line are not positional, and can be specified before or after other arguments.89 90 ### Common option types, boolean and value91 92-The two most used option types are a boolean flag, and an option which takes a value (declared using angle brackets). Both are `undefined` unless specified on command line.92+The two most used option types are a boolean option, and an option which takes its value93+from the following argument (declared with angle brackets like `--expect <value>`). Both are `undefined` unless specified on command line. 94 95 Example file: [options-common.js](./examples/options-common.js)96 97 ```js49 unmodified lines147 $ pizza-options --cheese stilton148 cheese: stilton149 ```150 150-### Other option types, negatable boolean and flag|value151+### Other option types, negatable boolean and boolean|value152 152-You can specify a boolean option long name with a leading `no-` to set the option value to false when used.153+You can define a boolean option long name with a leading `no-` to set the option value to false when used.154 Defined alone this also makes the option true by default.155 156 If you define `--foo` first, adding `--no-foo` does not change the default value from what it would156-otherwise be. You can specify a default boolean value for a boolean flag and it can be overridden on command line.157+otherwise be. You can specify a default boolean value for a boolean option and it can be overridden on command line.158 159 Example file: [options-negatable.js](./examples/options-negatable.js)160 161 ```js18 unmodified lines180 $ pizza-options --no-sauce --no-cheese181 You ordered a pizza with no sauce and no cheese182 ```183 183-You can specify an option which functions as a flag but may also take a value (declared using square brackets).184+You can specify an option which may be used as a boolean option but may optionally take an option-argument185+(declared with square brackets like `--optional [value]`).186 185-Example file: [options-flag-or-value.js](./examples/options-flag-or-value.js)187+Example file: [options-boolean-or-value.js](./examples/options-boolean-or-value.js)188 189 ```js190 program191 .option('-c, --cheese [type]', 'Add cheese with optional type');13 unmodified lines205 $ pizza-options --cheese mozzarella206 add cheese type mozzarella207 ```208 209+For information about possible ambiguous cases, see [options taking varying arguments](./docs/options-taking-varying-arguments.md).210+211 ### Extra option features212 213 You can add most options using the `.option()` method, but there are some additional features available214 by constructing an `Option` explicitly for less common cases.21 unmodified lines236 ```237 238 ### Custom option processing239 236-You may specify a function to do custom processing of option values. The callback function receives two parameters, the user specified value and the237-previous value for the option. It returns the new value for the option.240+You may specify a function to do custom processing of option-arguments. The callback function receives two parameters,241+the user specified option-argument and the previous value for the option. It returns the new value for the option.242 239-This allows you to coerce the option value to the desired type, or accumulate values, or do entirely custom processing.243+This allows you to coerce the option-argument to the desired type, or accumulate values, or do entirely custom processing.244 241-You can optionally specify the default/starting value for the option after the function.245+You can optionally specify the default/starting value for the option after the function parameter.246 247 Example file: [options-custom-processing.js](./examples/options-custom-processing.js)248 249 ```js64 unmodified lines314 315 ### Variadic option316 317 You may make an option variadic by appending `...` to the value placeholder when declaring the option. On the command line you314-can then specify multiple option arguments, and the parsed option value will be an array. The extra arguments318+can then specify multiple option-arguments, and the parsed option value will be an array. The extra arguments319 are read until the first argument starting with a dash. The special argument `--` stops option processing entirely. If a value320 is specified in the same argument as the option then no further values are read.321 322 Example file: [options-variadic.js](./examples/options-variadic.js)20 unmodified lines343 Options: { number: [ '1', '2', '3' ], letter: true }344 Remaining arguments: [ 'operand' ]345 ```346 347+For information about possible ambiguous cases, see [options taking varying arguments](./docs/options-taking-varying-arguments.md).348+349 ### Version option350 351 The optional `version` method adds handling for displaying the command version. The default option flags are `-V` and `--version`, and when present the command prints the version number and exits.352 16 unmodified lines369 ## Commands370 371 You can specify (sub)commands using `.command()` or `.addCommand()`. There are two ways these can be implemented: using an action handler attached to the command, or as a stand-alone executable file (described in more detail later). The subcommands may be nested ([example](./examples/nestedCommands.js)).372 367-In the first parameter to `.command()` you specify the command name and any command arguments. The arguments may be `<required>` or `[optional]`, and the last argument may also be `variadic...`.373+In the first parameter to `.command()` you specify the command name and any command-arguments. The arguments may be `<required>` or `[optional]`, and the last argument may also be `variadic...`.374 375 You can use `.addCommand()` to add an already configured subcommand to the program.376 377 For example:19 unmodified lines397 program398 .addCommand(build.makeBuildCommand());399 ```400 395-Configuration options can be passed with the call to `.command()` and `.addCommand()`. Specifying `true` for `opts.hidden` will remove the command from the generated help output. Specifying `true` for `opts.isDefault` will run the subcommand if no other subcommand is specified ([example](./examples/defaultCommand.js)).401+Configuration options can be passed with the call to `.command()` and `.addCommand()`. Specifying `hidden: true` will 402+remove the command from the generated help output. Specifying `isDefault: true` will run the subcommand if no other403+subcommand is specified ([example](./examples/defaultCommand.js)).404 405 ### Specify the argument syntax406 399-You use `.arguments` to specify the arguments for the top-level command, and for subcommands they are usually included in the `.command` call. Angled brackets (e.g. `<required>`) indicate required input. Square brackets (e.g. `[optional]`) indicate optional input.407+You use `.arguments` to specify the expected command-arguments for the top-level command, and for subcommands they are usually408+included in the `.command` call. Angled brackets (e.g. `<required>`) indicate required command-arguments.409+Square brackets (e.g. `[optional]`) indicate optional command-arguments.410 You can optionally describe the arguments in the help by supplying a hash as second parameter to `.description()`.411 412 Example file: [env](./examples/env)413 422 unmodified lines