-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1467 from Niryo/addArgument
added addArguments method
- Loading branch information
Showing
9 changed files
with
257 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#!/usr/bin/env node | ||
|
||
// This example shows specifying the arguments using addArgument() and argument() function. | ||
|
||
// const { Command } = require('commander'); // (normal include) | ||
const { Command, Argument } = require('../'); // include commander in git clone of commander repo | ||
const program = new Command(); | ||
|
||
program | ||
.version('0.1.0') | ||
.addArgument(new Argument('<username>', 'user to login')) | ||
.argument('[password]', 'password') | ||
.description('test command') | ||
.action((username, password) => { | ||
console.log('username:', username); | ||
console.log('environment:', password || 'no password given'); | ||
}); | ||
|
||
program.parse(); | ||
|
||
// Try the following: | ||
// node arguments.js --help | ||
// node arguments.js user | ||
// node arguments.js user secret |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
const commander = require('../'); | ||
|
||
test('should throw on bad argument', () => { | ||
const program = new commander.Command(); | ||
expect(() => program.addArgument(new commander.Argument('bad-format'))).toThrow(); | ||
expect(() => program.argument('bad-format')).toThrow(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
const commander = require('../'); | ||
|
||
// Testing variadic arguments. Testing all the action arguments, but could test just variadicArg. | ||
|
||
describe('variadic argument', () => { | ||
test('when no extra arguments specified for program then variadic arg is empty array', () => { | ||
const actionMock = jest.fn(); | ||
const program = new commander.Command(); | ||
program | ||
.argument('<id>') | ||
.argument('[variadicArg...]') | ||
.action(actionMock); | ||
|
||
program.parse(['node', 'test', 'id']); | ||
|
||
expect(actionMock).toHaveBeenCalledWith('id', [], program.opts(), program); | ||
}); | ||
|
||
test('when extra arguments specified for program then variadic arg is array of values', () => { | ||
const actionMock = jest.fn(); | ||
const program = new commander.Command(); | ||
program | ||
.addArgument(new commander.Argument('<id>')) | ||
.argument('[variadicArg...]') | ||
.action(actionMock); | ||
const extraArguments = ['a', 'b', 'c']; | ||
|
||
program.parse(['node', 'test', 'id', ...extraArguments]); | ||
|
||
expect(actionMock).toHaveBeenCalledWith('id', extraArguments, program.opts(), program); | ||
}); | ||
|
||
test('when no extra arguments specified for command then variadic arg is empty array', () => { | ||
const actionMock = jest.fn(); | ||
const program = new commander.Command(); | ||
const cmd = program | ||
.command('sub [variadicArg...]') | ||
.action(actionMock); | ||
|
||
program.parse(['node', 'test', 'sub']); | ||
|
||
expect(actionMock).toHaveBeenCalledWith([], cmd.opts(), cmd); | ||
}); | ||
|
||
test('when extra arguments specified for command then variadic arg is array of values', () => { | ||
const actionMock = jest.fn(); | ||
const program = new commander.Command(); | ||
const cmd = program | ||
.command('sub [variadicArg...]') | ||
.action(actionMock); | ||
const extraArguments = ['a', 'b', 'c']; | ||
|
||
program.parse(['node', 'test', 'sub', ...extraArguments]); | ||
|
||
expect(actionMock).toHaveBeenCalledWith(extraArguments, cmd.opts(), cmd); | ||
}); | ||
|
||
test('when program variadic argument not last then error', () => { | ||
const program = new commander.Command(); | ||
|
||
expect(() => { | ||
program | ||
.argument('<variadicArg...>') | ||
.argument('[optionalArg]'); | ||
}).toThrow("only the last argument can be variadic 'variadicArg'"); | ||
}); | ||
|
||
test('when command variadic argument not last then error', () => { | ||
const program = new commander.Command(); | ||
|
||
expect(() => { | ||
program.command('sub <variadicArg...> [optionalArg]'); | ||
}).toThrow("only the last argument can be variadic 'variadicArg'"); | ||
}); | ||
|
||
test('when variadic argument then usage shows variadic', () => { | ||
const program = new commander.Command(); | ||
program | ||
.name('foo') | ||
.argument('[args...]'); | ||
|
||
expect(program.usage()).toBe('[options] [args...]'); | ||
}); | ||
}); |
Oops, something went wrong.