Skip to content
Myr

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

3 files+24 added−8 removed

modified:cobra_test.go+2 added-2 removed
1148 unmodified lines
1149}11501151func 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")11551156 checkResultContains(t, r, flagName)11571158 // reset CommandLine flagset1158 // Reset pflag.CommandLine flagset.1159 pflag.CommandLine = pflag.NewFlagSet(os.Args[0], pflag.ExitOnError)1160}11611162func TestAddTemplateFunctions(t *testing.T) {
46 unmodified lines
modified:command_test.go+21 added-5 removed
225 unmodified lines
226}227228// 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.231func TestSortedFlags(t *testing.T) {232 cmd := &Command{}233 cmd.Flags().SortFlags = false234 names := []string{"C", "B", "A", "D"}
27 unmodified lines
262263// 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.267func 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)271272 child := &Command{Use: "child", Long: "long", Run: func(*Command, []string) { return }}272 child := &Command{Use: "child", Run: func(*Command, []string) {}}273 parent.AddCommand(child)274275 parent.SetArgs([]string{"help", "child"})276 err := parent.Execute()
1 unmodified line
278 t.Fatal(err)279 }280281 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}285286// 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.290func 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 }297298 // Reset pflag.CommandLine flagset.299 pflag.CommandLine = pflag.NewFlagSet(os.Args[0], pflag.ExitOnError)300}
modified:command.go+1 added-1 removed
1273 unmodified lines
12741275// mergePersistentFlags merges c.PersistentFlags() to c.Flags()1276// and adds missing persistent flags of all parents.1277func (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}12821283// updateParentsPflags updates c.parentsPflags by adding
15 unmodified lines