Skip to content
Myr

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

1 file+60 added−18 removed

modified:Readme.md+60 added-18 removed
5 unmodified lines
6[![Install Size](https://packagephobia.now.sh/badge?p=commander)](https://packagephobia.now.sh/result?p=commander)78The complete solution for [node.js](http://nodejs.org) command-line interfaces, inspired by Ruby's [commander](https://github.com/commander-rb/commander).910- [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)3738## Installation3940```bash12 $ npm install commander41npm install commander42```4344## Declaring _program_ variable4546Commander exports a global object which is convenient for quick programs.
11 unmodified lines
58 const program = new commander.Command();59 program.version('0.0.1');60 ```613262## Options6364Options 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 lines
147console.log(`You ordered a pizza with ${sauceStr} and ${cheeseStr}`);148```149150```bash122$ pizza-options 151$ pizza-options152You ordered a pizza with sauce and mozzarella cheese153$ pizza-options --sauce154error: unknown option '--sauce'155$ pizza-options --cheese=blue
27 unmodified lines
183```184185### Custom option processing186158You may specify a function to do custom processing of option values. The callback function receives two parameters, the user specified value and the 187You may specify a function to do custom processing of option values. The callback function receives two parameters, the user specified value and the188previous value for the option. It returns the new value for the option.189190This allows you to coerce the option value to the desired type, or accumulate values, or do entirely custom processing.191
95 unmodified lines
287### Specify the argument syntax288289You 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.290262291```js292var program = require('commander');293294program
89 unmodified lines
384## Automated --help385386 The help information is auto-generated based on the information commander already knows about your program, so the following `--help` info is for free:387360``` 388```bash389$ ./examples/pizza --help390Usage: pizza [options]391392An application for pizzas ordering
7 unmodified lines
400 -C, --no-cheese You do not want any cheese401 -h, --help output usage information402```403376## Custom help404### Custom help405406 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 program
3 unmodified lines
412413```js414#!/usr/bin/env node415388/**389 * Module dependencies.390 */391416var program = require('commander');417418program419 .version('0.1.0')
17 unmodified lines
437```438439Yields the following help output when `node script-name.js -h` or `node script-name.js --help` are run:440417```441```Text442Usage: custom-help [options]443444Options:445 -h, --help output usage information
6 unmodified lines
452 $ custom-help --help453 $ custom-help -h454```455432## .outputHelp(cb)456### .usage and .name457458These allow you to customise the usage description in the first line of the help. The name is otherwise459deduced from the (full) program arguments. Given:460461```js462program463 .name("my-command")464 .usage("[global options] command")465```466467The help will start with:468469```Text470Usage: my-command [global options] command471```472473### .outputHelp(cb)474475Output help information without exiting.476Optional callback cb allows post-processing of help text before it is displayed.477478If you want to display help by default (e.g. if no command was provided), you can use something like:
15 unmodified lines
494 return colors.red(txt); //display the help text in red on the console495}496```497457## .helpOption(flags, description)498### .helpOption(flags, description)499500 Override the default help flags and description.501502```js503program504 .helpOption('-e, --HELP', 'read more information');505```506466## .help(cb)507### .help(cb)508509 Output help information and exit immediately.510 Optional callback cb allows post-processing of help text before it is displayed.511471512## Custom event listeners513514 You can execute custom actions by listening to command and option events.515516```js517program.on('option:verbose', function () {
26 unmodified lines
544545### Node options such as `--harmony`546547You 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.548549- 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.551552### Node debugging553554If 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,555the inspector port is incremented by 1 for the spawned subcommand.
57 unmodified lines