-
-
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
Extra arguments : document and add configuration to fail if they exist #1268
Comments
Related issue, which prompted some improvements: #749 I want the error detection myself in one of my own programs. The arguments are (now) available through |
I do not consider |
I created a helper function for it: function handleTooManyArgs(action) {
return (...args) => {
if (args.length < 2) {
action(...args);
} else {
// The arguments to Commander actions are:
// expectedCliArg1, expectedCliArg2, expectedCliArgN, Cmd, restCliArgs
const rest = args[args.length - 1];
if (rest.length > 0) {
const expected = args.length - 2;
const s = expected === 1 ? '' : 's';
console.error(
`Expected ${expected} argument${s}, but got ${
expected + rest.length
}.`
);
process.exit(1);
} else {
action(...args);
}
}
};
}
program
.command('install <package>')
.action(
handleTooManyArgs((packageName, cmd) => {
// This is the regular .action callback
})
); |
I'm giving this a try in #1399, still a work in progress. |
Second try. Opt-in error for now. #1407 Get the error check by calling |
I have switched to an error by default for extra command-arguments. Can opt-out by calling This is available in v7.0.0-2 prerelease. Gentle readers, speak up soon if you think error-by-default is a bad idea! 📣 |
I am finding this is breaking quite a lot of my unit tests (due to lazily not declaring arguments) and not picking up actual problems. I think for Commander v7 it would be better to make this an opt-in behaviour that is inherited to subcommands (so can easily turn it on for whole program). Command 7 has breaking changes with options storage and action handler parameters, so plenty else for users to deal with! I'll think a bit more and do a PR for consideration. |
Included in Commander 7.0.0. |
Thanks! commander 7 is great, well done! 🎉 |
By default it seems that any extra argument will be available in an array after the command object.
The issue comes from the fact that it is not a documented behavior, and if the callback does not have this additional array parameter, it will silently be executed.
While this can be useful if you want to parse those extra arguments yourself, I think the default should be to error out when some arguments are not parsed (or at least, give it as an config option).
Right now, one must do the following:
It would be nice if something like this could have the same effect:
The text was updated successfully, but these errors were encountered: