Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More consistent way of adding command arguments #1490

Merged
merged 36 commits into from Apr 8, 2021
Merged
Show file tree
Hide file tree
Changes from 34 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
39a47ed
added addArguments method
Niryo Feb 6, 2021
88a4cc5
fix undefined args
Niryo Feb 6, 2021
e8415ed
added tests, docs and typings
Niryo Feb 13, 2021
782f705
code review fixes
Niryo Feb 19, 2021
d106ca0
throw error on bad arg
Niryo Mar 6, 2021
39cb757
Parse command-argument details in constructor
shadowspawn Mar 13, 2021
8c4d8d0
Handle argument descriptions separately for legacy and new support
shadowspawn Mar 13, 2021
c2e0a5a
Add text to distinguish test names with .each
shadowspawn Mar 13, 2021
32f001b
Match nameAndArgs into two parts, rather than split on spaces.
shadowspawn Mar 14, 2021
4a8e323
Merge pull request #1 from shadowspawn/Niryo-addArgument
Niryo Mar 14, 2021
ad20e0b
Update release date post-release to be more accurate
shadowspawn Mar 22, 2021
9a77be4
Merge pull request #1467 from Niryo/addArgument
shadowspawn Mar 27, 2021
da37436
Fix test naming
shadowspawn Mar 27, 2021
81248e2
Simplify and tidy argument example
shadowspawn Mar 27, 2021
6052c14
Typing and typings test for .argument
shadowspawn Mar 27, 2021
0dd3547
Expand argument section to include existing multiple-argument approac…
shadowspawn Mar 27, 2021
0c43cd9
Add name method and improve Argument typings and tests
shadowspawn Mar 27, 2021
0dea8ac
Fix copy-and-paste JSDoc error
shadowspawn Mar 27, 2021
82ff5bd
Update example to match new method and README
shadowspawn Mar 27, 2021
a20a8fe
Deprecate old way of adding command argument descriptions
shadowspawn Mar 27, 2021
df73a19
Be lenient about Argument construction to allow lazy building
shadowspawn Mar 27, 2021
43ce351
Call first param to .argument "name", and expand jsdoc
shadowspawn Mar 28, 2021
af08a2b
Add low-level check that get same Argument from multiple ways of spec…
shadowspawn Mar 28, 2021
bac5ea9
Minor wording tweaks
shadowspawn Mar 28, 2021
0e505be
Add low-level tests for multiple arg variations
shadowspawn Mar 28, 2021
dac9656
Simplify test. Use .argument now.
shadowspawn Mar 28, 2021
3745659
Restore simple test, use .argument
shadowspawn Mar 28, 2021
a76f831
Big switch from .arguments to .argument in tests
shadowspawn Mar 28, 2021
5a08571
Expand help to explain argument variations
shadowspawn Mar 28, 2021
747d6ef
Keep Argument properties private for now (like Command, unlike Option)
shadowspawn Mar 29, 2021
b7def8a
Argument should follow Option for properties, make public again
shadowspawn Mar 30, 2021
f54d1ed
Generate help for arguments using same methods as for option and subc…
shadowspawn Mar 30, 2021
0879a1f
Simplify Argument .name(), just getter
shadowspawn Mar 30, 2021
1e66821
Expand test coverage for visibleArguments
shadowspawn Mar 30, 2021
ed7a1c8
Rework the multiple ways of specifying command-arguments
shadowspawn Apr 5, 2021
819616f
Add Argument to esm exports (and createOption)
shadowspawn Apr 7, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Expand Up @@ -8,7 +8,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
<!-- markdownlint-disable MD024 -->
<!-- markdownlint-disable MD004 -->

## [7.2.0] (2021-03-26)
## [7.2.0] (2021-03-22)

### Added

Expand Down
33 changes: 20 additions & 13 deletions Readme.md
Expand Up @@ -428,27 +428,34 @@ subcommand is specified ([example](./examples/defaultCommand.js)).

### Specify the argument syntax

You use `.arguments` to specify the expected command-arguments for the top-level command, and for subcommands they are usually
included in the `.command` call. Angled brackets (e.g. `<required>`) indicate required command-arguments.
Square brackets (e.g. `[optional]`) indicate optional command-arguments.
You can optionally describe the arguments in the help by supplying a hash as second parameter to `.description()`.
You use `.argument` to specify the expected command-arguments.
Angle brackets around the name indicate a required command-argument (e.g. `<required>`).
Square brackets indicate an optional command-argument (e.g. `[optional]`). The description parameter is optional.

Example file: [arguments.js](./examples/arguments.js)
Example file: [argument.js](./examples/argument.js)

```js
program
.version('0.1.0')
.arguments('<username> [password]')
.description('test command', {
username: 'user to login',
password: 'password for user, if required'
})
.argument('<username>', 'user to login')
.argument('[password]', 'password for user, if required')
.action((username, password) => {
console.log('username:', username);
console.log('environment:', password || 'no password given');
console.log('password:', password || 'no password given');
});
```

There are two ways to specify all the arguments at once, but these do not allow argument descriptions.

```js
program
.command('add <number> <number>');

program
.command('fraction')
.arguments('<numerator> <denominator>');
```

The last argument of a command can be variadic, and only the last argument. To make an argument variadic you
append `...` to the argument name. For example:

Expand All @@ -463,7 +470,7 @@ program
});
```

The variadic argument is passed to the action handler as an array.
A variadic argument is passed to the action handler as an array.

### Action handler

Expand All @@ -474,7 +481,7 @@ Example file: [thank.js](./examples/thank.js)

```js
program
.arguments('<name>')
.argument('<name>')
.option('-t, --title <honorific>', 'title to use before name')
.option('-d, --debug', 'display some debugging')
.action((name, options, command) => {
Expand Down
25 changes: 25 additions & 0 deletions docs/deprecated.md
Expand Up @@ -86,3 +86,28 @@ Examples:
```

Deprecated from Commander v7.

## cmd.description(cmdDescription, argDescriptions)

This was used to add command argument descriptions for the help.

```js
program
.command('price <book>')
.description('show price of book', {
book: 'ISBN number for book'
});
```

The new approach is to use the `.argument()` method.

```js
program
.command('price')
.description('show price of book')
.argument('<book>', 'ISBN number for book');
```


Deprecated from Commander v8.

4 changes: 2 additions & 2 deletions docs/options-taking-varying-arguments.md
Expand Up @@ -5,7 +5,7 @@ and subtle issues in depth.

- [Options taking varying numbers of option-arguments](#options-taking-varying-numbers-of-option-arguments)
- [Parsing ambiguity](#parsing-ambiguity)
- [Alternative: Make `--` part of your syntax](#alternative-make----part-of-your-syntax)
- [Alternative: Make `--` part of your syntax](#alternative-make-----part-of-your-syntax)
- [Alternative: Put options last](#alternative-put-options-last)
- [Alternative: Use options instead of command-arguments](#alternative-use-options-instead-of-command-arguments)
- [Combining short options, and options taking arguments](#combining-short-options-and-options-taking-arguments)
Expand Down Expand Up @@ -34,7 +34,7 @@ intend the argument following the option as a command or command-argument.
```js
program
.name('cook')
.arguments('[technique]')
.argument('[technique]')
.option('-i, --ingredient [ingredient]', 'add cheese or given ingredient')
.action((technique, options) => {
console.log(`technique: ${technique}`);
Expand Down
4 changes: 2 additions & 2 deletions docs/zh-CN/可变参数的选项.md
Expand Up @@ -31,7 +31,7 @@ Commander 首先解析选项的参数,而用户有可能想将选项后面跟
```js
program
.name('cook')
.arguments('[technique]')
.argument('[technique]')
.option('-i, --ingredient [ingredient]', 'add cheese or given ingredient')
.action((technique, options) => {
console.log(`technique: ${technique}`);
Expand Down Expand Up @@ -190,4 +190,4 @@ halal servings: true
```js
.combineFlagAndOptionalValue(true) // `-v45` 被视为 `--vegan=45`,这是默认的行为
.combineFlagAndOptionalValue(false) // `-vl` 被视为 `-v -l`
```
```
24 changes: 24 additions & 0 deletions examples/argument.js
@@ -0,0 +1,24 @@
#!/usr/bin/env node

// This example shows specifying the arguments using argument() function.

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

program
.version('0.1.0')
.argument('<username>', 'user to login')
.argument('[password]', 'password for user, if required')
.description('example program for argument')
.action((username, password) => {
console.log('username:', username);
console.log('password:', password || 'no password given');
});

program.parse();

// Try the following:
// node arguments.js --help
// node arguments.js user
// node arguments.js user secret
2 changes: 1 addition & 1 deletion examples/arguments.js
Expand Up @@ -15,7 +15,7 @@ program
})
.action((username, password) => {
console.log('username:', username);
console.log('environment:', password || 'no password given');
console.log('password:', password || 'no password given');
});

program.parse();
Expand Down
3 changes: 2 additions & 1 deletion examples/pass-through-options.js
Expand Up @@ -5,7 +5,8 @@ const { Command } = require('../'); // include commander in git clone of command
const program = new Command();

program
.arguments('<utility> [args...]')
.argument('<utility>')
.argument('[args...]')
.passThroughOptions()
.option('-d, --dry-run')
.action((utility, args, options) => {
Expand Down
2 changes: 1 addition & 1 deletion examples/thank.js
Expand Up @@ -7,7 +7,7 @@ const { Command } = require('../'); // include commander in git clone of command
const program = new Command();

program
.arguments('<name>')
.argument('<name>')
.option('-t, --title <honorific>', 'title to use before name')
.option('-d, --debug', 'display some debugging')
.action((name, options, command) => {
Expand Down