Skip to content
Myr

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

7 files+77 added−69 removed

modified:docs/options-taking-varying-arguments.md+3 added-22 removed
8 unmodified lines
9 - [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)1314Certain options take a varying number of arguments:1516```js
4 unmodified lines
21```2223This page uses examples with options taking 0 or 1 arguments, but the discussions also apply to variadic options taking more arguments.2425For information about terms used in this document see: [terminology](./terminology.md)2627## Parsing ambiguity2829There is a potential downside to be aware of. If a command has both30command-arguments and options with varying option-arguments, this introduces a parsing ambiguity which may affect the user of your program.
109 unmodified lines
140$ cook -i -t scrambled141technique: scrambled142ingredient: cheese143```144145## Combining short options, and options taking arguments146147Multiple boolean short options can be combined after a single `-`, like `ls -al`. You can also include just148a single short option which might take a value, as any following characters will
49 unmodified lines
198```js199.combineFlagAndOptionalValue(true) // `-v45` is treated like `--vegan=45`, this is the default behaviour200.combineFlagAndOptionalValue(false) // `-vl` is treated like `-v -l`201```200201## Terminology202203_Work in progress: this section may move to the main README, or a page of its own._204205The command line arguments are made up of options, option-arguments, commands, and command-arguments.206207| 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) |213214For example:215216```sh217my-utility command -o --option option-argument command-argument-1 command-argument-2218```219220In other references options are sometimes called flags, and command-arguments are sometimes called positional arguments or operands.
added:docs/terminology.md+18 added
added:examples/options-boolean-or-value.js+27 added
deleted:examples/options-flag-or-value.js-27 removed
modified:tests/command.help.test.js+1 added-1 removed
217 unmodified lines
218 .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 lines
1705 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 lines
modified:Readme.md+28 added-18 removed
13 unmodified lines
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)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 lines
47 - [Examples](#examples)48 - [Support](#support)49 - [Commander for enterprise](#commander-for-enterprise)5051For information about terms used in this document see: [terminology](./docs/terminology.md)5253## Installation5455```bash56npm install commander
26 unmodified lines
83Multiple 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).84For example `-a -b -p 80` may be written as `-ab -p80` or even `-abp80`.8586You can use `--` to indicate the end of the options, and any remaining arguments will be used without being interpreted.85This is particularly useful for passing options through to another86command, like: `do -- git --version`.8788Options on the command line are not positional, and can be specified before or after other command arguments.88Options on the command line are not positional, and can be specified before or after other arguments.8990### Common option types, boolean and value9192The 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.92The two most used option types are a boolean option, and an option which takes its value93from the following argument (declared with angle brackets like `--expect <value>`). Both are `undefined` unless specified on command line. 9495Example file: [options-common.js](./examples/options-common.js)9697```js
49 unmodified lines
147$ pizza-options --cheese stilton148cheese: stilton149```150150### Other option types, negatable boolean and flag|value151### Other option types, negatable boolean and boolean|value152152You can specify a boolean option long name with a leading `no-` to set the option value to false when used.153You can define a boolean option long name with a leading `no-` to set the option value to false when used.154Defined alone this also makes the option true by default.155156If you define `--foo` first, adding `--no-foo` does not change the default value from what it would156otherwise be. You can specify a default boolean value for a boolean flag and it can be overridden on command line.157otherwise be. You can specify a default boolean value for a boolean option and it can be overridden on command line.158159Example file: [options-negatable.js](./examples/options-negatable.js)160161```js
18 unmodified lines
180$ pizza-options --no-sauce --no-cheese181You ordered a pizza with no sauce and no cheese182```183183You can specify an option which functions as a flag but may also take a value (declared using square brackets).184You 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]`).186185Example file: [options-flag-or-value.js](./examples/options-flag-or-value.js)187Example file: [options-boolean-or-value.js](./examples/options-boolean-or-value.js)188189```js190program191 .option('-c, --cheese [type]', 'Add cheese with optional type');
13 unmodified lines
205$ pizza-options --cheese mozzarella206add cheese type mozzarella207```208209For information about possible ambiguous cases, see [options taking varying arguments](./docs/options-taking-varying-arguments.md).210211### Extra option features212213You can add most options using the `.option()` method, but there are some additional features available214by constructing an `Option` explicitly for less common cases.
21 unmodified lines
236```237238### Custom option processing239236You may specify a function to do custom processing of option values. The callback function receives two parameters, the user specified value and the237previous value for the option. It returns the new value for the option.240You may specify a function to do custom processing of option-arguments. The callback function receives two parameters,241the user specified option-argument and the previous value for the option. It returns the new value for the option.242239This allows you to coerce the option value to the desired type, or accumulate values, or do entirely custom processing.243This allows you to coerce the option-argument to the desired type, or accumulate values, or do entirely custom processing.244241You can optionally specify the default/starting value for the option after the function.245You can optionally specify the default/starting value for the option after the function parameter.246247Example file: [options-custom-processing.js](./examples/options-custom-processing.js)248249```js
64 unmodified lines
314315### Variadic option316317You may make an option variadic by appending `...` to the value placeholder when declaring the option. On the command line you314can then specify multiple option arguments, and the parsed option value will be an array. The extra arguments318can then specify multiple option-arguments, and the parsed option value will be an array. The extra arguments319are read until the first argument starting with a dash. The special argument `--` stops option processing entirely. If a value320is specified in the same argument as the option then no further values are read.321322Example file: [options-variadic.js](./examples/options-variadic.js)
20 unmodified lines
343Options: { number: [ '1', '2', '3' ], letter: true }344Remaining arguments: [ 'operand' ]345```346347For information about possible ambiguous cases, see [options taking varying arguments](./docs/options-taking-varying-arguments.md).348349### Version option350351The 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 lines
369## Commands370371You 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)).372367In 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...`.373In 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...`.374375You can use `.addCommand()` to add an already configured subcommand to the program.376377For example:
19 unmodified lines
397program398 .addCommand(build.makeBuildCommand());399```400395Configuration 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)).401Configuration options can be passed with the call to `.command()` and `.addCommand()`. Specifying `hidden: true` will 402remove the command from the generated help output. Specifying `isDefault: true` will run the subcommand if no other403subcommand is specified ([example](./examples/defaultCommand.js)).404405### Specify the argument syntax406399You 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.407You use `.arguments` to specify the expected command-arguments for the top-level command, and for subcommands they are usually408included in the `.command` call. Angled brackets (e.g. `<required>`) indicate required command-arguments.409Square brackets (e.g. `[optional]`) indicate optional command-arguments.410You can optionally describe the arguments in the help by supplying a hash as second parameter to `.description()`.411412Example file: [env](./examples/env)413
422 unmodified lines