-
-
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
Add .allowExcessArguments() and error message #1407
Add .allowExcessArguments() and error message #1407
Conversation
Example const { program } = require('commander');
program
.arguments('<file')
.allowExcessArguments(false)
.action(() => {
console.log('Called main action handler')
});
program
.command('sub <first> <second>')
.allowExcessArguments(false)
.action(() => {
console.log('Called subcommand action handler')
});
program.parse(); $ node foo.js alpha beta
error: too many arguments. Expected 1 argument but got 2.
$ node foo.js sub alpha beta
Called subcommand action handler
$ node foo.js sub alpha beta gamma
error: too many arguments for 'sub'. Expected 2 arguments but got 3. |
index.js
Outdated
* @param {Boolean} [allowExcess] - if `true` or omitted, no error will be thrown | ||
* for excess arguments. | ||
*/ | ||
allowExcessArguments(allowExcess) { |
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.
Are the default parameters available?
allowExcessArguments(allowExcess = true)
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.
Good question, I'll look into that. We have a few methods with this sort of pattern that could use it. Likely can because we have moved forward our minimum version of node.
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.
Defaults from node 6, which is our minimum supported version: https://node.green/#ES2015-syntax-default-function-parameters
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.
Added default parameter instead of manual test to multiple routines.
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.
Thank you for fixing it.
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.
Great!
Pull Request
Problem
By default, excess arguments are silently ignored. Checking manually is a bit tricky.
See: #259 #749 #1000 #1268
Solution
.allowExcessArguments()
is like.allowUnknownOption()
to enable/disable the new errorCommandStrict
, see WIP: CommandStrict #1404Like missing arguments, only checked if there is an action handler.
ChangeLog
.allowExcessArguments()
to control whether excess arguments are allowed for command (Add .allowExcessArguments() and error message #1407)Later
I have not added to the README yet and will do that separately. (Hopefully in combination with
CommandStrict
if that works out.)