Skip to content
Myr

Make .helpInformation public and update help sections in README (#1169)

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

* Make example shorter * Focus on point of example, shorten code * Make .helpInformation public * Update mostly help sections - add .helpInformation - shorten related examples - remove callback example - add links to example code * Add simple calls to help function to typings test

authored by John Gee · committed by GitHub · parent 5f725f6 · Browse files · 6 files+38 added−72 removed
This commit

State

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

Diff stats

6 files+38 added−72 removed

  • other:+38 added−72 removed

6 files+38 added−72 removed

modified:examples/custom-help+3 added-10 removed
2 unmodified lines
3// const program = require('commander'); // (normal include)4const program = require('../'); // include commander in git clone of commander repo56program7 .version('0.0.1')8 .option('-f, --foo', 'enable some foo')7 .option('-f, --foo', 'enable some foo');9 .option('-b, --bar', 'enable some bar')10 .option('-B, --baz', 'enable some baz');89// must be before .parse()13program.on('--help', function() {10program.on('--help', () => {11 console.log('');15 console.log('Examples:');16 console.log('');12 console.log('Example call:');13 console.log(' $ custom-help --help');18 console.log(' $ custom-help -h');14});1516program.parse(process.argv);2223program.help();
modified:examples/pizza-2 removed
5 unmodified lines
6program7 .version('0.0.1')8 .description('An application for pizzas ordering')9 .option('-p, --peppers', 'Add peppers')10 .option('-P, --pineapple', 'Add pineapple')11 .option('-b, --bbq', 'Add bbq sauce')10 .option('-c, --cheese <type>', 'Add the specified type of cheese', 'marble')11 .option('-C, --no-cheese', 'You do not want any cheese')12 .parse(process.argv);13
9 unmodified lines
modified:typings/commander-tests.ts+4 added
modified:typings/index.d.ts+5 added
279 unmodified lines
280 */281 outputHelp(cb?: (str: string) => string): void;282283 /**284 * Return command help documentation.285 */286 helpInformation(): string;287 288 /**289 * You can pass in flags and a description to override the help290 * flags and help description for your command.291 */292 helpOption(flags?: string, description?: string): Command;
41 unmodified lines
modified:index.js+1 added-1 removed
1321 unmodified lines
1322 /**1323 * Return program help documentation.1324 *1325 * @return {String}1326 * @api private1326 * @api public1327 */13281329 helpInformation() {1330 let desc = [];
287 unmodified lines
modified:Readme.md+25 added-59 removed
24 unmodified lines
25 - [Git-style executable (sub)commands](#git-style-executable-subcommands)26 - [Automated help](#automated-help)27 - [Custom help](#custom-help)28 - [.usage and .name](#usage-and-name)29 - [.help(cb)](#helpcb)30 - [.outputHelp(cb)](#outputhelpcb)31 - [.helpInformation()](#helpinformation)32 - [.helpOption(flags, description)](#helpoptionflags-description)33 - [.addHelpCommand()](#addhelpcommand)32 - [.help(cb)](#helpcb)34 - [Custom event listeners](#custom-event-listeners)35 - [Bits and pieces](#bits-and-pieces)36 - [Avoiding option name clashes](#avoiding-option-name-clashes)37 - [TypeScript](#typescript)
398 unmodified lines
436If the program is designed to be installed globally, make sure the executables have proper modes, like `755`.437438## Automated help439439 The help information is auto-generated based on the information commander already knows about your program. The default440 help option is `-h,--help`.440The help information is auto-generated based on the information commander already knows about your program. The default441help option is `-h,--help`. ([example](./examples/pizza))442443```bash443$ ./examples/pizza --help444$ node ./examples/pizza --help445Usage: pizza [options]446447An application for pizzas ordering448449Options:450 -V, --version output the version number451 -p, --peppers Add peppers451 -P, --pineapple Add pineapple452 -b, --bbq Add bbq sauce452 -c, --cheese <type> Add the specified type of cheese (default: "marble")453 -C, --no-cheese You do not want any cheese454 -h, --help display help for command455```
10 unmodified lines
466```467468### Custom help469471 You can display extra `-h, --help` information472 by listening for "--help". Commander will automatically473 exit after displaying the help.470You can display extra information by listening for "--help". ([example](./examples/custom-help))471472```js476#!/usr/bin/env node477478const program = require('commander');479473program481 .version('0.1.0')482 .option('-f, --foo', 'enable some foo')474 .option('-f, --foo', 'enable some foo');483 .option('-b, --bar', 'enable some bar')484 .option('-B, --baz', 'enable some baz');475476// must be before .parse()487program.on('--help', function(){488 console.log('')489 console.log('Examples:');477program.on('--help', () => {478 console.log('');479 console.log('Example call:');480 console.log(' $ custom-help --help');491 console.log(' $ custom-help -h');481});493494program.parse(process.argv);495496console.log('stuff');482```483499Yields the following help output when `node script-name.js -h` or `node script-name.js --help` are run:484Yields the following help output:485486```Text487Usage: custom-help [options]488489Options:505 -V, --version output the version number506 -f, --foo enable some foo507 -b, --bar enable some bar508 -B, --baz enable some baz509 -h, --help display help for command490 -f, --foo enable some foo491 -h, --help display help for command492511Examples:493Example call:494 $ custom-help --help513 $ custom-help -h495```496497### .usage and .name498
11 unmodified lines
510```Text511Usage: my-command [global options] command512```513514### .help(cb)515516Output help information and exit immediately. Optional callback cb allows post-processing of help text before it is displayed.517518### .outputHelp(cb)519520Output help information without exiting.521Optional callback cb allows post-processing of help text before it is displayed.522538If you want to display help by default (e.g. if no command was provided), you can use something like:523### .helpInformation()524540```js541const program = require('commander');542const colors = require('colors');525Get the command help information as a string for processing or displaying yourself. (The text does not include the custom help526from `--help` listeners.)527544program545 .version('0.1.0')546 .command('getstream [url]', 'get stream URL')547 .parse(process.argv);548549if (!process.argv.slice(2).length) {550 program.outputHelp(make_red);551}552553function make_red(txt) {554 return colors.red(txt); //display the help text in red on the console555}556```557528### .helpOption(flags, description)529560 Override the default help flags and description.530Override the default help flags and description.531532```js533program534 .helpOption('-e, --HELP', 'read more information');
8 unmodified lines
543```js544program.addHelpCommand('assist [command]', 'show assistance');545```546577### .help(cb)578579 Output help information and exit immediately.580 Optional callback cb allows post-processing of help text before it is displayed.581547## Custom event listeners548584 You can execute custom actions by listening to command and option events.549You can execute custom actions by listening to command and option events.550551```js552program.on('option:verbose', function () {553 process.env.VERBOSE = this.verbose;
22 unmodified lines
576- `storeOptionsAsProperties`: whether to store option values as properties on command object, or store separately (specify false) and access using `.opts()`577- `passCommandToAction`: whether to pass command to action handler,578or just the options (specify false)579580([example](./examples/storeOptionsAsProperties-action.js))581582```js616// file: ./examples/storeOptionsAsProperties.action.js583program584 .storeOptionsAsProperties(false)585 .passCommandToAction(false);586
118 unmodified lines