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

DisableFlagParsing should disable creation of the help flag? #1328

Closed
watk opened this issue Feb 2, 2021 · 4 comments
Closed

DisableFlagParsing should disable creation of the help flag? #1328

watk opened this issue Feb 2, 2021 · 4 comments
Labels
area/flags-args Changes to functionality around command line flags and args lifecycle/needs-pr Ready for a PR from the community

Comments

@watk
Copy link

watk commented Feb 2, 2021

I expected this to be the default behaviour but maybe there's a good argument for how it is today.

If you add a command with DisableFlagParsing, then help cmd will list -h as a flag, but running cmd -h will not print the help, it'll invoke Run() with ['-h']. If it's the command's responsibility to parse all of the flags, I think it makes sense to not add flags automatically.

Here is a simplified example:

package main
import "github.com/spf13/cobra"
func main() {
	cmd := &cobra.Command{
		Use:              "root",
		TraverseChildren: true,
	}
	sub := &cobra.Command{
		Use:                "sub",
		DisableFlagParsing: true,
		Run:                func(cmd *cobra.Command, args []string) {},
	}
	cmd.AddCommand(sub)
	cmd.SetArgs([]string{"help", "sub"})
	cmd.Execute()
}

The output is:

Usage:
  root sub [flags]

Flags:
  -h, --help   help for sub

Is there any better way to disable this help flag than this?

	sub.InitDefaultHelpFlag()
	sub.Flags().MarkHidden("help")
@github-actions
Copy link

github-actions bot commented Apr 4, 2021

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

@johnSchnake
Copy link
Collaborator

That is interesting. Thanks for showing a simple workaround for now.

I don't personally know of a reason you'd want to keep the help flag defined there if parsing is disabled. I'd be happy to review a PR if you (or someone else) can make the narrow change.

@johnSchnake johnSchnake added area/flags-args Changes to functionality around command line flags and args lifecycle/needs-pr Ready for a PR from the community labels Mar 18, 2022
@watk
Copy link
Author

watk commented Apr 6, 2022

Thanks, I'm no longer using cobra so feel free to close this given that there's a simple workaround anyway.

@hanoii
Copy link

hanoii commented Oct 11, 2022

I would like an option to disable the help command instantiation for specific commands. We execute other commands inside a container and I like those commands to receive --help instead of the cobra command.

It's not exactly the same issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/flags-args Changes to functionality around command line flags and args lifecycle/needs-pr Ready for a PR from the community
Projects
None yet
Development

No branches or pull requests

3 participants