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
Provenance
- Commit
1757564- Tree
65353a9- Author
John Gee <john@ruru.gen.nz>- Committer
GitHub <noreply@github.com>- Parent
0a50bd6
1 file changed+14 added−14 removed
modified:Readme.md+14 added-14 removed
54 unmodified lines55 Commander exports a global object which is convenient for quick programs.56 This is used in the examples in this README for brevity.57 58 ```js59-const program = require('commander').program;59+const { program } = require('commander');60 program.version('0.0.1');61 ```62 63 For larger programs which may use commander in multiple ways, including unit testing, it is better to create a local Command object to use.64 65 ```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 ```70 71 ## Options15 unmodified lines87 88 The 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.89 90 ```js91-const program = require('commander');91+const { program } = require('commander');92 93 program94 .option('-d, --debug', 'output extra debugging')95 .option('-s, --small', 'small pizza size')29 unmodified lines125 126 You can specify a default value for an option which takes a value.127 128 ```js129-const program = require('commander');129+const { program } = require('commander');130 131 program132 .option('-c, --cheese <type>', 'add the specified type of cheese', 'blue');133 17 unmodified lines151 If you define `--foo` first, adding `--no-foo` does not change the default value from what it would152 otherwise be. You can specify a default boolean value for a boolean flag and it can be overridden on command line.153 154 ```js155-const program = require('commander');155+const { program } = require('commander');156 157 program158 .option('--no-sauce', 'Remove sauce')159 .option('--cheese <flavour>', 'cheese flavour', 'mozzarella')18 unmodified lines178 179 You can specify an option which functions as a flag but may also take a value (declared using square brackets).180 181 ```js182-const program = require('commander');182+const { program } = require('commander');183 184 program185 .option('-c, --cheese [type]', 'Add cheese with optional type');186 22 unmodified lines209 210 You can optionally specify the default/starting value for the option after the function.211 212 ```js213-const program = require('commander');213+const { program } = require('commander');214 215 function myParseInt(value, dummyPrevious) {216 // parseInt takes a string and an optional radix217 return parseInt(value);45 unmodified lines263 264 You 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.265 266 ```js267-const program = require('commander');267+const { program } = require('commander');268 269 program270 .requiredOption('-c, --cheese <type>', 'pizza must have cheese');271 63 unmodified lines335 336 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.337 338 ```js339-const program = require('commander');339+const { program } = require('commander');340 341 program342 .version('0.1.0')343 .arguments('<cmd> [env]')15 unmodified lines359 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:361 362 ```js363-const program = require('commander');363+const { program } = require('commander');364 365 program366 .version('0.1.0')367 .command('rmdir <dir> [otherDirs...]')17 unmodified lines385 The action handler gets passed a parameter for each argument you declared, and one additional argument which is the386 command object itself. This command argument has the values for the command-specific options added as properties.387 388 ```js389-const program = require('commander');389+const { program } = require('commander');390 391 program392 .command('rm <dir>')393 .option('-r, --recursive', 'Remove recursively')28 unmodified lines422 You handle the options for an executable (sub)command in the executable, and don't declare them at the top-level.423 424 ```js425 // file: ./examples/pm426-const program = require('commander');426+const { program } = require('commander');427 428 program429 .version('0.1.0')430 .command('install [name]', 'install one or more packages')235 unmodified lines666 667 ## Examples668 669 ```js670-const program = require('commander');670+const { program } = require('commander');671 672 program673 .version('0.1.0')674 .option('-C, --chdir <path>', 'change the working directory')49 unmodified lines