Cobra: Add ability to remove "help" subcommand

Created on 24 Nov 2017  路  7Comments  路  Source: spf13/cobra

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.

  • You can't simply remove the command, because InitDefaultHelpCmd will re-add it
  • Using SetHelpCommand to set the command to nil doesn't work, because InitDefaultHelpCmd will instantiate the default help command when the command is nil

The 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.

kinstale

Most helpful comment

you may use the following code:

    rootCmd.SetHelpCommand(&cobra.Command{
        Use:    "no-help",
        Hidden: true,
    })

it works for me

All 7 comments

Possible approaches:

  • Add flag/setting to Command that, when set to true, does not add Help command
  • Add a private var 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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

andygrunwald picture andygrunwald  路  6Comments

diwakergupta picture diwakergupta  路  5Comments

anuraaga picture anuraaga  路  5Comments

alapidas picture alapidas  路  3Comments

pjbgf picture pjbgf  路  5Comments