Add .name and .usage, TOC, and tidy README
Before adoption: this commit came in with the history the project adopted, so no landing records it.
- add .name and .usage - lint - add TOC (using Markdown All in One in Visual Studio Code) - put the help related topics below the main helop title
authored by John Gee · committed by John Gee · parent 3b0127b · Browse files · 1 file·+60 added−18 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+60 added−18 removed
- other:+60 added−18 removed
Provenance
- Commit
2c65463- Tree
8c559a2- Author
John Gee <john@ruru.gen.nz>- Committer
John Gee <john@ruru.gen.nz>- Parent
3b0127b
1 file changed+60 added−18 removed
modified:Readme.md+60 added-18 removed
5 unmodified lines6 [](https://packagephobia.now.sh/result?p=commander)7 8 The complete solution for [node.js](http://nodejs.org) command-line interfaces, inspired by Ruby's [commander](https://github.com/commander-rb/commander).9 10+- [Commander.js](#commanderjs)11+ - [Installation](#installation)12+ - [Declaring _program_ variable](#declaring-program-variable)13+ - [Options](#options)14+ - [Common option types, boolean and value](#common-option-types-boolean-and-value)15+ - [Default option value](#default-option-value)16+ - [Other option types, negatable boolean and flag|value](#other-option-types-negatable-boolean-and-flagvalue)17+ - [Custom option processing](#custom-option-processing)18+ - [Version option](#version-option)19+ - [Commands](#commands)20+ - [Specify the argument syntax](#specify-the-argument-syntax)21+ - [Action handler (sub)commands](#action-handler-subcommands)22+ - [Git-style executable (sub)commands](#git-style-executable-subcommands)23+ - [Automated --help](#automated---help)24+ - [Custom help](#custom-help)25+ - [.usage and .name](#usage-and-name)26+ - [.outputHelp(cb)](#outputhelpcb)27+ - [.helpOption(flags, description)](#helpoptionflags-description)28+ - [.help(cb)](#helpcb)29+ - [Custom event listeners](#custom-event-listeners)30+ - [Bits and pieces](#bits-and-pieces)31+ - [TypeScript](#typescript)32+ - [Node options such as `--harmony`](#node-options-such-as---harmony)33+ - [Node debugging](#node-debugging)34+ - [Examples](#examples)35+ - [License](#license)36+ - [Support](#support)37+38 ## Installation39 40+```bash12- $ npm install commander41+npm install commander42+```43 44 ## Declaring _program_ variable45 46 Commander exports a global object which is convenient for quick programs.11 unmodified lines58 const program = new commander.Command();59 program.version('0.0.1');60 ```61 32-62 ## Options63 64 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.65 81 unmodified lines147 console.log(`You ordered a pizza with ${sauceStr} and ${cheeseStr}`);148 ```149 150 ```bash122-$ pizza-options 151+$ pizza-options152 You ordered a pizza with sauce and mozzarella cheese153 $ pizza-options --sauce154 error: unknown option '--sauce'155 $ pizza-options --cheese=blue27 unmodified lines183 ```184 185 ### Custom option processing186 158-You may specify a function to do custom processing of option values. The callback function receives two parameters, the user specified value and the 187+You may specify a function to do custom processing of option values. The callback function receives two parameters, the user specified value and the188 previous value for the option. It returns the new value for the option.189 190 This allows you to coerce the option value to the desired type, or accumulate values, or do entirely custom processing.191 95 unmodified lines287 ### Specify the argument syntax288 289 You use `.arguments` to specify the arguments for the top-level command, and for subcommands they are included in the `.command` call. Angled brackets (e.g. `<required>`) indicate required input. Square brackets (e.g. `[optional]`) indicate optional input.290 262-291 ```js292 var program = require('commander');293 294 program89 unmodified lines384 ## Automated --help385 386 The help information is auto-generated based on the information commander already knows about your program, so the following `--help` info is for free:387 360-``` 388+```bash389 $ ./examples/pizza --help390 Usage: pizza [options]391 392 An application for pizzas ordering7 unmodified lines400 -C, --no-cheese You do not want any cheese401 -h, --help output usage information402 ```403 376-## Custom help404+### Custom help405 406 You can display arbitrary `-h, --help` information407 by listening for "--help". Commander will automatically408 exit once you are done so that the remainder of your program3 unmodified lines412 413 ```js414 #!/usr/bin/env node415 388-/**389- * Module dependencies.390- */391-416 var program = require('commander');417 418 program419 .version('0.1.0')17 unmodified lines437 ```438 439 Yields the following help output when `node script-name.js -h` or `node script-name.js --help` are run:440 417-```441+```Text442 Usage: custom-help [options]443 444 Options:445 -h, --help output usage information6 unmodified lines452 $ custom-help --help453 $ custom-help -h454 ```455 432-## .outputHelp(cb)456+### .usage and .name457 458+These allow you to customise the usage description in the first line of the help. The name is otherwise459+deduced from the (full) program arguments. Given:460+461+```js462+program463+ .name("my-command")464+ .usage("[global options] command")465+```466+467+The help will start with:468+469+```Text470+Usage: my-command [global options] command471+```472+473+### .outputHelp(cb)474+475 Output help information without exiting.476 Optional callback cb allows post-processing of help text before it is displayed.477 478 If you want to display help by default (e.g. if no command was provided), you can use something like:15 unmodified lines494 return colors.red(txt); //display the help text in red on the console495 }496 ```497 457-## .helpOption(flags, description)498+### .helpOption(flags, description)499 500 Override the default help flags and description.501 502 ```js503 program504 .helpOption('-e, --HELP', 'read more information');505 ```506 466-## .help(cb)507+### .help(cb)508 509 Output help information and exit immediately.510 Optional callback cb allows post-processing of help text before it is displayed.511 471-512 ## Custom event listeners513+514 You can execute custom actions by listening to command and option events.515 516 ```js517 program.on('option:verbose', function () {26 unmodified lines544 545 ### Node options such as `--harmony`546 547 You can enable `--harmony` option in two ways:507-* Use `#! /usr/bin/env node --harmony` in the sub-commands scripts. (Note Windows does not support this pattern.)508-* Use the `--harmony` option when call the command, like `node --harmony examples/pm publish`. The `--harmony` option will be preserved when spawning sub-command process.548 549+- Use `#! /usr/bin/env node --harmony` in the sub-commands scripts. (Note Windows does not support this pattern.)550+- Use the `--harmony` option when call the command, like `node --harmony examples/pm publish`. The `--harmony` option will be preserved when spawning sub-command process.551+552 ### Node debugging553 554 If you are using the node inspector for [debugging](https://nodejs.org/en/docs/guides/debugging-getting-started/) git-style executable (sub)commands using `node -inspect` et al,555 the inspector port is incremented by 1 for the spawned subcommand.57 unmodified lines