Skip to content

Commit

Permalink
Add argument choices to README
Browse files Browse the repository at this point in the history
  • Loading branch information
shadowspawn committed May 21, 2021
1 parent 05ddc17 commit ca17c15
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
24 changes: 18 additions & 6 deletions Readme.md
Expand Up @@ -22,8 +22,9 @@ Read this in other languages: English | [简体中文](./Readme_zh-CN.md)
- [More configuration](#more-configuration)
- [Custom option processing](#custom-option-processing)
- [Commands](#commands)
- [Specify the argument syntax](#specify-the-argument-syntax)
- [Custom argument processing](#custom-argument-processing)
- [Command-arguments](#command-arguments)
- [More configuration](#more-configuration-1)
- [Custom argument processing](#custom-argument-processing)
- [Action handler](#action-handler)
- [Stand-alone executable (sub)commands](#stand-alone-executable-subcommands)
- [Life cycle hooks](#life-cycle-hooks)
Expand All @@ -33,7 +34,7 @@ Read this in other languages: English | [简体中文](./Readme_zh-CN.md)
- [.usage and .name](#usage-and-name)
- [.helpOption(flags, description)](#helpoptionflags-description)
- [.addHelpCommand()](#addhelpcommand)
- [More configuration](#more-configuration-1)
- [More configuration](#more-configuration-2)
- [Custom event listeners](#custom-event-listeners)
- [Bits and pieces](#bits-and-pieces)
- [.parse() and .parseAsync()](#parse-and-parseasync)
Expand Down Expand Up @@ -431,7 +432,7 @@ Configuration options can be passed with the call to `.command()` and `.addComma
remove the command from the generated help output. Specifying `isDefault: true` will run the subcommand if no other
subcommand is specified ([example](./examples/defaultCommand.js)).
### Specify the argument syntax
### Command-arguments
For subcommands, you can specify the argument syntax in the call to `.command()` (as shown above). This
is the only method usable for subcommands implemented using a stand-alone executable, but for other subcommands
Expand All @@ -441,7 +442,6 @@ To configure a command, you can use `.argument()` to specify each expected comma
You supply the argument name and an optional description. The argument may be `<required>` or `[optional]`.
You can specify a default value for an optional command-argument.
Example file: [argument.js](./examples/argument.js)
```js
Expand Down Expand Up @@ -477,7 +477,19 @@ program
.arguments('<username> <password>');
```
### Custom argument processing
#### More configuration
There are some additional features available by constructing an `Argument` explicitly for less common cases.
Example file: [arguments-extra.js](./examples/arguments-extra.js)
```js
program
.addArgument(new commander.Argument('<drink-size>', 'drink cup size').choices(['small', 'medium', 'large']))
.addArgument(new commander.Argument('[timeout]', 'timeout in seconds').default(60, 'one minute'))
```
#### Custom argument processing
You may specify a function to do custom processing of command-arguments before they are passed to the action handler.
The callback function receives two parameters, the user specified command-argument and the previous value for the argument.
Expand Down
23 changes: 23 additions & 0 deletions examples/arguments-extra.js
@@ -0,0 +1,23 @@
#!/usr/bin/env node

// This is used as an example in the README for extra argument features.

// const commander = require('commander'); // (normal include)
const commander = require('../'); // include commander in git clone of commander repo
const program = new commander.Command();

program
.addArgument(new commander.Argument('<drink-size>', 'drink cup size').choices(['small', 'medium', 'large']))
.addArgument(new commander.Argument('[timeout]', 'timeout in seconds').default(60, 'one minute'))
.action((drinkSize, timeout) => {
console.log(`Drink size: ${drinkSize}`);
console.log(`Timeout (s): ${timeout}`);
});

program.parse();

// Try the following:
// node arguments-extra.js --help
// node arguments-extra.js huge
// node arguments-extra.js small
// node arguments-extra.js medium 30

0 comments on commit ca17c15

Please sign in to comment.