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
Provenance
- Commit
a094ef2- Tree
f3e1f3a- Author
John Gee <john@ruru.gen.nz>- Committer
GitHub <noreply@github.com>- Parent
5f725f6
6 files changed+38 added−72 removed
modified:examples/custom-help+3 added-10 removed
2 unmodified lines3 // const program = require('commander'); // (normal include)4 const program = require('../'); // include commander in git clone of commander repo5 6 program7- .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');8 9 // must be before .parse()13-program.on('--help', function() {10+program.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 });15 16 program.parse(process.argv);22-23-program.help();modified:examples/pizza-2 removed
5 unmodified lines6 program7 .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 linesmodified:typings/commander-tests.ts+4 added
150 unmodified linesExpand all151152153154155156157158159150 unmodified linesExpand all}).catch(err => { console.log('parseAsync failed');});
program.help();program.outputHelp();const info = program.helpInformation();
console.log('stuff');modified:typings/index.d.ts+5 added
279 unmodified lines280 */281 outputHelp(cb?: (str: string) => string): void;282 283 /**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 linesmodified:index.js+1 added-1 removed
1321 unmodified lines1322 /**1323 * Return program help documentation.1324 *1325 * @return {String}1326- * @api private1326+ * @api public1327 */1328 1329 helpInformation() {1330 let desc = [];287 unmodified linesmodified:Readme.md+25 added-59 removed
24 unmodified lines25 - [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 lines436 If the program is designed to be installed globally, make sure the executables have proper modes, like `755`.437 438 ## Automated help439 439- The help information is auto-generated based on the information commander already knows about your program. The default440- help option is `-h,--help`.440+The help information is auto-generated based on the information commander already knows about your program. The default441+help option is `-h,--help`. ([example](./examples/pizza))442 443 ```bash443-$ ./examples/pizza --help444+$ node ./examples/pizza --help445 Usage: pizza [options]446 447 An application for pizzas ordering448 449 Options: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 lines466 ```467 468 ### Custom help469 471- You can display extra `-h, --help` information472- by listening for "--help". Commander will automatically473- exit after displaying the help.470+You can display extra information by listening for "--help". ([example](./examples/custom-help))471 472 ```js476-#!/usr/bin/env node477-478-const program = require('commander');479-473 program481- .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');475 476 // must be before .parse()487-program.on('--help', function(){488- console.log('')489- console.log('Examples:');477+program.on('--help', () => {478+ console.log('');479+ console.log('Example call:');480 console.log(' $ custom-help --help');491- console.log(' $ custom-help -h');481 });493-494-program.parse(process.argv);495-496-console.log('stuff');482 ```483 499-Yields the following help output when `node script-name.js -h` or `node script-name.js --help` are run:484+Yields the following help output:485 486 ```Text487 Usage: custom-help [options]488 489 Options: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 command492 511-Examples:493+Example call:494 $ custom-help --help513- $ custom-help -h495 ```496 497 ### .usage and .name498 11 unmodified lines510 ```Text511 Usage: my-command [global options] command512 ```513 514+### .help(cb)515+516+Output help information and exit immediately. Optional callback cb allows post-processing of help text before it is displayed.517+518 ### .outputHelp(cb)519 520 Output help information without exiting.521 Optional callback cb allows post-processing of help text before it is displayed.522 538-If you want to display help by default (e.g. if no command was provided), you can use something like:523+### .helpInformation()524 540-```js541-const program = require('commander');542-const colors = require('colors');525+Get the command help information as a string for processing or displaying yourself. (The text does not include the custom help526+from `--help` listeners.)527 544-program545- .version('0.1.0')546- .command('getstream [url]', 'get stream URL')547- .parse(process.argv);548-549-if (!process.argv.slice(2).length) {550- program.outputHelp(make_red);551-}552-553-function make_red(txt) {554- return colors.red(txt); //display the help text in red on the console555-}556-```557-528 ### .helpOption(flags, description)529 560- Override the default help flags and description.530+Override the default help flags and description.531 532 ```js533 program534 .helpOption('-e, --HELP', 'read more information');8 unmodified lines543 ```js544 program.addHelpCommand('assist [command]', 'show assistance');545 ```546 577-### .help(cb)578-579- Output help information and exit immediately.580- Optional callback cb allows post-processing of help text before it is displayed.581-547 ## Custom event listeners548 584- You can execute custom actions by listening to command and option events.549+You can execute custom actions by listening to command and option events.550 551 ```js552 program.on('option:verbose', function () {553 process.env.VERBOSE = this.verbose;22 unmodified lines576 - `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,578 or just the options (specify false)579 580+([example](./examples/storeOptionsAsProperties-action.js))581+582 ```js616-// file: ./examples/storeOptionsAsProperties.action.js583 program584 .storeOptionsAsProperties(false)585 .passCommandToAction(false);586 118 unmodified lines