-
-
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.
Extend .argument() to add default value and custom parse for command-…
…argument (#1508) * First cut at adding default value and parse for Argument * Simplify reduce call, good match for pattern * Refactor new code into routine * Add custom processing argument tests * Add default value tests for argument * Rename tests * Add error for ignored default value * Add argument default to help information * Default only makes sense for optional argument * Extend argument typings * Add argument custom processing to README * Add InvalidArgumentError and deprecate InvalidOptionArgumentError * Fix JSDoc * Add example of varidic argument custom processing * Update comment * Remove low value comment
- Loading branch information
1 parent
3d67542
commit f99d6fa
Showing
16 changed files
with
446 additions
and
39 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
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
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,47 @@ | ||
#!/usr/bin/env node | ||
|
||
// This is used as an example in the README for: | ||
// Custom argument processing | ||
// You may specify a function to do custom processing of argument values. | ||
|
||
// const commander = require('commander'); // (normal include) | ||
const commander = require('../'); // include commander in git clone of commander repo | ||
const program = new commander.Command(); | ||
|
||
function myParseInt(value, dummyPrevious) { | ||
// parseInt takes a string and a radix | ||
const parsedValue = parseInt(value, 10); | ||
if (isNaN(parsedValue)) { | ||
throw new commander.InvalidArgumentError('Not a number.'); | ||
} | ||
return parsedValue; | ||
} | ||
|
||
// The previous value passed to the custom processing is used when processing variadic values. | ||
function mySum(value, total) { | ||
return total + myParseInt(value); | ||
} | ||
|
||
program | ||
.command('add') | ||
.argument('<first>', 'integer argument', myParseInt) | ||
.argument('[second]', 'integer argument', myParseInt, 1000) | ||
.action((first, second) => { | ||
console.log(`${first} + ${second} = ${first + second}`); | ||
}); | ||
|
||
program | ||
.command('sum') | ||
.argument('<value...>', 'values to be summed', mySum, 0) | ||
.action((total) => { | ||
console.log(`sum is ${total}`); | ||
}); | ||
|
||
program.parse(); | ||
|
||
// Try the following: | ||
// node arguments-custom-processing add --help | ||
// node arguments-custom-processing add 2 | ||
// node arguments-custom-processing add 12 56 | ||
// node arguments-custom-processing sum 1 2 3 | ||
// node arguments-custom-processing sum silly |
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
Oops, something went wrong.