mirror of
https://github.com/spf13/cobra
synced 2024-11-24 22:57:12 +00:00
Merge pull request #286 from dnephin/merge_flags_before_adding_help
Merge persistent flags before checking for a help flag
This commit is contained in:
commit
ec2fe78599
2 changed files with 16 additions and 0 deletions
|
@ -735,6 +735,7 @@ func (c *Command) ExecuteC() (cmd *Command, err error) {
|
|||
}
|
||||
|
||||
func (c *Command) initHelpFlag() {
|
||||
c.mergePersistentFlags()
|
||||
if c.Flags().Lookup("help") == nil {
|
||||
c.Flags().BoolP("help", "h", false, "help for "+c.Name())
|
||||
}
|
||||
|
|
|
@ -136,6 +136,21 @@ func Test_DisableFlagParsing(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestInitHelpFlagMergesFlags(t *testing.T) {
|
||||
usage := "custom flag"
|
||||
baseCmd := Command{Use: "testcmd"}
|
||||
baseCmd.PersistentFlags().Bool("help", false, usage)
|
||||
cmd := Command{Use: "do"}
|
||||
baseCmd.AddCommand(&cmd)
|
||||
|
||||
cmd.initHelpFlag()
|
||||
actual := cmd.Flags().Lookup("help").Usage
|
||||
if actual != usage {
|
||||
t.Fatalf("Expected the help flag from the base command with usage '%s', " +
|
||||
"but got the default with usage '%s'", usage, actual)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCommandsAreSorted(t *testing.T) {
|
||||
EnableCommandSorting = true
|
||||
|
||||
|
|
Loading…
Reference in a new issue