-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
added addArguments method #1467
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
39a47ed
added addArguments method
Niryo 88a4cc5
fix undefined args
Niryo e8415ed
added tests, docs and typings
Niryo 782f705
code review fixes
Niryo d106ca0
throw error on bad arg
Niryo 39cb757
Parse command-argument details in constructor
shadowspawn 8c4d8d0
Handle argument descriptions separately for legacy and new support
shadowspawn c2e0a5a
Add text to distinguish test names with .each
shadowspawn 32f001b
Match nameAndArgs into two parts, rather than split on spaces.
shadowspawn 4a8e323
Merge pull request #1 from shadowspawn/Niryo-addArgument
Niryo File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The test naming convention being used is:
|
||
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The description from
arguments.js
would be better,'password for user, if required'
.