docs(README): Tweak formatting, punctuation for clarity (#2465)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* docs(README): Tweak formatting, punctuation for clarity * Update Readme.md Co-authored-by: John Gee <john@ruru.gen.nz> * Update Readme.md Co-authored-by: John Gee <john@ruru.gen.nz> * Update Readme.md Co-authored-by: John Gee <john@ruru.gen.nz> * Update Readme.md Co-authored-by: John Gee <john@ruru.gen.nz> * Update Readme.md Co-authored-by: John Gee <john@ruru.gen.nz> * docs(README): Rewrites requested by @shadowspawn * docs(README): One more rewrite, @shadowspawn ;-) * Update Readme.md Co-authored-by: John Gee <john@ruru.gen.nz> * Update Readme.md Co-authored-by: John Gee <john@ruru.gen.nz> * Update Readme.md Co-authored-by: John Gee <john@ruru.gen.nz> ---------
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- Co-authored-by: John Gee <john@ruru.gen.nz>
Diff stats
1 file+78 added−64 removed
- other:+78 added−64 removed
Provenance
- Commit
9211918- Tree
d589880- Author
Zearin <Zearin@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
4208a96
1 file changed+78 added−64 removed
modified:Readme.md+78 added-64 removed
152 unmodified lines153 // CommonJS (.cjs)154 const { program } = require('commander');155 ```156 157-For larger programs which may use commander in multiple ways, including unit testing, it is better to create a local Command object to use.157+For larger programs which may use commander in multiple ways, including unit testing, it is better to create a local `Command` object to use.158 159 ```js160 // CommonJS (.cjs)161 const { Command } = require('commander');13 unmodified lines175 ```176 177 ## Options178 179-Options are defined with the `.option()` method, also serving as documentation for the options. Each option can have a short flag (single character) and a long name, separated by a comma or space or vertical bar ('|'). To allow a wider range of short-ish flags than just180-single characters, you may also have two long options. Examples:179+Options are defined with the `.option()` method, also serving as documentation for the options. Each option can have a short flag (single character) and a long name, separated by a comma, a space, or a vertical bar (`|`). To allow a wider range of short-ish flags than just single characters, you may also have two long options.180 181 ```js182 program183 .option('-p, --port <number>', 'server port number')2 unmodified lines186 ```187 188 The parsed options can be accessed by calling `.opts()` on a `Command` object, and are passed to the action handler.189 191-Multi-word options such as "--template-engine" are camel-cased, becoming `program.opts().templateEngine` etc.190+Multi-word options like `--template-engine` are normalized to camelCase option names, resulting in properties such as `program.opts().templateEngine`.191 193-An option and its option-argument can be separated by a space, or combined into the same argument. The option-argument can follow the short option directly or follow an `=` for a long option.192+An option and its option-argument can be separated by a space, or combined into the same argument. The option-argument can follow the short option directly, or follow an `=` for a long option.193 194 ```sh195 serve -p 80196 serve -p8046 unmodified lines243 - cheese244 ```245 246 Multiple boolean short options may be combined following the dash, and may be followed by a single short option taking a value.248-For example `-d -s -p cheese` may be written as `-ds -p cheese` or even `-dsp cheese`.247+For example, `-d -s -p cheese` may be written as `-ds -p cheese` or even `-dsp cheese`.248 249 Options with an expected option-argument are greedy and will consume the following argument whatever the value.250 So `--id -xyz` reads `-xyz` as the option-argument.251 22 unmodified lines274 ```275 276 ### Other option types, negatable boolean and boolean|value277 279-You can define a boolean option long name with a leading `no-` to set the option value to false when used.280-Defined alone this also makes the option true by default.278+You can define a boolean option long name with a leading `no-` to set the option value to `false` when used.279+Defined alone, this also makes the option `true` by default.280 281 If you define `--foo` first, adding `--no-foo` does not change the default value from what it would282 otherwise be.283 23 unmodified lines307 You ordered a pizza with no sauce and no cheese308 ```309 310 You can specify an option which may be used as a boolean option but may optionally take an option-argument312-(declared with square brackets like `--optional [value]`).311+(declared with square brackets, like `--optional [value]`).312 313 Example file: [options-boolean-or-value.js](./examples/options-boolean-or-value.js)314 315 ```js24 unmodified lines340 For information about possible ambiguous cases, see [options taking varying arguments](./docs/options-in-depth.md).341 342 ### Required option343 345-You may specify a required (mandatory) option using `.requiredOption()`. The option must have a value after parsing, usually specified on the command line, or perhaps from a default value (say from environment). The method is otherwise the same as `.option()` in format, taking flags and description, and optional default value or custom processing.344+You may specify a required (mandatory) option using `.requiredOption()`. The option must have a value after parsing, usually specified on the command line, or perhaps from a default value (e.g., from environment). 345 346+The method is otherwise the same as `.option()` in format, taking flags and description, and optional default value or custom processing.347+348 Example file: [options-required.js](./examples/options-required.js)349 350 ```js351 program11 unmodified lines363 364 You may make an option variadic by appending `...` to the value placeholder when declaring the option. On the command line you365 can then specify multiple option-arguments, and the parsed option value will be an array. The extra arguments366 are read until the first argument starting with a dash. The special argument `--` stops option processing entirely. If a value366-is specified in the same argument as the option then no further values are read.367+is specified in the same argument as the option, then no further values are read.368 369 Example file: [options-variadic.js](./examples/options-variadic.js)370 371 ```js22 unmodified lines394 For information about possible ambiguous cases, see [options taking varying arguments](./docs/options-in-depth.md).395 396 ### Version option397 397-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.398+The optional `.version()` method adds handling for displaying the command version. The default option flags are `-V` and `--version`. When used, the command prints the version number and exits.399 400 ```js401 program.version('0.0.1');402 ```2 unmodified lines405 $ ./examples/pizza -V406 0.0.1407 ```408 408-You may change the flags and description by passing additional parameters to the `version` method, using409-the same syntax for flags as the `option` method.409+You may change the flags and description by passing additional parameters to the `.version()` method, using410+the same syntax for flags as the `.option()` method.411 412 ```js413 program.version('0.0.1', '-v, --vers', 'output the current version');414 ```38 unmodified lines453 $ extra --disable-server --port 8000454 error: option '--disable-server' cannot be used with option '-p, --port <number>'455 ```456 456-Specify a required (mandatory) option using the `Option` method `.makeOptionMandatory()`. This matches the `Command` method [.requiredOption()](#required-option).457+Specify a required (mandatory) option using the `Option` method `.makeOptionMandatory()`. This matches the `Command` method [`.requiredOption()`](#required-option).458 459 ### Custom option processing460 461 You may specify a function to do custom processing of option-arguments. The callback function receives two parameters,59 unmodified lines521 ```522 523 ## Commands524 524-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)).525+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. (More detail about this later.)526 527+Subcommands may be nested. Example file: [nestedCommands.js](./examples/nestedCommands.js).528+529 In the first parameter to `.command()` you specify the command name. You may append the command-arguments after the command name, or specify them separately using `.argument()`. The arguments may be `<required>` or `[optional]`, and the last argument may also be `variadic...`.530 531 You can use `.addCommand()` to add an already configured subcommand to the program.532 22 unmodified lines555 ```556 557 Configuration options can be passed with the call to `.command()` and `.addCommand()`. Specifying `hidden: true` will558 remove the command from the generated help output. Specifying `isDefault: true` will run the subcommand if no other556-subcommand is specified ([example](./examples/defaultCommand.js)).559+subcommand is specified. (Example file: [defaultCommand.js](./examples/defaultCommand.js).)560 558-You can add alternative names for a command with `.alias()`. ([example](./examples/alias.js))561+You can add alternative names for a command with `.alias()`. (Example file: [alias.js](./examples/alias.js).)562 560-`.command()` automatically copies the inherited settings from the parent command to the newly created subcommand. This is only done during creation, any later setting changes to the parent are not inherited.563+`.command()` automatically copies the inherited settings from the parent command to the newly created subcommand. This is only done during creation; any later setting changes to the parent are not inherited.564 565 For safety, `.addCommand()` does not automatically copy the inherited settings from the parent command. There is a helper routine `.copyInheritedSettings()` for copying the settings when they are wanted.566 567 ### Command-arguments568 569 For subcommands, you can specify the argument syntax in the call to `.command()` (as shown above). This567-is the only method usable for subcommands implemented using a stand-alone executable, but for other subcommands568-you can instead use the following method.570+is the only method usable for subcommands implemented using a stand-alone executable.571 570-To configure a command, you can use `.argument()` to specify each expected command-argument.572+Alternatively, you can instead use the following method. To configure a command, you can use `.argument()` to specify each expected command-argument.573 You supply the argument name and an optional description. The argument may be `<required>` or `[optional]`.574 You can specify a default value for an optional command-argument.575 576 Example file: [argument.js](./examples/argument.js)8 unmodified lines585 console.log('password:', password);586 });587 ```588 587- The last argument of a command can be variadic, and only the last argument. To make an argument variadic you588- append `...` to the argument name. A variadic argument is passed to the action handler as an array. For example:589+The last argument of a command can be variadic, and _only_ the last argument. To make an argument variadic, simply590+append `...` to the argument name.591 592+A variadic argument is passed to the action handler as an array.593+594 ```js595 program596 .version('0.1.0')597 .command('rmdir')67 unmodified lines665 console.log(`Thank-you ${title}${name}`);666 });667 ```668 665-If you prefer, you can work with the command directly and skip declaring the parameters for the action handler. The `this` keyword is set to the running command and can be used from a function expression (but not from an arrow function).669+If you prefer, you can work with the command directly and skip declaring the parameters for the action handler. If you use a function expression (but not an arrow function), the `this` keyword is set to the running command.670 671 Example file: [action-this.js](./examples/action-this.js)672 673 ```js5 unmodified lines679 console.error('Run script %s on port %s', this.args[0], this.opts().port);680 });681 ```682 679-You may supply an `async` action handler, in which case you call `.parseAsync` rather than `.parse`.683+You may supply an `async` action handler, in which case you call `.parseAsync()` rather than `.parse()`.684 685 ```js686 async function run() { /* code goes here */ }687 4 unmodified lines692 await program.parseAsync(process.argv);693 }694 ```695 692-A command's options and arguments on the command line are validated when the command is used. Any unknown options or missing arguments or excess arguments will be reported as an error. You can suppress the unknown option check with `.allowUnknownOption()`. You can suppress the excess arguments check with `.allowExcessArguments()`.696+A command's options and arguments on the command line are validated when the command is used. Any unknown options, missing arguments, or excess arguments will be reported as an error. 697 698+You can suppress the unknown option check with `.allowUnknownOption()`. You can suppress the excess arguments check with `.allowExcessArguments()`.699+700 ### Stand-alone executable (sub)commands701 702 When `.command()` is invoked with a description argument, this tells Commander that you're going to use stand-alone executables for subcommands.697-Commander will search the files in the directory of the entry script for a file with the name combination `command-subcommand`, like `pm-install` or `pm-search` in the example below. The search includes trying common file extensions, like `.js`.703+Commander will search the files in the directory of the entry script for a file with the name combination `command-subcommand` (like `pm-install` or `pm-search` in the example below). The search includes trying common file extensions, like `.js`.704 You may specify a custom name (and path) with the `executableFile` configuration option.705 You may specify a custom search directory for subcommands with `.executableDir()`.706 707 You handle the options for an executable (sub)command in the executable, and don't declare them at the top-level.31 unmodified lines739 }740 });741 ```742 737-The callback hook can be `async`, in which case you call `.parseAsync` rather than `.parse`. You can add multiple hooks per event.743+The callback hook can be `async`, in which case you call `.parseAsync()` rather than `.parse()`. You can add multiple hooks per event.744 745 The supported events are:746 747 | event name | when hook called | callback parameters |748 | :-- | :-- | :-- |749 | `preAction`, `postAction` | before/after action handler for this command and its nested subcommands | `(thisCommand, actionCommand)` |750 | `preSubcommand` | before parsing direct subcommand | `(thisCommand, subcommand)` |751 746-For an overview of the life cycle events see [parsing life cycle and hooks](./docs/parsing-and-hooks.md).752+For an overview of the life cycle events, see [parsing life cycle and hooks](./docs/parsing-and-hooks.md).753 754 ## Automated help755 756 The help information is auto-generated based on the information commander already knows about your program. The default66 unmodified lines823 The positions "beforeAll" and "afterAll" apply to the command and all its subcommands.824 825 The second parameter can be a string, or a function returning a string. The function is passed a context object for your convenience. The properties are:826 821-- error: a boolean for whether the help is being displayed due to a usage error822-- command: the Command which is displaying the help827+- `error`: a boolean for whether the help is being displayed due to a usage error828+- `command`: the `Command` which is displaying the help829 830 ### Display help after errors831 832 The default behaviour for usage errors is to just display a short error message.35 unmodified lines868 ### .name869 870 The command name appears in the help, and is also used for locating stand-alone executable subcommands.871 866-You may specify the program name using `.name()` or in the Command constructor. For the program, Commander will872+You may specify the program name using `.name()` or in the `Command` constructor. For the program, Commander will873 fall back to using the script name from the full arguments passed into `.parse()`. However, the script name varies874 depending on how your program is launched, so you may wish to specify it explicitly.875 876 ```js1 unmodified line878 const pm = new Command('pm');879 ```880 881 Subcommands get a name when specified using `.command()`. If you create the subcommand yourself to use with `.addCommand()`,876-then set the name using `.name()` or in the Command constructor.882+then set the name using `.name()` or in the `Command` constructor.883 884 ### .usage885 886 This allows you to customise the usage description in the first line of the help. Given:25 unmodified lines912 ```913 914 ### .helpOption(flags, description)915 910-By default, every command has a help option. You may change the default help flags and description. Pass false to disable the built-in help option.916+By default, every command has a help option. You may change the default help flags and description. Pass `false` to disable the built-in help option.917 918 ```js919 program920 .helpOption('-e, --HELP', 'read more information');921 ```922 917-(Or use `.addHelpOption()` to add an option you construct yourself.)923+Alternatively, use `.addHelpOption()` to add an option you construct yourself.924 925 ### .helpCommand()926 921-A help command is added by default if your command has subcommands. You can explicitly turn on or off the implicit help command with `.helpCommand(true)` and `.helpCommand(false)`.927+A help command is added by default if your command has subcommands. You can explicitly turn the implicit help command on or off with `.helpCommand(true)` and `.helpCommand(false)`.928 929 You can both turn on and customise the help command by supplying the name and description:930 931 ```js932 program.helpCommand('assist [command]', 'show assistance');933 ```934 929-(Or use `.addHelpCommand()` to add a command you construct yourself.)935+Alternatively, use `.addHelpCommand()` to add a command you construct yourself.936 937 ### Help Groups938 939 The help by default lists options under the the heading `Options:` and commands under `Commands:`. You can create your own groups934-with different headings. The high-level way is to set the desired group heading while adding the options and commands,935-using `.optionsGroup()` and `.commandsGroup()`. The low-level way is using `.helpGroup()` on an individual `Option` or `Command`940+with different headings. 941 942+- The high-level way is to set the desired group heading while adding the options and commands, using `.optionsGroup()` and `.commandsGroup()`. 943+- The low-level way is using `.helpGroup()` on an individual `Option` or `Command`.944+945 Example file: [help-groups.js](./examples/help-groups.js)946 947 ### More configuration948 941-The built-in help is formatted using the Help class.942-You can configure the help by modifying data properties and methods using `.configureHelp()`, or by subclassing Help using `.createHelp()` .949+The built-in help is formatted using the `Help` class.950+You can configure the help by modifying data properties and methods using `.configureHelp()`, or by subclassing `Help` using `.createHelp()` .951 944-Simple properties include `sortSubcommands`, `sortOptions`, and `showGlobalOptions`. You can add color using the style methods like `styleTitle()`.952+Simple properties include `sortSubcommands`, `sortOptions`, and `showGlobalOptions`. You can add color using the style methods like `.styleTitle()`.953 946-For more detail and examples of changing the displayed text, color, and layout see [help in depth](./docs/help-in-depth.md).954+For more detail and examples of changing the displayed text, color, and layout, see: [help in depth](./docs/help-in-depth.md).955 956 ## Custom event listeners957 950-You can execute custom actions by listening to command and option events.958+You can execute custom actions by listening to `command` and `option` events.959 960 ```js961 program.on('option:verbose', function () {962 process.env.VERBOSE = this.opts().verbose;5 unmodified lines968 ### .parse() and .parseAsync()969 970 Call with no parameters to parse `process.argv`. Detects Electron and special node options like `node --eval`. Easy mode!971 964-Or call with an array of strings to parse, and optionally where the user arguments start by specifying where the arguments are `from`:972+Or, call with an array of strings to parse, and optionally where the user arguments start by specifying where the arguments are `from`:973 974 - `'node'`: default, `argv[0]` is the application and `argv[1]` is the script being run, with user arguments after that975 - `'electron'`: `argv[0]` is the application and `argv[1]` varies depending on whether the electron application is packaged976 - `'user'`: just user arguments5 unmodified lines982 program.parse(process.argv); // assume argv[0] is app and argv[1] is script983 program.parse(['--port', '80'], { from: 'user' }); // just user supplied arguments, nothing special about argv[0]984 ```985 978-Use parseAsync instead of parse if any of your action handlers are async.986+Use `.parseAsync()` instead of `.parse()` if any of your action handlers are async.987 988 ### Parsing Configuration989 990 If the default parsing does not suit your needs, there are some behaviours to support other usage patterns.26 unmodified lines1017 1018 By default, the option processing shows an error for an unknown option. To have an unknown option treated as an ordinary command-argument and continue looking for options, use `.allowUnknownOption()`. This lets you mix known and unknown options.1019 1020 By default, the argument processing displays an error for more command-arguments than expected.1013-To suppress the error for excess arguments, use`.allowExcessArguments()`.1021+To suppress the error for excess arguments, use `.allowExcessArguments()`.1022 1023 ### Legacy options as properties1024 1025 Before Commander 7, the option values were stored as properties on the command.1026 This was convenient to code, but the downside was possible clashes with1019-existing properties of `Command`. You can revert to the old behaviour to run unmodified legacy code by using `.storeOptionsAsProperties()`.1027+existing properties of `Command`. 1028 1029+You can revert to the old behaviour to run unmodified legacy code by using `.storeOptionsAsProperties()`.1030+1031 ```js1032 program1033 .storeOptionsAsProperties()1034 .option('-d, --debug')5 unmodified lines1040 ```1041 1042 ### TypeScript1043 1034-extra-typings: There is an optional project to infer extra type information from the option and argument definitions.1044+**extra-typings:** There is an optional project to infer extra type information from the option and argument definitions.1045 This adds strong typing to the options returned by `.opts()` and the parameters to `.action()`.1036-See [commander-js/extra-typings](https://github.com/commander-js/extra-typings) for more.1046+For more, see the repo: [commander-js/extra-typings](https://github.com/commander-js/extra-typings).1047 1038-```1048+```ts1049 import { Command } from '@commander-js/extra-typings';1050 ```1051 1042-ts-node: If you use `ts-node` and stand-alone executable subcommands written as `.ts` files, you need to call your program through node to get the subcommands called correctly. e.g.1052+**ts-node:** If you use `ts-node` and stand-alone executable subcommands written as `.ts` files, you need to call your program through node to get the subcommands called correctly, e.g.:1053 1054 ```sh1055 node -r ts-node/register pm.ts1056 ```6 unmodified lines1063 const { createCommand } = require('commander');1064 const program = createCommand();1065 ```1066 1057-`createCommand` is also a method of the Command object, and creates a new command rather than a subcommand. This gets used internally1067+`createCommand()` is also a method of the `Command` object, and creates a new command rather than a subcommand. This gets used internally1068 when creating subcommands using `.command()`, and you may override it to1059-customise the new subcommand (example file [custom-command-class.js](./examples/custom-command-class.js)).1069+customise the new subcommand. Example file: [custom-command-class.js](./examples/custom-command-class.js).1070 1071 ### Node options such as `--harmony`1072 1073 You can enable `--harmony` option in two ways:1074 1065-- Use `#! /usr/bin/env node --harmony` in the subcommands scripts. (Note Windows does not support this pattern.)1075+- Use `#! /usr/bin/env node --harmony` in the subcommands scripts. (Note: Windows does not support this pattern.)1076 - Use the `--harmony` option when calling the command, like `node --harmony examples/pm publish`. The `--harmony` option will be preserved when spawning subcommand processes.1077 1078 ### Debugging stand-alone executable subcommands1079 1 unmodified line1081 1082 If you are using the node inspector for [debugging](https://nodejs.org/en/docs/guides/debugging-getting-started/) executable subcommands using `node --inspect` et al.,1083 the inspector port is incremented by 1 for the spawned subcommand.1084 1075-If you are using VSCode to debug executable subcommands you need to set the `"autoAttachChildProcesses": true` flag in your launch.json configuration.1085+If you are using VSCode to debug executable subcommands you need to set the `"autoAttachChildProcesses": true` flag in your `launch.json` configuration.1086 1087 ### npm run-script1088 1079-By default, when you call your program using run-script, `npm` will parse any options on the command-line and they will not reach your program. Use1089+By default, when you call your program using `run-script`, `npm` will parse any options on the command-line and they will not reach your program. Use1090 `--` to stop the npm option parsing and pass through all the arguments.1091 1082- The synopsis for [npm run-script](https://docs.npmjs.com/cli/v9/commands/npm-run-script) explicitly shows the `--` for this reason:1092+ The synopsis for [`npm run-script`](https://docs.npmjs.com/cli/v9/commands/npm-run-script) explicitly shows the `--` for this reason:1093 1094 ```console1095 npm run-script <command> [-- <args>]1096 ```1 unmodified line1098 ### Display error1099 1100 This routine is available to invoke the Commander error handling for your own error conditions. (See also the next section about exit handling.)1101 1092-As well as the error message, you can optionally specify the `exitCode` (used with `process.exit`)1102+As well as the error message, you can optionally specify the `exitCode` (used with `process.exit()`)1103 and `code` (used with `CommanderError`).1104 1105 ```js1106 program.error('Password must be longer than four characters');1 unmodified line1108 ```1109 1110 ### Override exit and output handling1111 1102-By default, Commander calls `process.exit` when it detects errors, or after displaying the help or version. You can override1112+By default, Commander calls `process.exit()` when it detects errors, or after displaying the help or version. You can override1113 this behaviour and optionally supply a callback. The default override throws a `CommanderError`.1114 1105-The override callback is passed a `CommanderError` with properties `exitCode` number, `code` string, and `message`.1106-Commander expects the callback to terminate the normal program flow, and will call `process.exit` if the callback returns.1107-The normal display of error messages or version or help is not affected by the override which is called after the display.1115+The override callback is passed a `CommanderError` with the properties:1116+- `exitCode`: number1117+- `code`: string 1118+- `message`: string1119 1120+Commander expects the callback to terminate the normal program flow, and will call `process.exit()` if the callback returns.1121+The normal display of error messages or version or help is not affected by the override, which is called after the display.1122+1123 ```js1124 program.exitOverride();1125 1126 try {47 unmodified lines