-
Notifications
You must be signed in to change notification settings - Fork 992
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
No error appearing if not existing command is called #287
Comments
@maxrimue Thanks! I think this is the same issue as #174. There's some discussion there, but it was never implemented or resolved. We have a few ideas for making the command API a little better which will probably be a primary feature of yargs v4, but it's been difficult to nail down a roadmap. Have any specific thoughts or ideas on this matter? Would you expect/prefer yargs to fail-fast with a specific error message here? Would a "strict mode" for commands work for you? |
Thanks for the response @nexdrew, well I think it should just say something like
You could go even further and do it like git, which not only fails with an error output, but suggests a command based on what you typed:
|
@nexdrew Hey, I was wondering if we could start to approach version 4 by making one issue for collecting ideas for what to change, especially in the API? |
@maxrimue I like that idea. How 'bout we create a new issue ( and by "we" I mean "you" 😀 ) specifically for the next commands API that we can start throwing ideas into, and we couple that with a new |
@nexdrew Sure, I'm on it :D |
@maxrimue Awesome, thanks! I just created the label. 👍 |
@maxrimue If you have ideas for 4.x API changes beyond commands, just create more issues. I'm sure that (plus the new label) will help move our roadmap forward. Might want to take a look at existing |
Handy workaround for those looking for a solution: var commandHandlers = yargs.getCommandHandlers();
var argv = yargs
.command('foo', 'Do the foo')
.command('bar', 'Do the bar')
.argv;
if (!argv._[0] || !(argv._[0] in commandHandlers)) {
throw new Error('Must provide a valid command');
} Works in subcommands too using |
@orlade Thanks! |
The following call does not work (anymore): yargs.getCommandHandlers(); The following will work: yargs.getCommandInstance().getCommands() Note that this function returns all command names (as well as aliases) in a string array. My solution now becomes: yargs
.command('foo', 'Do the foo')
.command('bar', 'Do the bar');
commands = yargs.getCommandInstance().getCommands();
argv = yargs.argv;
if (!argv._[0] || commands.indexOf(argv._[0]) === -1) {
console.log("non-existing or no command specified");
yargs.showHelp();
process.exit(1);
} |
I have the same issue with yargs 8.0.2. Is there an API to handle this or shall I still handle this myself like @orlade and @OSjoerdWie suggested? |
@jazzzz you should be able to use |
I have a project using yargs and everything works as expected, except for if I call my programme with a command that doesn't exist. If I don't specify a command:
If I however call my app with a command that doesn't exist, I just get no output at all:
The programme fails silently, not executing any of its code.
Note: All commands I specified do work as expected, also failing if not enough options are passed.
The text was updated successfully, but these errors were encountered: