Skip to content

DisableFlagParsing should disable creation of the help flag? #1328

@watk

Description

@watk

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")

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/flags-argsChanges to functionality around command line flags and argslifecycle/needs-prReady for a PR from the community

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions