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

No error appearing if not existing command is called #287

Closed
maxrimue opened this issue Oct 31, 2015 · 12 comments
Closed

No error appearing if not existing command is called #287

maxrimue opened this issue Oct 31, 2015 · 12 comments
Labels

Comments

@maxrimue
Copy link
Member

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:

$ myapp
"Not enough arguments..."
"Use --help for further information"

If I however call my app with a command that doesn't exist, I just get no output at all:

$ myapp commandthatdoesntexist
$

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.

@nexdrew
Copy link
Member

nexdrew commented Nov 1, 2015

@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?

@maxrimue
Copy link
Member Author

maxrimue commented Nov 1, 2015

Thanks for the response @nexdrew, well I think it should just say something like 'command' is not a command, and show help output based on the value of .showHelpOnFail(), so it should trigger an error with this message, which also is how my CLI programmes behave when they expect a command but receive a not existing one:

max$ brew lol
Error: Unknown command: lol

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:

max$ git vomit
git: 'vomit' is not a git command. See 'git --help'.

Did you mean this?
commit

@maxrimue
Copy link
Member Author

maxrimue commented Dec 3, 2015

@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?

@nexdrew
Copy link
Member

nexdrew commented Dec 3, 2015

@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 4.x label that we start throwing onto issues we think would be good candidates for yargs 4?

@maxrimue
Copy link
Member Author

maxrimue commented Dec 3, 2015

@nexdrew Sure, I'm on it :D

@nexdrew
Copy link
Member

nexdrew commented Dec 3, 2015

@maxrimue Awesome, thanks! I just created the label. 👍

@nexdrew
Copy link
Member

nexdrew commented Dec 3, 2015

@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 enhancement issues too.

@maxrimue maxrimue mentioned this issue Dec 3, 2015
15 tasks
@bcoe bcoe added the 4.x label Jan 9, 2016
@orlade
Copy link

orlade commented Jan 31, 2016

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 argv._[1]. Note that the yargs.getCommandHandlers() method is reset after triggering yargs with .argv.

@maxrimue
Copy link
Member Author

@orlade Thanks!

@OSjoerdWie
Copy link

OSjoerdWie commented Jul 11, 2017

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);
}

drudv added a commit to drudv/font-compiler that referenced this issue Jul 18, 2017
@jazzzz
Copy link

jazzzz commented Aug 28, 2017

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?

@bcoe
Copy link
Member

bcoe commented Aug 29, 2017

@jazzzz you should be able to use .strict() I believe, have you given this a shot? it might be worth opening a new ticket if this doesn't work, because these old closed issues tend to get lost in the shuffle.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants