Fix incorrect merge of pflag.CommandLine to cmd.Flags()
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Fix #443
authored by Albert Nigmatzianov · committed by Albert Nigmatzianov · parent 3454e0e · Browse files · 3 files·+24 added−8 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+24 added−8 removed
- other:+24 added−8 removed
Provenance
- Commit
7d22e77- Tree
149ba94- Author
Albert Nigmatzianov <albertnigma@gmail.com>- Committer
Albert Nigmatzianov <albertnigma@gmail.com>- Parent
3454e0e
3 files changed+24 added−8 removed
modified:cobra_test.go+2 added-2 removed
1148 unmodified lines1149 }1150 1151 func TestFlagOnPflagCommandLine(t *testing.T) {1152 flagName := "flagOnCommandLine"1153- pflag.CommandLine.String(flagName, "", "about my flag")1153+ pflag.String(flagName, "", "about my flag")1154 r := fullSetupTest("--help")1155 1156 checkResultContains(t, r, flagName)1157 1158- // reset CommandLine flagset1158+ // Reset pflag.CommandLine flagset.1159 pflag.CommandLine = pflag.NewFlagSet(os.Args[0], pflag.ExitOnError)1160 }1161 1162 func TestAddTemplateFunctions(t *testing.T) {46 unmodified linesmodified:command_test.go+21 added-5 removed
225 unmodified lines226 }227 228 // TestSortedFlags checks,229 // if cmd.LocalFlags() is unsorted when cmd.Flags().SortFlags set to false.230-// https://github.com/spf13/cobra/issues/404230+// Related to https://github.com/spf13/cobra/issues/404.231 func TestSortedFlags(t *testing.T) {232 cmd := &Command{}233 cmd.Flags().SortFlags = false234 names := []string{"C", "B", "A", "D"}27 unmodified lines262 263 // TestHelpFlagInHelp checks,264 // if '--help' flag is shown in help for child (executing `parent help child`),265 // that has no other flags.266-// https://github.com/spf13/cobra/issues/302266+// Related to https://github.com/spf13/cobra/issues/302.267 func TestHelpFlagInHelp(t *testing.T) {268 output := new(bytes.Buffer)269- parent := &Command{Use: "parent", Long: "long", Run: func(*Command, []string) { return }}269+ parent := &Command{Use: "parent", Run: func(*Command, []string) {}}270 parent.SetOutput(output)271 272- child := &Command{Use: "child", Long: "long", Run: func(*Command, []string) { return }}272+ child := &Command{Use: "child", Run: func(*Command, []string) {}}273 parent.AddCommand(child)274 275 parent.SetArgs([]string{"help", "child"})276 err := parent.Execute()1 unmodified line278 t.Fatal(err)279 }280 281 if !strings.Contains(output.String(), "[flags]") {282- t.Fatalf("\nExpecting to contain: %v\nGot: %v", "[flags]", output.String())282+ t.Errorf("\nExpecting to contain: %v\nGot: %v", "[flags]", output.String())283 }284 }285+286+// TestMergeCommandLineToFlags checks,287+// if pflag.CommandLine is correctly merged to c.Flags() after first call288+// of c.mergePersistentFlags.289+// Related to https://github.com/spf13/cobra/issues/443.290+func TestMergeCommandLineToFlags(t *testing.T) {291+ pflag.Bool("boolflag", false, "")292+ c := &Command{Use: "c", Run: func(*Command, []string) {}}293+ c.mergePersistentFlags()294+ if c.Flags().Lookup("boolflag") == nil {295+ t.Fatal("Expecting to have flag from CommandLine in c.Flags()")296+ }297+298+ // Reset pflag.CommandLine flagset.299+ pflag.CommandLine = pflag.NewFlagSet(os.Args[0], pflag.ExitOnError)300+}modified:command.go+1 added-1 removed
1273 unmodified lines1274 1275 // mergePersistentFlags merges c.PersistentFlags() to c.Flags()1276 // and adds missing persistent flags of all parents.1277 func (c *Command) mergePersistentFlags() {1278- c.Flags().AddFlagSet(c.PersistentFlags())1278 c.updateParentsPflags()1279+ c.Flags().AddFlagSet(c.PersistentFlags())1280 c.Flags().AddFlagSet(c.parentsPflags)1281 }1282 1283 // updateParentsPflags updates c.parentsPflags by adding15 unmodified lines