From 9cee32493282784de4158d595555fd460c1d0d8c Mon Sep 17 00:00:00 2001 From: Dan Allen Date: Thu, 18 Jan 2018 23:35:06 -0700 Subject: [PATCH] document how to override the version flag --- Readme.md | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/Readme.md b/Readme.md index 7dcdaf9fe..25c09af95 100644 --- a/Readme.md +++ b/Readme.md @@ -16,7 +16,7 @@ ## Option parsing - Options with commander are defined with the `.option()` method, also serving as documentation for the options. The example below parses args and options from `process.argv`, leaving remaining args as the `program.args` array which were not consumed by options. +Options with commander are defined with the `.option()` method, also serving as documentation for the options. The example below parses args and options from `process.argv`, leaving remaining args as the `program.args` array which were not consumed by options. ```js #!/usr/bin/env node @@ -42,7 +42,7 @@ if (program.bbqSauce) console.log(' - bbq'); console.log(' - %s cheese', program.cheese); ``` - Short flags may be passed as a single arg, for example `-abc` is equivalent to `-a -b -c`. Multi-word options such as "--template-engine" are camel-cased, becoming `program.templateEngine` etc. +Short flags may be passed as a single arg, for example `-abc` is equivalent to `-a -b -c`. Multi-word options such as "--template-engine" are camel-cased, becoming `program.templateEngine` etc. Note that multi-word options starting with `--no` prefix negate the boolean value of the following word. For example, `--no-sauce` sets the value of `program.sauce` to false. @@ -64,6 +64,23 @@ if (program.sauce) console.log(' with sauce'); else console.log(' without sauce'); ``` +## Version option + +Calling the `version` implicitly adds the `-V` and `--version` options to the command. +When either of these options is present, the command prints the version number and exits. + + $ ./examples/pizza -V + 0.0.1 + +If you want your program to respond to the `-v` option instead of the `-V` option, simply pass custom flags to the `version` method using the same syntax as the `option` method. + +```js +program + .version('0.0.1', '-v, --version') +``` + +Now the command will accept the `-v` option instead of the `-V` option. + ## Command-specific options You can attach options to a command.