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

Cobra won't show help if cmd is not Runnable #790

Closed
retpolanne opened this issue Nov 19, 2018 · 3 comments
Closed

Cobra won't show help if cmd is not Runnable #790

retpolanne opened this issue Nov 19, 2018 · 3 comments

Comments

@retpolanne
Copy link

I have a subcommand that has no Run function.

var containerCmd = &cobra.Command{
	Use:   "container",
	Short: "Global settings for running containers",
}

func init() {
	configCmd.AddCommand(containerCmd)
	containerCmd.PersistentFlags().String("networkEndpoint", "", "Specify a network endpoint (in case you need to network between containers)")
	viper.BindPFlag("networkEndpoint", containerCmd.PersistentFlags().Lookup("networkEndpoint"))
}
...

It only shows the usage when it's added to the rootCmd. When it is not, it only shows the short description. It won't show the help even if I pass the --help flag. It should show the usage when I pass the help flag or no arguments.

I noticed two things:

  1. Cobra returns "help requested" if the command is not runnable

    cobra/command.go

    Lines 724 to 726 in fe5e611

    if !c.Runnable() {
    return flag.ErrHelp
    }
  2. Cobra only renders the Usage if the command is runnable

    cobra/command.go

    Lines 432 to 434 in fe5e611

    return `{{with (or .Long .Short)}}{{. | trimTrailingWhitespaces}}
    {{end}}{{if or .Runnable .HasSubCommands}}{{.UsageString}}{{end}}`

I added HasFlags to this template and it always displays the usage.

It might be related to #206 (comment)

@umarcor
Copy link
Contributor

umarcor commented Mar 21, 2019

Hi @vinicyusmacedo! In #842 the management of errors from non-runnable commands is modified. Please, have look, as it may suit your use case.

@retpolanne
Copy link
Author

@umarcor hi, sorry for taking too long.

I believe it should help, though I'm not sure if the Help template will render in this case.

@github-actions
Copy link

github-actions bot commented Apr 7, 2020

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

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

Successfully merging a pull request may close this issue.

2 participants