Currently, if an application has any subcommands, a help subcommand is automatically added. There may be applications that don't want the help subcommand, but as it currently stands there is no easy way to disable it.
InitDefaultHelpCmd will re-add itSetHelpCommand to set the command to nil doesn't work, because InitDefaultHelpCmd will instantiate the default help command when the command is nilThe best I have come up with is something like:
rootCmd.SetHelpCommand(&cobra.Command{
Use: "help",
Hidden: true,
RunE: func(cmd *cobra.Command, args []string) error {
return fmt.Errorf(`unknown command "help" for "%s"`, cmd.Root().Name())
},
})
but even this isn't perfect because it goes down a different error path since the subcommand is actually still executed.
I think it would be nice to have some mechanism to disable the help command -- even if it's not an explicit flag or setting, having some way to remove this subcommand would be nice.
Possible approaches:
Command that, when set to true, does not add Help commandvar noHelpCommand = &Command{} to the package and add a DisableHelp function to Command. When called, it sets helpCommand to be noHelpCommand. In InitDefaultHelpCmd, if c.helpCommand == noHelpCommand, then it does not add any help.Being able to decide whether you want a help command or help flags on individual commands would be great. Having both by default is kind of nice but I generally prefer having one or the other.
@nmiyake Instead of having a negative set to true, let's have a positive (true by default) set to false when needed. What about just having a Help setting to Command that by default is true and that you can set to false to prevent the help flag?
you may use the following code:
rootCmd.SetHelpCommand(&cobra.Command{
Use: "no-help",
Hidden: true,
})
it works for me
PR #864 implements option (1) from https://github.com/spf13/cobra/issues/587#issuecomment-346922307
@rawtaz unfortunately Go does not provide a way to have a boolean struct member default to true. All boolean values must have a default value of false their "zero value" (see https://tour.golang.org/basics/12)
This issue is being marked as stale due to a long period of inactivity
It works for me.
seeting
DisableFlagsInUseLine: true
Most helpful comment
you may use the following code:
it works for me