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

How to display help when someone types an invalid option? #919

Closed
toymachiner62 opened this issue Feb 20, 2019 · 6 comments
Closed

How to display help when someone types an invalid option? #919

toymachiner62 opened this issue Feb 20, 2019 · 6 comments

Comments

@toymachiner62
Copy link

If someone types in an option that doesn't exist, i want to display the --help text similarly to using program.help().

Where can i listen for errors such as this:

$ ./createUser -V
 error: unknown option `-V'

So that I can call program.help()?

@willxy
Copy link

willxy commented Mar 6, 2019

This came up previously also in #482.

@Tarabass
Copy link

Does this help?

// Assert that a VALID command is provided 
if (!process.argv.slice(2).length || !/[arudl]/.test(process.argv.slice(2))) {
    program.outputHelp()
    process.exit()
}

@shadowspawn
Copy link
Collaborator

An answer was provided, and no further activity in a month. Closing this as resolved.

Feel free to open a new issue if it comes up again, with new information and renewed interest.

@trusktr
Copy link

trusktr commented May 11, 2021

In case someone stumbles here, this is how to show help when an invalid option was passed:

const cli = new commander.Command()

cli
	.description('...')
	.option(...)
	// ...
	.exitOverride(err => {
		// If an unknown option was passed, show help.
		if (err.code === 'commander.unknownOption') cli.help();
	});

cli.parse()

@shadowspawn

This comment has been minimized.

@shadowspawn
Copy link
Collaborator

Related: .showHelpAfterError() added in prerelease v8.0.0-2 by PR #1534

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

No branches or pull requests

5 participants