Add .allowExcessArguments() and error message#1407
Merged
shadowspawn merged 7 commits intotj:release/7.xfrom Dec 4, 2020
Merged
Add .allowExcessArguments() and error message#1407shadowspawn merged 7 commits intotj:release/7.xfrom
shadowspawn merged 7 commits intotj:release/7.xfrom
Conversation
Collaborator
Author
|
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. |
abetomo
reviewed
Dec 2, 2020
index.js
Outdated
| * @param {Boolean} [allowExcess] - if `true` or omitted, no error will be thrown | ||
| * for excess arguments. | ||
| */ | ||
| allowExcessArguments(allowExcess) { |
Collaborator
There was a problem hiding this comment.
Are the default parameters available?
allowExcessArguments(allowExcess = true)
Collaborator
Author
There was a problem hiding this comment.
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.
Collaborator
Author
There was a problem hiding this comment.
Defaults from node 6, which is our minimum supported version: https://node.green/#ES2015-syntax-default-function-parameters
Collaborator
Author
There was a problem hiding this comment.
Added default parameter instead of manual test to multiple routines.
3 tasks
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
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
CommandStrictif that works out.)