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
Provenance
- Commit
0ea881c- Tree
6f9cf71- Author
Eric Paris <eparis@redhat.com>- Committer
Eric Paris <eparis@redhat.com>- Parent 1
bbc5177- Parent 2
5b56110
2 files changed+61 added−0 removed
modified:cobra_test.go+52 added
15 unmodified lines16 var flags1, flags2a, flags2b, flags3 string17 var flagi1, flagi2, flagi3, flagir int18 var globalFlag1 bool19 var flagEcho, rootcalled bool20+var versionUsed int21 22 const strtwoParentHelp = "help message for parent flag strtwo"23 const strtwoChildHelp = "help message for child flag strtwo"24 45 unmodified lines70 rootcalled = true71 },72 }73 74+var 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+}82+83+var 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+}91+92 func flagInit() {93 cmdEcho.ResetFlags()94 cmdPrint.ResetFlags()95 cmdTimes.ResetFlags()10 unmodified lines106 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 }113 114 func commandInit() {115 cmdEcho.ResetCommands()601 unmodified lines717 t.Errorf("Valid Input shouldn't have errors, got:\n %q", x.Error)718 }719 720 }721+722+func 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+}733+734+func 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 lines142 A flag can also be assigned locally which will only apply to that specific command.143 144 HugoCmd.Flags().StringVarP(&Source, "source", "s", "", "Source directory to read from")145 146+### Remove a command from its parent147+148+Removing a command is not a common action is simple program but it allows 3rd parties to customize an existing command tree.149+150+In this exemple, we remove the existing `VersionCmd` command of an existing root command, and we replace it by our own version.151+152+ mainlib.RootCmd.RemoveCommand(mainlib.VersionCmd)153+ mainlib.RootCmd.AddCommand(versionCmd)154+155 ### Once all commands and flags are defined, Execute the commands156 157 Execute should be run on the root for clarity, though it can be called on any command.158 250 unmodified lines