From 7d22e77cce369986db33eb3f03dbde82d4d6f62a Mon Sep 17 00:00:00 2001 From: Albert Nigmatzianov Date: Fri, 12 May 2017 20:22:26 +0200 Subject: [PATCH] Fix incorrect merge of pflag.CommandLine to cmd.Flags() Fix #443 --- cobra_test.go | 4 ++-- command.go | 2 +- command_test.go | 26 +++++++++++++++++++++----- 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/cobra_test.go b/cobra_test.go index 6f3ceb1..fcba209 100644 --- a/cobra_test.go +++ b/cobra_test.go @@ -1150,12 +1150,12 @@ func TestGlobalNormFuncPropagation(t *testing.T) { func TestFlagOnPflagCommandLine(t *testing.T) { flagName := "flagOnCommandLine" - pflag.CommandLine.String(flagName, "", "about my flag") + pflag.String(flagName, "", "about my flag") r := fullSetupTest("--help") checkResultContains(t, r, flagName) - // reset CommandLine flagset + // Reset pflag.CommandLine flagset. pflag.CommandLine = pflag.NewFlagSet(os.Args[0], pflag.ExitOnError) } diff --git a/command.go b/command.go index 9e6bdf3..3be5554 100644 --- a/command.go +++ b/command.go @@ -1275,8 +1275,8 @@ func (c *Command) Parent() *Command { // mergePersistentFlags merges c.PersistentFlags() to c.Flags() // and adds missing persistent flags of all parents. func (c *Command) mergePersistentFlags() { - c.Flags().AddFlagSet(c.PersistentFlags()) c.updateParentsPflags() + c.Flags().AddFlagSet(c.PersistentFlags()) c.Flags().AddFlagSet(c.parentsPflags) } diff --git a/command_test.go b/command_test.go index 24abd3e..978bacc 100644 --- a/command_test.go +++ b/command_test.go @@ -227,7 +227,7 @@ func TestFlagErrorFunc(t *testing.T) { // TestSortedFlags checks, // if cmd.LocalFlags() is unsorted when cmd.Flags().SortFlags set to false. -// https://github.com/spf13/cobra/issues/404 +// Related to https://github.com/spf13/cobra/issues/404. func TestSortedFlags(t *testing.T) { cmd := &Command{} cmd.Flags().SortFlags = false @@ -263,13 +263,13 @@ func isStringInStringSlice(s string, ss []string) bool { // TestHelpFlagInHelp checks, // if '--help' flag is shown in help for child (executing `parent help child`), // that has no other flags. -// https://github.com/spf13/cobra/issues/302 +// Related to https://github.com/spf13/cobra/issues/302. func TestHelpFlagInHelp(t *testing.T) { output := new(bytes.Buffer) - parent := &Command{Use: "parent", Long: "long", Run: func(*Command, []string) { return }} + parent := &Command{Use: "parent", Run: func(*Command, []string) {}} parent.SetOutput(output) - child := &Command{Use: "child", Long: "long", Run: func(*Command, []string) { return }} + child := &Command{Use: "child", Run: func(*Command, []string) {}} parent.AddCommand(child) parent.SetArgs([]string{"help", "child"}) @@ -279,6 +279,22 @@ func TestHelpFlagInHelp(t *testing.T) { } if !strings.Contains(output.String(), "[flags]") { - t.Fatalf("\nExpecting to contain: %v\nGot: %v", "[flags]", output.String()) + t.Errorf("\nExpecting to contain: %v\nGot: %v", "[flags]", output.String()) } } + +// TestMergeCommandLineToFlags checks, +// if pflag.CommandLine is correctly merged to c.Flags() after first call +// of c.mergePersistentFlags. +// Related to https://github.com/spf13/cobra/issues/443. +func TestMergeCommandLineToFlags(t *testing.T) { + pflag.Bool("boolflag", false, "") + c := &Command{Use: "c", Run: func(*Command, []string) {}} + c.mergePersistentFlags() + if c.Flags().Lookup("boolflag") == nil { + t.Fatal("Expecting to have flag from CommandLine in c.Flags()") + } + + // Reset pflag.CommandLine flagset. + pflag.CommandLine = pflag.NewFlagSet(os.Args[0], pflag.ExitOnError) +}