mirror of
https://github.com/spf13/cobra
synced 2024-12-28 23:37:09 +00:00
Merge pull request #60 from dohzya/remove-command
Add Command's RemoveCommand method
This commit is contained in:
commit
0ea881ce45
2 changed files with 61 additions and 0 deletions
|
@ -143,6 +143,15 @@ A flag can also be assigned locally which will only apply to that specific comma
|
|||
|
||||
HugoCmd.Flags().StringVarP(&Source, "source", "s", "", "Source directory to read from")
|
||||
|
||||
### Remove a command from its parent
|
||||
|
||||
Removing a command is not a common action is simple program but it allows 3rd parties to customize an existing command tree.
|
||||
|
||||
In this exemple, we remove the existing `VersionCmd` command of an existing root command, and we replace it by our own version.
|
||||
|
||||
mainlib.RootCmd.RemoveCommand(mainlib.VersionCmd)
|
||||
mainlib.RootCmd.AddCommand(versionCmd)
|
||||
|
||||
### Once all commands and flags are defined, Execute the commands
|
||||
|
||||
Execute should be run on the root for clarity, though it can be called on any command.
|
||||
|
|
|
@ -17,6 +17,7 @@ var flags1, flags2a, flags2b, flags3 string
|
|||
var flagi1, flagi2, flagi3, flagir int
|
||||
var globalFlag1 bool
|
||||
var flagEcho, rootcalled bool
|
||||
var versionUsed int
|
||||
|
||||
const strtwoParentHelp = "help message for parent flag strtwo"
|
||||
const strtwoChildHelp = "help message for child flag strtwo"
|
||||
|
@ -70,6 +71,24 @@ var cmdRootWithRun = &Command{
|
|||
},
|
||||
}
|
||||
|
||||
var cmdVersion1 = &Command{
|
||||
Use: "version",
|
||||
Short: "Print the version number",
|
||||
Long: `First version of the version command`,
|
||||
Run: func(cmd *Command, args []string) {
|
||||
versionUsed = 1
|
||||
},
|
||||
}
|
||||
|
||||
var cmdVersion2 = &Command{
|
||||
Use: "version",
|
||||
Short: "Print the version number",
|
||||
Long: `Second version of the version command`,
|
||||
Run: func(cmd *Command, args []string) {
|
||||
versionUsed = 2
|
||||
},
|
||||
}
|
||||
|
||||
func flagInit() {
|
||||
cmdEcho.ResetFlags()
|
||||
cmdPrint.ResetFlags()
|
||||
|
@ -88,6 +107,8 @@ func flagInit() {
|
|||
cmdEcho.Flags().BoolVarP(&flagb1, "boolone", "b", true, "help message for flag boolone")
|
||||
cmdTimes.Flags().BoolVarP(&flagb2, "booltwo", "c", false, "help message for flag booltwo")
|
||||
cmdPrint.Flags().BoolVarP(&flagb3, "boolthree", "b", true, "help message for flag boolthree")
|
||||
cmdVersion1.ResetFlags()
|
||||
cmdVersion2.ResetFlags()
|
||||
}
|
||||
|
||||
func commandInit() {
|
||||
|
@ -697,3 +718,34 @@ func TestFlagsBeforeCommand(t *testing.T) {
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
func TestRemoveCommand(t *testing.T) {
|
||||
versionUsed = 0
|
||||
c := initializeWithRootCmd()
|
||||
c.AddCommand(cmdVersion1)
|
||||
c.RemoveCommand(cmdVersion1)
|
||||
x := fullTester(c, "version")
|
||||
if x.Error == nil {
|
||||
t.Errorf("Removed command should not have been called\n")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func TestReplaceCommandWithRemove(t *testing.T) {
|
||||
versionUsed = 0
|
||||
c := initializeWithRootCmd()
|
||||
c.AddCommand(cmdVersion1)
|
||||
c.RemoveCommand(cmdVersion1)
|
||||
c.AddCommand(cmdVersion2)
|
||||
x := fullTester(c, "version")
|
||||
if x.Error != nil {
|
||||
t.Errorf("Valid Input shouldn't have errors, got:\n %q", x.Error)
|
||||
return
|
||||
}
|
||||
if versionUsed == 1 {
|
||||
t.Errorf("Removed command shouldn't be called\n")
|
||||
}
|
||||
if versionUsed != 2 {
|
||||
t.Errorf("Replacing command should have been called but didn't\n")
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue