Skip to content
Myr

Use a tidier global command (#1200)

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

authored by John Gee · committed by GitHub · parent 0a50bd6 · Browse files · 1 file+14 added−14 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+14 added−14 removed

  • other:+14 added−14 removed

1 file+14 added−14 removed

modified:Readme.md+14 added-14 removed
54 unmodified lines
55Commander exports a global object which is convenient for quick programs.56This is used in the examples in this README for brevity.5758```js59const program = require('commander').program;59const { program } = require('commander');60program.version('0.0.1');61```6263For larger programs which may use commander in multiple ways, including unit testing, it is better to create a local Command object to use.6465 ```js66 const commander = require('commander');67 const program = new commander.Command();66 const { Command } = require('commander');67 const program = new Command();68 program.version('0.0.1');69 ```7071## Options
15 unmodified lines
8788The two most used option types are a boolean flag, and an option which takes a value (declared using angle brackets). Both are `undefined` unless specified on command line.8990```js91const program = require('commander');91const { program } = require('commander');9293program94 .option('-d, --debug', 'output extra debugging')95 .option('-s, --small', 'small pizza size')
29 unmodified lines
125126You can specify a default value for an option which takes a value.127128```js129const program = require('commander');129const { program } = require('commander');130131program132 .option('-c, --cheese <type>', 'add the specified type of cheese', 'blue');133
17 unmodified lines
151If you define `--foo` first, adding `--no-foo` does not change the default value from what it would152otherwise be. You can specify a default boolean value for a boolean flag and it can be overridden on command line.153154```js155const program = require('commander');155const { program } = require('commander');156157program158 .option('--no-sauce', 'Remove sauce')159 .option('--cheese <flavour>', 'cheese flavour', 'mozzarella')
18 unmodified lines
178179You can specify an option which functions as a flag but may also take a value (declared using square brackets).180181```js182const program = require('commander');182const { program } = require('commander');183184program185 .option('-c, --cheese [type]', 'Add cheese with optional type');186
22 unmodified lines
209210You can optionally specify the default/starting value for the option after the function.211212```js213const program = require('commander');213const { program } = require('commander');214215function myParseInt(value, dummyPrevious) {216 // parseInt takes a string and an optional radix217 return parseInt(value);
45 unmodified lines
263264You 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.265266```js267const program = require('commander');267const { program } = require('commander');268269program270 .requiredOption('-c, --cheese <type>', 'pizza must have cheese');271
63 unmodified lines
335336You 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.337338```js339const program = require('commander');339const { program } = require('commander');340341program342 .version('0.1.0')343 .arguments('<cmd> [env]')
15 unmodified lines
359 The last argument of a command can be variadic, and only the last argument. To make an argument variadic you360 append `...` to the argument name. For example:361362```js363const program = require('commander');363const { program } = require('commander');364365program366 .version('0.1.0')367 .command('rmdir <dir> [otherDirs...]')
17 unmodified lines
385The action handler gets passed a parameter for each argument you declared, and one additional argument which is the386command object itself. This command argument has the values for the command-specific options added as properties.387388```js389const program = require('commander');389const { program } = require('commander');390391program392 .command('rm <dir>')393 .option('-r, --recursive', 'Remove recursively')
28 unmodified lines
422You handle the options for an executable (sub)command in the executable, and don't declare them at the top-level.423424```js425// file: ./examples/pm426const program = require('commander');426const { program } = require('commander');427428program429 .version('0.1.0')430 .command('install [name]', 'install one or more packages')
235 unmodified lines
666667## Examples668669```js670const program = require('commander');670const { program } = require('commander');671672program673 .version('0.1.0')674 .option('-C, --chdir <path>', 'change the working directory')
49 unmodified lines