Skip to content

Commit 4122785

Browse files
nirsmarckhouzam
authored andcommitted
Document how to create a plugin
Using the new CommandDisplayNameAnnotation annotation introduced in Cobra 1.8.0.
1 parent a73b9c3 commit 4122785

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

site/content/user_guide.md

+54
Original file line numberDiff line numberDiff line change
@@ -748,3 +748,57 @@ Read more about it in [Shell Completions](completions/_index.md).
748748
Cobra makes use of the shell-completion system to define a framework allowing you to provide Active Help to your users.
749749
Active Help are messages (hints, warnings, etc) printed as the program is being used.
750750
Read more about it in [Active Help](active_help.md).
751+
752+
## Creating a plugin
753+
754+
When creating a plugin for tools like *kubectl*, the executable is named
755+
`kubectl-myplugin`, but it is used as `kubectl myplugin`. To fix help
756+
messages and completions, annotate the root command with the
757+
`cobra.CommandDisplayNameAnnotation` annotation.
758+
759+
### Example kubectl plugin
760+
761+
```go
762+
package main
763+
764+
import (
765+
"fmt"
766+
767+
"github.com/spf13/cobra"
768+
)
769+
770+
func main() {
771+
rootCmd := &cobra.Command{
772+
Use: "kubectl-myplugin",
773+
Annotations: map[string]string{
774+
cobra.CommandDisplayNameAnnotation: "kubectl myplugin",
775+
},
776+
}
777+
subCmd := &cobra.Command{
778+
Use: "subcmd",
779+
Run: func(cmd *cobra.Command, args []string) {
780+
fmt.Println("kubectl myplugin subcmd")
781+
},
782+
}
783+
rootCmd.AddCommand(subCmd)
784+
rootCmd.Execute()
785+
}
786+
```
787+
788+
Example run as a kubectl plugin:
789+
790+
```
791+
$ kubectl myplugin
792+
Usage:
793+
kubectl myplugin [command]
794+
795+
Available Commands:
796+
completion Generate the autocompletion script for the specified shell
797+
help Help about any command
798+
subcmd
799+
800+
Flags:
801+
-h, --help help for kubectl myplugin
802+
803+
Use "kubectl myplugin [command] --help" for more information about a command.
804+
```

0 commit comments

Comments
 (0)