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

Extend .argument() to add default value and custom parse for command-argument #1508

Merged
merged 16 commits into from
May 3, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
Add example of varidic argument custom processing
  • Loading branch information
shadowspawn committed Apr 30, 2021
commit eb498b2e091b97e495dfe9d6ed4116355895ea9c
1 change: 1 addition & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,7 @@ Example file: [arguments-custom-processing.js](./examples/arguments-custom-proce

```js
program
.command('add')
.argument('<first>', 'integer argument', myParseInt)
.argument('[second]', 'integer argument', myParseInt, 1000)
.action((first, second) => {
Expand Down
24 changes: 19 additions & 5 deletions examples/arguments-custom-processing.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,31 @@ function myParseInt(value, dummyPrevious) {
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 --help
// node arguments-custom-processing 2
// node arguments-custom-processing 12 56
// 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