package cobra import ( "bytes" "fmt" "os" "reflect" "strings" "testing" "github.com/spf13/pflag") func emptyRun(*Command, []string) {} func executeCommand(root *Command, args ...string) (output string, err error) { _, output, err = executeCommandC(root, args...) return output, err} func executeCommandC(root *Command, args ...string) (c *Command, output string, err error) { buf := new(bytes.Buffer) root.SetOutput(buf) root.SetArgs(args) c, err = root.ExecuteC() return c, buf.String(), err} func resetCommandLineFlagSet() { pflag.CommandLine = pflag.NewFlagSet(os.Args[0], pflag.ExitOnError)} func checkStringContains(t *testing.T, got, expected string) { if !strings.Contains(got, expected) { t.Errorf("Expected to contain: \n %v\nGot:\n %v\n", expected, got) }} func checkStringOmits(t *testing.T, got, expected string) { if strings.Contains(got, expected) { t.Errorf("Expected to not contain: \n %v\nGot: %v", expected, got) }} func TestSingleCommand(t *testing.T) { var rootCmdArgs []string rootCmd := &Command{ Use: "root", Args: ExactArgs(2), Run: func(_ *Command, args []string) { rootCmdArgs = args }, } aCmd := &Command{Use: "a", Args: NoArgs, Run: emptyRun} bCmd := &Command{Use: "b", Args: NoArgs, Run: emptyRun} rootCmd.AddCommand(aCmd, bCmd) output, err := executeCommand(rootCmd, "one", "two") if output != "" { t.Errorf("Unexpected output: %v", output) } if err != nil { t.Errorf("Unexpected error: %v", err) } got := strings.Join(rootCmdArgs, " ") expected := "one two" if got != expected { t.Errorf("rootCmdArgs expected: %q, got: %q", expected, got) }} func TestChildCommand(t *testing.T) { var child1CmdArgs []string rootCmd := &Command{Use: "root", Args: NoArgs, Run: emptyRun} child1Cmd := &Command{ Use: "child1", Args: ExactArgs(2), Run: func(_ *Command, args []string) { child1CmdArgs = args }, } child2Cmd := &Command{Use: "child2", Args: NoArgs, Run: emptyRun} rootCmd.AddCommand(child1Cmd, child2Cmd) output, err := executeCommand(rootCmd, "child1", "one", "two") if output != "" { t.Errorf("Unexpected output: %v", output) } if err != nil { t.Errorf("Unexpected error: %v", err) } got := strings.Join(child1CmdArgs, " ") expected := "one two" if got != expected { t.Errorf("child1CmdArgs expected: %q, got: %q", expected, got) }} func TestCallCommandWithoutSubcommands(t *testing.T) { rootCmd := &Command{Use: "root", Args: NoArgs, Run: emptyRun} _, err := executeCommand(rootCmd) if err != nil { t.Errorf("Calling command without subcommands should not have error: %v", err) }} func TestRootExecuteUnknownCommand(t *testing.T) { rootCmd := &Command{Use: "root", Run: emptyRun} rootCmd.AddCommand(&Command{Use: "child", Run: emptyRun}) output, _ := executeCommand(rootCmd, "unknown") expected := "Error: unknown command \"unknown\" for \"root\"\nRun 'root --help' for usage.\n" if output != expected { t.Errorf("Expected:\n %q\nGot:\n %q\n", expected, output) }} func TestSubcommandExecuteC(t *testing.T) { rootCmd := &Command{Use: "root", Run: emptyRun} childCmd := &Command{Use: "child", Run: emptyRun} rootCmd.AddCommand(childCmd) c, output, err := executeCommandC(rootCmd, "child") if output != "" { t.Errorf("Unexpected output: %v", output) } if err != nil { t.Errorf("Unexpected error: %v", err) } if c.Name() != "child" { t.Errorf(`invalid command returned from ExecuteC: expected "child"', got %q`, c.Name()) }} func TestRootUnknownCommandSilenced(t *testing.T) { rootCmd := &Command{Use: "root", Run: emptyRun} rootCmd.SilenceErrors = true rootCmd.SilenceUsage = true rootCmd.AddCommand(&Command{Use: "child", Run: emptyRun}) output, _ := executeCommand(rootCmd, "unknown") if output != "" { t.Errorf("Expected blank output, because of silenced usage.\nGot:\n %q\n", output) }} func TestCommandAlias(t *testing.T) { var timesCmdArgs []string rootCmd := &Command{Use: "root", Args: NoArgs, Run: emptyRun} echoCmd := &Command{ Use: "echo", Aliases: []string{"say", "tell"}, Args: NoArgs, Run: emptyRun, } timesCmd := &Command{ Use: "times", Args: ExactArgs(2), Run: func(_ *Command, args []string) { timesCmdArgs = args }, } echoCmd.AddCommand(timesCmd) rootCmd.AddCommand(echoCmd) output, err := executeCommand(rootCmd, "tell", "times", "one", "two") if output != "" { t.Errorf("Unexpected output: %v", output) } if err != nil { t.Errorf("Unexpected error: %v", err) } got := strings.Join(timesCmdArgs, " ") expected := "one two" if got != expected { t.Errorf("timesCmdArgs expected: %v, got: %v", expected, got) }} func TestEnablePrefixMatching(t *testing.T) { EnablePrefixMatching = true var aCmdArgs []string rootCmd := &Command{Use: "root", Args: NoArgs, Run: emptyRun} aCmd := &Command{ Use: "aCmd", Args: ExactArgs(2), Run: func(_ *Command, args []string) { aCmdArgs = args }, } bCmd := &Command{Use: "bCmd", Args: NoArgs, Run: emptyRun} rootCmd.AddCommand(aCmd, bCmd) output, err := executeCommand(rootCmd, "a", "one", "two") if output != "" { t.Errorf("Unexpected output: %v", output) } if err != nil { t.Errorf("Unexpected error: %v", err) } got := strings.Join(aCmdArgs, " ") expected := "one two" if got != expected { t.Errorf("aCmdArgs expected: %q, got: %q", expected, got) } EnablePrefixMatching = false} func TestAliasPrefixMatching(t *testing.T) { EnablePrefixMatching = true var timesCmdArgs []string rootCmd := &Command{Use: "root", Args: NoArgs, Run: emptyRun} echoCmd := &Command{ Use: "echo", Aliases: []string{"say", "tell"}, Args: NoArgs, Run: emptyRun, } timesCmd := &Command{ Use: "times", Args: ExactArgs(2), Run: func(_ *Command, args []string) { timesCmdArgs = args }, } echoCmd.AddCommand(timesCmd) rootCmd.AddCommand(echoCmd) output, err := executeCommand(rootCmd, "sa", "times", "one", "two") if output != "" { t.Errorf("Unexpected output: %v", output) } if err != nil { t.Errorf("Unexpected error: %v", err) } got := strings.Join(timesCmdArgs, " ") expected := "one two" if got != expected { t.Errorf("timesCmdArgs expected: %v, got: %v", expected, got) } EnablePrefixMatching = false} // TestChildSameName checks the correct behaviour of cobra in cases,// when an application with name "foo" and with subcommand "foo"// is executed with args "foo foo".func TestChildSameName(t *testing.T) { var fooCmdArgs []string rootCmd := &Command{Use: "foo", Args: NoArgs, Run: emptyRun} fooCmd := &Command{ Use: "foo", Args: ExactArgs(2), Run: func(_ *Command, args []string) { fooCmdArgs = args }, } barCmd := &Command{Use: "bar", Args: NoArgs, Run: emptyRun} rootCmd.AddCommand(fooCmd, barCmd) output, err := executeCommand(rootCmd, "foo", "one", "two") if output != "" { t.Errorf("Unexpected output: %v", output) } if err != nil { t.Errorf("Unexpected error: %v", err) } got := strings.Join(fooCmdArgs, " ") expected := "one two" if got != expected { t.Errorf("fooCmdArgs expected: %v, got: %v", expected, got) }} // TestGrandChildSameName checks the correct behaviour of cobra in cases,// when user has a root command and a grand child// with the same name.func TestGrandChildSameName(t *testing.T) { var fooCmdArgs []string rootCmd := &Command{Use: "foo", Args: NoArgs, Run: emptyRun} barCmd := &Command{Use: "bar", Args: NoArgs, Run: emptyRun} fooCmd := &Command{ Use: "foo", Args: ExactArgs(2), Run: func(_ *Command, args []string) { fooCmdArgs = args }, } barCmd.AddCommand(fooCmd) rootCmd.AddCommand(barCmd) output, err := executeCommand(rootCmd, "bar", "foo", "one", "two") if output != "" { t.Errorf("Unexpected output: %v", output) } if err != nil { t.Errorf("Unexpected error: %v", err) } got := strings.Join(fooCmdArgs, " ") expected := "one two" if got != expected { t.Errorf("fooCmdArgs expected: %v, got: %v", expected, got) }} func TestFlagLong(t *testing.T) { var cArgs []string c := &Command{ Use: "c", Args: ArbitraryArgs, Run: func(_ *Command, args []string) { cArgs = args }, } var intFlagValue int var stringFlagValue string c.Flags().IntVar(&intFlagValue, "intf", -1, "") c.Flags().StringVar(&stringFlagValue, "sf", "", "") output, err := executeCommand(c, "--intf=7", "--sf=abc", "one", "--", "two") if output != "" { t.Errorf("Unexpected output: %v", err) } if err != nil { t.Errorf("Unexpected error: %v", err) } if c.ArgsLenAtDash() != 1 { t.Errorf("Expected ArgsLenAtDash: %v but got %v", 1, c.ArgsLenAtDash()) } if intFlagValue != 7 { t.Errorf("Expected intFlagValue: %v, got %v", 7, intFlagValue) } if stringFlagValue != "abc" { t.Errorf("Expected stringFlagValue: %q, got %q", "abc", stringFlagValue) } got := strings.Join(cArgs, " ") expected := "one two" if got != expected { t.Errorf("Expected arguments: %q, got %q", expected, got) }} func TestFlagShort(t *testing.T) { var cArgs []string c := &Command{ Use: "c", Args: ArbitraryArgs, Run: func(_ *Command, args []string) { cArgs = args }, } var intFlagValue int var stringFlagValue string c.Flags().IntVarP(&intFlagValue, "intf", "i", -1, "") c.Flags().StringVarP(&stringFlagValue, "sf", "s", "", "") output, err := executeCommand(c, "-i", "7", "-sabc", "one", "two") if output != "" { t.Errorf("Unexpected output: %v", err) } if err != nil { t.Errorf("Unexpected error: %v", err) } if intFlagValue != 7 { t.Errorf("Expected flag value: %v, got %v", 7, intFlagValue) } if stringFlagValue != "abc" { t.Errorf("Expected stringFlagValue: %q, got %q", "abc", stringFlagValue) } got := strings.Join(cArgs, " ") expected := "one two" if got != expected { t.Errorf("Expected arguments: %q, got %q", expected, got) }} func TestChildFlag(t *testing.T) { rootCmd := &Command{Use: "root", Run: emptyRun} childCmd := &Command{Use: "child", Run: emptyRun} rootCmd.AddCommand(childCmd) var intFlagValue int childCmd.Flags().IntVarP(&intFlagValue, "intf", "i", -1, "") output, err := executeCommand(rootCmd, "child", "-i7") if output != "" { t.Errorf("Unexpected output: %v", err) } if err != nil { t.Errorf("Unexpected error: %v", err) } if intFlagValue != 7 { t.Errorf("Expected flag value: %v, got %v", 7, intFlagValue) }} func TestChildFlagWithParentLocalFlag(t *testing.T) { rootCmd := &Command{Use: "root", Run: emptyRun} childCmd := &Command{Use: "child", Run: emptyRun} rootCmd.AddCommand(childCmd) var intFlagValue int rootCmd.Flags().StringP("sf", "s", "", "") childCmd.Flags().IntVarP(&intFlagValue, "intf", "i", -1, "") _, err := executeCommand(rootCmd, "child", "-i7", "-sabc") if err == nil { t.Errorf("Invalid flag should generate error") } checkStringContains(t, err.Error(), "unknown shorthand") if intFlagValue != 7 { t.Errorf("Expected flag value: %v, got %v", 7, intFlagValue) }} func TestFlagInvalidInput(t *testing.T) { rootCmd := &Command{Use: "root", Run: emptyRun} rootCmd.Flags().IntP("intf", "i", -1, "") _, err := executeCommand(rootCmd, "-iabc") if err == nil { t.Errorf("Invalid flag value should generate error") } checkStringContains(t, err.Error(), "invalid syntax")} func TestFlagBeforeCommand(t *testing.T) { rootCmd := &Command{Use: "root", Run: emptyRun} childCmd := &Command{Use: "child", Run: emptyRun} rootCmd.AddCommand(childCmd) var flagValue int childCmd.Flags().IntVarP(&flagValue, "intf", "i", -1, "") // With short flag. _, err := executeCommand(rootCmd, "-i7", "child") if err != nil { t.Errorf("Unexpected error: %v", err) } if flagValue != 7 { t.Errorf("Expected flag value: %v, got %v", 7, flagValue) } // With long flag. _, err = executeCommand(rootCmd, "--intf=8", "child") if err != nil { t.Errorf("Unexpected error: %v", err) } if flagValue != 8 { t.Errorf("Expected flag value: %v, got %v", 9, flagValue) }} func TestStripFlags(t *testing.T) { tests := []struct { input []string output []string }{ { []string{"foo", "bar"}, []string{"foo", "bar"}, }, { []string{"foo", "--str", "-s"}, []string{"foo"}, }, { []string{"-s", "foo", "--str", "bar"}, []string{}, }, { []string{"-i10", "echo"}, []string{"echo"}, }, { []string{"-i=10", "echo"}, []string{"echo"}, }, { []string{"--int=100", "echo"}, []string{"echo"}, }, { []string{"-ib", "echo", "-sfoo", "baz"}, []string{"echo", "baz"}, }, { []string{"-i=baz", "bar", "-i", "foo", "blah"}, []string{"bar", "blah"}, }, { []string{"--int=baz", "-sbar", "-i", "foo", "blah"}, []string{"blah"},Lines 1–500 of 1733. Load more
package cobra import ( "bytes" "fmt" "os" "reflect" "strings" "testing" "github.com/spf13/pflag" ) func emptyRun(*Command, []string) {} func executeCommand(root *Command, args ...string) (output string, err error) { _, output, err = executeCommandC(root, args...) return output, err } func executeCommandC(root *Command, args ...string) (c *Command, output string, err error) { buf := new(bytes.Buffer) root.SetOutput(buf) root.SetArgs(args) c, err = root.ExecuteC() return c, buf.String(), err } func resetCommandLineFlagSet() { pflag.CommandLine = pflag.NewFlagSet(os.Args[0], pflag.ExitOnError) } func checkStringContains(t *testing.T, got, expected string) { if !strings.Contains(got, expected) { t.Errorf("Expected to contain: \n %v\nGot:\n %v\n", expected, got) } } func checkStringOmits(t *testing.T, got, expected string) { if strings.Contains(got, expected) { t.Errorf("Expected to not contain: \n %v\nGot: %v", expected, got) } } func TestSingleCommand(t *testing.T) { var rootCmdArgs []string rootCmd := &Command{ Use: "root", Args: ExactArgs(2), Run: func(_ *Command, args []string) { rootCmdArgs = args }, } aCmd := &Command{Use: "a", Args: NoArgs, Run: emptyRun} bCmd := &Command{Use: "b", Args: NoArgs, Run: emptyRun} rootCmd.AddCommand(aCmd, bCmd) output, err := executeCommand(rootCmd, "one", "two") if output != "" { t.Errorf("Unexpected output: %v", output) } if err != nil { t.Errorf("Unexpected error: %v", err) } got := strings.Join(rootCmdArgs, " ") expected := "one two" if got != expected { t.Errorf("rootCmdArgs expected: %q, got: %q", expected, got) } } func TestChildCommand(t *testing.T) { var child1CmdArgs []string rootCmd := &Command{Use: "root", Args: NoArgs, Run: emptyRun} child1Cmd := &Command{ Use: "child1", Args: ExactArgs(2), Run: func(_ *Command, args []string) { child1CmdArgs = args }, } child2Cmd := &Command{Use: "child2", Args: NoArgs, Run: emptyRun} rootCmd.AddCommand(child1Cmd, child2Cmd) output, err := executeCommand(rootCmd, "child1", "one", "two") if output != "" { t.Errorf("Unexpected output: %v", output) } if err != nil { t.Errorf("Unexpected error: %v", err) } got := strings.Join(child1CmdArgs, " ") expected := "one two" if got != expected { t.Errorf("child1CmdArgs expected: %q, got: %q", expected, got) } } func TestCallCommandWithoutSubcommands(t *testing.T) { rootCmd := &Command{Use: "root", Args: NoArgs, Run: emptyRun} _, err := executeCommand(rootCmd) if err != nil { t.Errorf("Calling command without subcommands should not have error: %v", err) } } func TestRootExecuteUnknownCommand(t *testing.T) { rootCmd := &Command{Use: "root", Run: emptyRun} rootCmd.AddCommand(&Command{Use: "child", Run: emptyRun}) output, _ := executeCommand(rootCmd, "unknown") expected := "Error: unknown command \"unknown\" for \"root\"\nRun 'root --help' for usage.\n" if output != expected { t.Errorf("Expected:\n %q\nGot:\n %q\n", expected, output) } } func TestSubcommandExecuteC(t *testing.T) { rootCmd := &Command{Use: "root", Run: emptyRun} childCmd := &Command{Use: "child", Run: emptyRun} rootCmd.AddCommand(childCmd) c, output, err := executeCommandC(rootCmd, "child") if output != "" { t.Errorf("Unexpected output: %v", output) } if err != nil { t.Errorf("Unexpected error: %v", err) } if c.Name() != "child" { t.Errorf(`invalid command returned from ExecuteC: expected "child"', got %q`, c.Name()) } } func TestRootUnknownCommandSilenced(t *testing.T) { rootCmd := &Command{Use: "root", Run: emptyRun} rootCmd.SilenceErrors = true rootCmd.SilenceUsage = true rootCmd.AddCommand(&Command{Use: "child", Run: emptyRun}) output, _ := executeCommand(rootCmd, "unknown") if output != "" { t.Errorf("Expected blank output, because of silenced usage.\nGot:\n %q\n", output) } } func TestCommandAlias(t *testing.T) { var timesCmdArgs []string rootCmd := &Command{Use: "root", Args: NoArgs, Run: emptyRun} echoCmd := &Command{ Use: "echo", Aliases: []string{"say", "tell"}, Args: NoArgs, Run: emptyRun, } timesCmd := &Command{ Use: "times", Args: ExactArgs(2), Run: func(_ *Command, args []string) { timesCmdArgs = args }, } echoCmd.AddCommand(timesCmd) rootCmd.AddCommand(echoCmd) output, err := executeCommand(rootCmd, "tell", "times", "one", "two") if output != "" { t.Errorf("Unexpected output: %v", output) } if err != nil { t.Errorf("Unexpected error: %v", err) } got := strings.Join(timesCmdArgs, " ") expected := "one two" if got != expected { t.Errorf("timesCmdArgs expected: %v, got: %v", expected, got) } } func TestEnablePrefixMatching(t *testing.T) { EnablePrefixMatching = true var aCmdArgs []string rootCmd := &Command{Use: "root", Args: NoArgs, Run: emptyRun} aCmd := &Command{ Use: "aCmd", Args: ExactArgs(2), Run: func(_ *Command, args []string) { aCmdArgs = args }, } bCmd := &Command{Use: "bCmd", Args: NoArgs, Run: emptyRun} rootCmd.AddCommand(aCmd, bCmd) output, err := executeCommand(rootCmd, "a", "one", "two") if output != "" { t.Errorf("Unexpected output: %v", output) } if err != nil { t.Errorf("Unexpected error: %v", err) } got := strings.Join(aCmdArgs, " ") expected := "one two" if got != expected { t.Errorf("aCmdArgs expected: %q, got: %q", expected, got) } EnablePrefixMatching = false } func TestAliasPrefixMatching(t *testing.T) { EnablePrefixMatching = true var timesCmdArgs []string rootCmd := &Command{Use: "root", Args: NoArgs, Run: emptyRun} echoCmd := &Command{ Use: "echo", Aliases: []string{"say", "tell"}, Args: NoArgs, Run: emptyRun, } timesCmd := &Command{ Use: "times", Args: ExactArgs(2), Run: func(_ *Command, args []string) { timesCmdArgs = args }, } echoCmd.AddCommand(timesCmd) rootCmd.AddCommand(echoCmd) output, err := executeCommand(rootCmd, "sa", "times", "one", "two") if output != "" { t.Errorf("Unexpected output: %v", output) } if err != nil { t.Errorf("Unexpected error: %v", err) } got := strings.Join(timesCmdArgs, " ") expected := "one two" if got != expected { t.Errorf("timesCmdArgs expected: %v, got: %v", expected, got) } EnablePrefixMatching = false } // TestChildSameName checks the correct behaviour of cobra in cases, // when an application with name "foo" and with subcommand "foo" // is executed with args "foo foo". func TestChildSameName(t *testing.T) { var fooCmdArgs []string rootCmd := &Command{Use: "foo", Args: NoArgs, Run: emptyRun} fooCmd := &Command{ Use: "foo", Args: ExactArgs(2), Run: func(_ *Command, args []string) { fooCmdArgs = args }, } barCmd := &Command{Use: "bar", Args: NoArgs, Run: emptyRun} rootCmd.AddCommand(fooCmd, barCmd) output, err := executeCommand(rootCmd, "foo", "one", "two") if output != "" { t.Errorf("Unexpected output: %v", output) } if err != nil { t.Errorf("Unexpected error: %v", err) } got := strings.Join(fooCmdArgs, " ") expected := "one two" if got != expected { t.Errorf("fooCmdArgs expected: %v, got: %v", expected, got) } } // TestGrandChildSameName checks the correct behaviour of cobra in cases, // when user has a root command and a grand child // with the same name. func TestGrandChildSameName(t *testing.T) { var fooCmdArgs []string rootCmd := &Command{Use: "foo", Args: NoArgs, Run: emptyRun} barCmd := &Command{Use: "bar", Args: NoArgs, Run: emptyRun} fooCmd := &Command{ Use: "foo", Args: ExactArgs(2), Run: func(_ *Command, args []string) { fooCmdArgs = args }, } barCmd.AddCommand(fooCmd) rootCmd.AddCommand(barCmd) output, err := executeCommand(rootCmd, "bar", "foo", "one", "two") if output != "" { t.Errorf("Unexpected output: %v", output) } if err != nil { t.Errorf("Unexpected error: %v", err) } got := strings.Join(fooCmdArgs, " ") expected := "one two" if got != expected { t.Errorf("fooCmdArgs expected: %v, got: %v", expected, got) } } func TestFlagLong(t *testing.T) { var cArgs []string c := &Command{ Use: "c", Args: ArbitraryArgs, Run: func(_ *Command, args []string) { cArgs = args }, } var intFlagValue int var stringFlagValue string c.Flags().IntVar(&intFlagValue, "intf", -1, "") c.Flags().StringVar(&stringFlagValue, "sf", "", "") output, err := executeCommand(c, "--intf=7", "--sf=abc", "one", "--", "two") if output != "" { t.Errorf("Unexpected output: %v", err) } if err != nil { t.Errorf("Unexpected error: %v", err) } if c.ArgsLenAtDash() != 1 { t.Errorf("Expected ArgsLenAtDash: %v but got %v", 1, c.ArgsLenAtDash()) } if intFlagValue != 7 { t.Errorf("Expected intFlagValue: %v, got %v", 7, intFlagValue) } if stringFlagValue != "abc" { t.Errorf("Expected stringFlagValue: %q, got %q", "abc", stringFlagValue) } got := strings.Join(cArgs, " ") expected := "one two" if got != expected { t.Errorf("Expected arguments: %q, got %q", expected, got) } } func TestFlagShort(t *testing.T) { var cArgs []string c := &Command{ Use: "c", Args: ArbitraryArgs, Run: func(_ *Command, args []string) { cArgs = args }, } var intFlagValue int var stringFlagValue string c.Flags().IntVarP(&intFlagValue, "intf", "i", -1, "") c.Flags().StringVarP(&stringFlagValue, "sf", "s", "", "") output, err := executeCommand(c, "-i", "7", "-sabc", "one", "two") if output != "" { t.Errorf("Unexpected output: %v", err) } if err != nil { t.Errorf("Unexpected error: %v", err) } if intFlagValue != 7 { t.Errorf("Expected flag value: %v, got %v", 7, intFlagValue) } if stringFlagValue != "abc" { t.Errorf("Expected stringFlagValue: %q, got %q", "abc", stringFlagValue) } got := strings.Join(cArgs, " ") expected := "one two" if got != expected { t.Errorf("Expected arguments: %q, got %q", expected, got) } } func TestChildFlag(t *testing.T) { rootCmd := &Command{Use: "root", Run: emptyRun} childCmd := &Command{Use: "child", Run: emptyRun} rootCmd.AddCommand(childCmd) var intFlagValue int childCmd.Flags().IntVarP(&intFlagValue, "intf", "i", -1, "") output, err := executeCommand(rootCmd, "child", "-i7") if output != "" { t.Errorf("Unexpected output: %v", err) } if err != nil { t.Errorf("Unexpected error: %v", err) } if intFlagValue != 7 { t.Errorf("Expected flag value: %v, got %v", 7, intFlagValue) } } func TestChildFlagWithParentLocalFlag(t *testing.T) { rootCmd := &Command{Use: "root", Run: emptyRun} childCmd := &Command{Use: "child", Run: emptyRun} rootCmd.AddCommand(childCmd) var intFlagValue int rootCmd.Flags().StringP("sf", "s", "", "") childCmd.Flags().IntVarP(&intFlagValue, "intf", "i", -1, "") _, err := executeCommand(rootCmd, "child", "-i7", "-sabc") if err == nil { t.Errorf("Invalid flag should generate error") } checkStringContains(t, err.Error(), "unknown shorthand") if intFlagValue != 7 { t.Errorf("Expected flag value: %v, got %v", 7, intFlagValue) } } func TestFlagInvalidInput(t *testing.T) { rootCmd := &Command{Use: "root", Run: emptyRun} rootCmd.Flags().IntP("intf", "i", -1, "") _, err := executeCommand(rootCmd, "-iabc") if err == nil { t.Errorf("Invalid flag value should generate error") } checkStringContains(t, err.Error(), "invalid syntax") } func TestFlagBeforeCommand(t *testing.T) { rootCmd := &Command{Use: "root", Run: emptyRun} childCmd := &Command{Use: "child", Run: emptyRun} rootCmd.AddCommand(childCmd) var flagValue int childCmd.Flags().IntVarP(&flagValue, "intf", "i", -1, "") // With short flag. _, err := executeCommand(rootCmd, "-i7", "child") if err != nil { t.Errorf("Unexpected error: %v", err) } if flagValue != 7 { t.Errorf("Expected flag value: %v, got %v", 7, flagValue) } // With long flag. _, err = executeCommand(rootCmd, "--intf=8", "child") if err != nil { t.Errorf("Unexpected error: %v", err) } if flagValue != 8 { t.Errorf("Expected flag value: %v, got %v", 9, flagValue) } } func TestStripFlags(t *testing.T) { tests := []struct { input []string output []string }{ { []string{"foo", "bar"}, []string{"foo", "bar"}, }, { []string{"foo", "--str", "-s"}, []string{"foo"}, }, { []string{"-s", "foo", "--str", "bar"}, []string{}, }, { []string{"-i10", "echo"}, []string{"echo"}, }, { []string{"-i=10", "echo"}, []string{"echo"}, }, { []string{"--int=100", "echo"}, []string{"echo"}, }, { []string{"-ib", "echo", "-sfoo", "baz"}, []string{"echo", "baz"}, }, { []string{"-i=baz", "bar", "-i", "foo", "blah"}, []string{"bar", "blah"}, }, { []string{"--int=baz", "-sbar", "-i", "foo", "blah"}, []string{"blah"},