Skip to content
Myr

Use revised terminology (#1362)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by John Gee · committed by GitHub · parent e56fba2 · Browse files · 3 files+49 added−45 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+49 added−45 removed

  • other:+49 added−45 removed

3 files+49 added−45 removed

added:examples/options-boolean-or-value.js+27 added
deleted:examples/options-flag-or-value.js-27 removed
modified:Readme.md+22 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 - [Custom option processing](#custom-option-processing)20 - [Required option](#required-option)21 - [Variadic option](#variadic-option)22 - [Version option](#version-option)
58 unmodified lines
81Multiple 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).82For example `-a -b -p 80` may be written as `-ab -p80` or even `-abp80`.8384You 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`.8588Options on the command line are not positional, and can be specified before or after other command arguments.86Options on the command line are not positional, and can be specified before or after other arguments.8788### Common option types, boolean and value8992The 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.90The two most used option types are a boolean option, and an option which takes its value91from the following argument (declared with angle brackets like `--expect <value>`). Both are `undefined` unless specified on command line. 9293Example file: [options-common.js](./examples/options-common.js)9495```js
49 unmodified lines
145$ pizza-options --cheese stilton146cheese: stilton147```148150### Other option types, negatable boolean and flag|value149### Other option types, negatable boolean and boolean|value150152You can specify a boolean option long name with a leading `no-` to set the option value to false when used.151You can define a boolean option long name with a leading `no-` to set the option value to false when used.152Defined alone this also makes the option true by default.153154If 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.155otherwise be. You can specify a default boolean value for a boolean option and it can be overridden on command line.156157Example file: [options-negatable.js](./examples/options-negatable.js)158159```js
18 unmodified lines
178$ pizza-options --no-sauce --no-cheese179You ordered a pizza with no sauce and no cheese180```181183You can specify an option which functions as a flag but may also take a value (declared using square brackets).182You can specify an option which may be used as a boolean option but may optionally take an option-argument183(declared with square brackets like `--optional [value]`).184185Example file: [options-flag-or-value.js](./examples/options-flag-or-value.js)185Example file: [options-boolean-or-value.js](./examples/options-boolean-or-value.js)186187```js188program189 .option('-c, --cheese [type]', 'Add cheese with optional type');
17 unmodified lines
207For information about possible ambiguous cases, see [options taking varying arguments](./docs/options-taking-varying-arguments.md).208209### Custom option processing210211You may specify a function to do custom processing of option values. The callback function receives two parameters, the user specified value and the212previous value for the option. It returns the new value for the option.211You may specify a function to do custom processing of option-arguments. The callback function receives two parameters,212the user specified option-argument and the previous value for the option. It returns the new value for the option.213214This allows you to coerce the option value to the desired type, or accumulate values, or do entirely custom processing.214This allows you to coerce the option-argument to the desired type, or accumulate values, or do entirely custom processing.215216You can optionally specify the default/starting value for the option after the function.216You can optionally specify the default/starting value for the option after the function parameter.217218Example file: [options-custom-processing.js](./examples/options-custom-processing.js)219220```js
64 unmodified lines
285286### Variadic option287288You may make an option variadic by appending `...` to the value placeholder when declaring the option. On the command line you289can then specify multiple option arguments, and the parsed option value will be an array. The extra arguments289can then specify multiple option-arguments, and the parsed option value will be an array. The extra arguments290are read until the first argument starting with a dash. The special argument `--` stops option processing entirely. If a value291is specified in the same argument as the option then no further values are read.292293Example file: [options-variadic.js](./examples/options-variadic.js)
46 unmodified lines
340## Commands341342You 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)).343344In 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...`.344In 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...`.345346You can use `.addCommand()` to add an already configured subcommand to the program.347348For example:
19 unmodified lines
368program369 .addCommand(build.makeBuildCommand());370```371372Configuration 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)).372Configuration options can be passed with the call to `.command()` and `.addCommand()`. Specifying `hidden: true` will 373remove the command from the generated help output. Specifying `isDefault: true` will run the subcommand if no other374subcommand is specified ([example](./examples/defaultCommand.js)).375376### Specify the argument syntax377376You 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.378You use `.arguments` to specify the expected command-arguments for the top-level command, and for subcommands they are usually379included in the `.command` call. Angled brackets (e.g. `<required>`) indicate required command-arguments.380Square brackets (e.g. `[optional]`) indicate optional command-arguments.381You can optionally describe the arguments in the help by supplying a hash as second parameter to `.description()`.382383Example file: [env](./examples/env)384
407 unmodified lines