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 1 commit
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Big switch from .arguments to .argument in tests
  • Loading branch information
shadowspawn committed Mar 28, 2021
commit a76f831ee06ccb8e7389edb98963ce5ef1413cf5
4 changes: 2 additions & 2 deletions docs/options-taking-varying-arguments.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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`
```
```
3 changes: 2 additions & 1 deletion examples/pass-through-options.js
Original file line number Diff line number Diff line change
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
8 changes: 4 additions & 4 deletions tests/command.allowExcessArguments.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ describe('allowUnknownOption', () => {
test('when specify expected arg and allowExcessArguments(false) then no error', () => {
const program = new commander.Command();
program
.arguments('<file>')
.argument('<file>')
.exitOverride()
.allowExcessArguments(false)
.action(() => {});
Expand All @@ -107,7 +107,7 @@ describe('allowUnknownOption', () => {
test('when specify excess after <arg> and allowExcessArguments(false) then error', () => {
const program = new commander.Command();
program
.arguments('<file>')
.argument('<file>')
.exitOverride()
.allowExcessArguments(false)
.action(() => {});
Expand All @@ -120,7 +120,7 @@ describe('allowUnknownOption', () => {
test('when specify excess after [arg] and allowExcessArguments(false) then error', () => {
const program = new commander.Command();
program
.arguments('[file]')
.argument('[file]')
.exitOverride()
.allowExcessArguments(false)
.action(() => {});
Expand All @@ -133,7 +133,7 @@ describe('allowUnknownOption', () => {
test('when specify args for [args...] and allowExcessArguments(false) then no error', () => {
const program = new commander.Command();
program
.arguments('[files...]')
.argument('[files...]')
.exitOverride()
.allowExcessArguments(false)
.action(() => {});
Expand Down
2 changes: 1 addition & 1 deletion tests/command.allowUnknownOption.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ describe('allowUnknownOption', () => {
program
.exitOverride()
.command('sub')
.arguments('[args...]') // unknown option will be passed as an argument
.argument('[args...]') // unknown option will be passed as an argument
.allowUnknownOption()
.option('-p, --pepper', 'add pepper')
.action(() => { });
Expand Down
8 changes: 4 additions & 4 deletions tests/command.asterisk.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe(".command('*')", () => {
const program = new commander.Command();
program
.command('*')
.arguments('[args...]')
.argument('[args...]')
.action(mockAction);
program.parse(['node', 'test', 'unrecognised-command']);
expect(mockAction).toHaveBeenCalled();
Expand All @@ -59,7 +59,7 @@ describe(".command('*')", () => {
.command('install');
program
.command('*')
.arguments('[args...]')
.argument('[args...]')
.action(mockAction);
program.parse(['node', 'test', 'unrecognised-command']);
expect(mockAction).toHaveBeenCalled();
Expand All @@ -73,7 +73,7 @@ describe(".command('*')", () => {
.command('install');
const star = program
.command('*')
.arguments('[args...]')
.argument('[args...]')
.option('-d, --debug')
.action(mockAction);
program.parse(['node', 'test', 'unrecognised-command', '--debug']);
Expand All @@ -93,7 +93,7 @@ describe(".command('*')", () => {
.command('install');
program
.command('*')
.arguments('[args...]')
.argument('[args...]')
.action(mockAction);
let caughtErr;
try {
Expand Down
14 changes: 13 additions & 1 deletion tests/command.chain.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { Command, Option } = require('../');
const { Command, Option, Argument } = require('../');

// Testing the functions which should chain.
// parse and parseAsync are tested in command.parse.test.js
Expand All @@ -16,6 +16,18 @@ describe('Command methods that should return this for chaining', () => {
expect(result).toBe(program);
});

test('when call .argument() then returns this', () => {
const program = new Command();
const result = program.argument('<file>');
expect(result).toBe(program);
});

test('when call .addArgument() then returns this', () => {
const program = new Command();
const result = program.addArgument(new Argument('<file>'));
expect(result).toBe(program);
});

test('when set .arguments() then returns this', () => {
const program = new Command();
const result = program.arguments('<file>');
Expand Down
17 changes: 13 additions & 4 deletions tests/command.help.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,16 +235,25 @@ test('when option has choices and default then both included in helpInformation'
expect(helpInformation).toMatch('(choices: "red", "blue", default: "red")');
});

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

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

test('when arguments described in deprecated way then included in helpInformation', () => {
const program = new commander.Command();
program
.arguments('<file>')
Expand All @@ -254,7 +263,7 @@ test('when arguments described then included in helpInformation', () => {
expect(helpInformation).toMatch(/Arguments:\n +file +input source/);
});

test('when arguments described and empty description then arguments included in helpInformation', () => {
test('when arguments described in deprecated way and empty description then arguments still included in helpInformation', () => {
const program = new commander.Command();
program
.arguments('<file>')
Expand Down
26 changes: 13 additions & 13 deletions tests/command.positionalOptions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe('program with passThrough', () => {
program.passThroughOptions();
program
.option('-d, --debug')
.arguments('<args...>');
.argument('<args...>');
return program;
}

Expand Down Expand Up @@ -77,10 +77,10 @@ describe('program with positionalOptions and subcommand', () => {
program
.enablePositionalOptions()
.option('-s, --shared <value>')
.arguments('<args...>');
.argument('<args...>');
const sub = program
.command('sub')
.arguments('[arg]')
.argument('[arg]')
.option('-s, --shared <value>')
.action(() => {}); // Not used, but normal to have action handler on subcommand.
return { program, sub };
Expand Down Expand Up @@ -151,10 +151,10 @@ describe('program with positionalOptions and default subcommand (called sub)', (
.enablePositionalOptions()
.option('-s, --shared')
.option('-g, --global')
.arguments('<args...>');
.argument('<args...>');
const sub = program
.command('sub', { isDefault: true })
.arguments('[args...]')
.argument('[args...]')
.option('-s, --shared')
.option('-d, --default')
.action(() => {}); // Not used, but normal to have action handler on subcommand.
Expand Down Expand Up @@ -218,11 +218,11 @@ describe('subcommand with passThrough', () => {
program
.enablePositionalOptions()
.option('-s, --shared <value>')
.arguments('<args...>');
.argument('<args...>');
const sub = program
.command('sub')
.passThroughOptions()
.arguments('[args...]')
.argument('[args...]')
.option('-s, --shared <value>')
.option('-d, --debug')
.action(() => {}); // Not used, but normal to have action handler on subcommand.
Expand Down Expand Up @@ -289,7 +289,7 @@ describe('default command with passThrough', () => {
const sub = program
.command('sub', { isDefault: true })
.passThroughOptions()
.arguments('[args...]')
.argument('[args...]')
.option('-d, --debug')
.action(() => {}); // Not used, but normal to have action handler on subcommand.
return { program, sub };
Expand Down Expand Up @@ -332,11 +332,11 @@ describe('program with action handler and positionalOptions and subcommand', ()
program
.enablePositionalOptions()
.option('-g, --global')
.arguments('<args...>')
.argument('<args...>')
.action(() => {});
const sub = program
.command('sub')
.arguments('[arg]')
.argument('[arg]')
.action(() => {});
return { program, sub };
}
Expand Down Expand Up @@ -379,11 +379,11 @@ describe('program with action handler and passThrough and subcommand', () => {
program
.passThroughOptions()
.option('-g, --global')
.arguments('<args...>')
.argument('<args...>')
.action(() => {});
const sub = program
.command('sub')
.arguments('[arg]')
.argument('[arg]')
.option('-g, --group')
.option('-d, --debug')
.action(() => {});
Expand Down Expand Up @@ -451,7 +451,7 @@ describe('passThroughOptions(xxx) and option after command-argument', () => {
const program = new commander.Command();
program
.option('-d, --debug')
.arguments('<args...>');
.argument('<args...>');
return program;
}

Expand Down
2 changes: 1 addition & 1 deletion tests/command.unknownCommand.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('unknownCommand', () => {
.exitOverride()
.command('sub');
program
.arguments('[args...]')
.argument('[args...]')
.action(() => { });
expect(() => {
program.parse('node test.js unknown'.split(' '));
Expand Down
4 changes: 2 additions & 2 deletions tests/command.unknownOption.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ describe('unknownOption', () => {
const program = new commander.Command();
program
.exitOverride()
.arguments('[file]')
.argument('[file]')
.action(() => {});

let caughtErr;
Expand All @@ -71,7 +71,7 @@ describe('unknownOption', () => {
const program = new commander.Command();
program
.exitOverride()
.arguments('[file]')
.argument('[file]')
.action(() => {});

let caughtErr;
Expand Down
6 changes: 3 additions & 3 deletions tests/commander.configureCommand.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ test('when default then options+command passed to action', () => {
const program = new commander.Command();
const callback = jest.fn();
program
.arguments('<value>')
.argument('<value>')
.action(callback);
program.parse(['node', 'test', 'value']);
expect(callback).toHaveBeenCalledWith('value', program.opts(), program);
Expand Down Expand Up @@ -60,7 +60,7 @@ test('when storeOptionsAsProperties() then command+command passed to action', ()
const callback = jest.fn();
program
.storeOptionsAsProperties()
.arguments('<value>')
.argument('<value>')
.action(callback);
program.parse(['node', 'test', 'value']);
expect(callback).toHaveBeenCalledWith('value', program, program);
Expand All @@ -71,7 +71,7 @@ test('when storeOptionsAsProperties(false) then opts+command passed to action',
const callback = jest.fn();
program
.storeOptionsAsProperties(false)
.arguments('<value>')
.argument('<value>')
.action(callback);
program.parse(['node', 'test', 'value']);
expect(callback).toHaveBeenCalledWith('value', program.opts(), program);
Expand Down
4 changes: 2 additions & 2 deletions tests/help.commandTerm.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe('subcommandTerm', () => {

test('when command has <argument> then returns name <argument>', () => {
const command = new commander.Command('program')
.arguments('<argument>');
.argument('<argument>');
const helper = new commander.Help();
expect(helper.subcommandTerm(command)).toEqual('program <argument>');
});
Expand All @@ -36,7 +36,7 @@ describe('subcommandTerm', () => {
const command = new commander.Command('program')
.alias('alias')
.option('-a,--all')
.arguments('<argument>');
.argument('<argument>');
const helper = new commander.Help();
expect(helper.subcommandTerm(command)).toEqual('program|alias [options] <argument>');
});
Expand Down
12 changes: 4 additions & 8 deletions tests/help.longestArgumentTermLength.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,16 @@ describe('longestArgumentTermLength', () => {

test('when has argument description then returns argument length', () => {
const program = new commander.Command();
program.arguments('<wonder>');
program.description('dummy', { wonder: 'wonder description' });
program.argument('<wonder>', 'wonder description');
const helper = new commander.Help();
expect(helper.longestArgumentTermLength(program, helper)).toEqual('wonder'.length);
});

test('when has multiple argument descriptions then returns longest', () => {
const program = new commander.Command();
program.arguments('<alpha> <longest> <beta>');
program.description('dummy', {
alpha: 'x',
longest: 'x',
beta: 'x'
});
program.argument('<alpha>', 'x');
program.argument('<longest>', 'x');
program.argument('<beta>', 'x');
const helper = new commander.Help();
expect(helper.longestArgumentTermLength(program, helper)).toEqual('longest'.length);
});
Expand Down
Loading