Skip to content
Myr

Merge pull request #60 from dohzya/remove-command

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Add Command's RemoveCommand method

authored by Eric Paris · committed by Eric Paris · parents bbc5177, 5b56110 · Browse files · 2 files+61 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+61 added−0 removed

  • other:+61 added−0 removed

2 files+61 added−0 removed

modified:cobra_test.go+52 added
15 unmodified lines
16var flags1, flags2a, flags2b, flags3 string17var flagi1, flagi2, flagi3, flagir int18var globalFlag1 bool19var flagEcho, rootcalled bool20var versionUsed int2122const strtwoParentHelp = "help message for parent flag strtwo"23const strtwoChildHelp = "help message for child flag strtwo"24
45 unmodified lines
70 rootcalled = true71 },72}7374var cmdVersion1 = &Command{75 Use: "version",76 Short: "Print the version number",77 Long: `First version of the version command`,78 Run: func(cmd *Command, args []string) {79 versionUsed = 180 },81}8283var cmdVersion2 = &Command{84 Use: "version",85 Short: "Print the version number",86 Long: `Second version of the version command`,87 Run: func(cmd *Command, args []string) {88 versionUsed = 289 },90}9192func flagInit() {93 cmdEcho.ResetFlags()94 cmdPrint.ResetFlags()95 cmdTimes.ResetFlags()
10 unmodified lines
106 cmdPrint.PersistentFlags().StringVarP(&flags3, "strthree", "s", "three", "help message for flag strthree")107 cmdEcho.Flags().BoolVarP(&flagb1, "boolone", "b", true, "help message for flag boolone")108 cmdTimes.Flags().BoolVarP(&flagb2, "booltwo", "c", false, "help message for flag booltwo")109 cmdPrint.Flags().BoolVarP(&flagb3, "boolthree", "b", true, "help message for flag boolthree")110 cmdVersion1.ResetFlags()111 cmdVersion2.ResetFlags()112}113114func commandInit() {115 cmdEcho.ResetCommands()
601 unmodified lines
717 t.Errorf("Valid Input shouldn't have errors, got:\n %q", x.Error)718 }719720}721722func TestRemoveCommand(t *testing.T) {723 versionUsed = 0724 c := initializeWithRootCmd()725 c.AddCommand(cmdVersion1)726 c.RemoveCommand(cmdVersion1)727 x := fullTester(c, "version")728 if x.Error == nil {729 t.Errorf("Removed command should not have been called\n")730 return731 }732}733734func TestReplaceCommandWithRemove(t *testing.T) {735 versionUsed = 0736 c := initializeWithRootCmd()737 c.AddCommand(cmdVersion1)738 c.RemoveCommand(cmdVersion1)739 c.AddCommand(cmdVersion2)740 x := fullTester(c, "version")741 if x.Error != nil {742 t.Errorf("Valid Input shouldn't have errors, got:\n %q", x.Error)743 return744 }745 if versionUsed == 1 {746 t.Errorf("Removed command shouldn't be called\n")747 }748 if versionUsed != 2 {749 t.Errorf("Replacing command should have been called but didn't\n")750 }751}
modified:README.md+9 added
141 unmodified lines
142A flag can also be assigned locally which will only apply to that specific command.143144 HugoCmd.Flags().StringVarP(&Source, "source", "s", "", "Source directory to read from")145146### Remove a command from its parent147148Removing a command is not a common action is simple program but it allows 3rd parties to customize an existing command tree.149150In this exemple, we remove the existing `VersionCmd` command of an existing root command, and we replace it by our own version.151152 mainlib.RootCmd.RemoveCommand(mainlib.VersionCmd)153 mainlib.RootCmd.AddCommand(versionCmd)154155### Once all commands and flags are defined, Execute the commands156157Execute should be run on the root for clarity, though it can be called on any command.158
250 unmodified lines