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

Document and test description of arguments #1353

Merged
merged 4 commits into from Sep 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 5 additions & 0 deletions Readme.md
Expand Up @@ -368,13 +368,18 @@ Configuration options can be passed with the call to `.command()` and `.addComma
### Specify the argument syntax

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

Example file: [env](./examples/env)

```js
program
.version('0.1.0')
.arguments('<cmd> [env]')
.description('test command', {
cmd: 'command to run',
env: 'environment to run test in'
})
.action(function (cmd, env) {
console.log('command:', cmd);
console.log('environment:', env || 'no environment given');
Expand Down
9 changes: 9 additions & 0 deletions examples/env
Expand Up @@ -12,9 +12,18 @@ let envValue;
program
.version('0.0.1')
.arguments('<cmd> [env]')
.description('test command', {
cmd: 'command to run',
env: 'environment to run test in'
})
.action(function(cmdValue, envValue) {
console.log('command:', cmdValue);
console.log('environment:', envValue || 'no environment given');
});

program.parse(process.argv);

// Try the following:
// node env --help
// node env add
// node env add browser
19 changes: 19 additions & 0 deletions tests/command.help.test.js
Expand Up @@ -139,3 +139,22 @@ test('when no options then Options not includes in helpInformation', () => {
const helpInformation = program.helpInformation();
expect(helpInformation).not.toMatch('Options');
});

test('when arguments then included in helpInformation', () => {
const program = new commander.Command();
program
.name('foo')
.arguments('<file>');
const helpInformation = program.helpInformation();
expect(helpInformation).toMatch('Usage: foo [options] <file>');
});

test('when arguments described then included in helpInformation', () => {
const program = new commander.Command();
program
.arguments('<file>')
.helpOption(false)
.description('description', { file: 'input source' });
const helpInformation = program.helpInformation();
expect(helpInformation).toMatch(/Arguments:\n\n +file +input source/); // [sic], extra line
});