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

Display --help Suggestion Every Time an Error Occurs #950

Closed
danielhelfand opened this issue Sep 6, 2019 · 4 comments
Closed

Display --help Suggestion Every Time an Error Occurs #950

danielhelfand opened this issue Sep 6, 2019 · 4 comments

Comments

@danielhelfand
Copy link

danielhelfand commented Sep 6, 2019

I am wondering what the best practice would be for displaying a suggestion to run --help would be after every error message without displaying usage. I would like it for the suggestion to be specific to the root command and subcommands ran.

An example would be that a user types a wrong command or misuses a command. The resulting message would be:

Run rootcommand subcommand --help for usage.

@umarcor
Copy link
Contributor

umarcor commented Sep 6, 2019

Hi @danielhelfand! This is partially addressed in #841 and #842 (draft). Precisely, error messages are enhanced to properly identify the following contexts:

"invalid":    `invalid argument "a" for "c"`,
"unknown":    `unknown command "one" for "c"`,
"less":       "requires at least 2 arg(s), only received 1",
"more":       "accepts at most 2 arg(s), received 3",
"notexact":   "accepts 2 arg(s), received 3",
"notinrange": "accepts between 2 and 4 arg(s), received 1",

Then, in #842 generation of help/usage is reworked. See function helpHint.

It'd be great if you could have a look or try them and suggest any enhancement.

@danielhelfand
Copy link
Author

@umarcor Thanks for all this information! I'll take a look and keep up with these updates. This looks great so far though. Glad to see this is being worked on.

@github-actions
Copy link

github-actions bot commented Apr 4, 2020

This issue is being marked as stale due to a long period of inactivity

@johnSchnake
Copy link
Collaborator

If you want a custom usage message you can achieve that as well; you could gather the parent commands names by just walking up the command tree too:

	cmd.SetUsageFunc(func(c *cobra.Command) error {
		current := c
		cmds := []string{c.Name()}
		for{
			if current.HasParent(){
				current= c.Parent()
			}else{
				break
			}
			cmds = append(cmds, current.Name())
		}
		fmt.Printf(`Try "%v --help" for more info`,strings.Join(cmds, " ") )
		return nil
	},

Just one way to do it.

Based on time/activity I'm going to close as motivated by #1600. Reopen if you feel something else needs done here.

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

3 participants