@@ -748,3 +748,57 @@ Read more about it in [Shell Completions](completions/_index.md).
748
748
Cobra makes use of the shell-completion system to define a framework allowing you to provide Active Help to your users.
749
749
Active Help are messages (hints, warnings, etc) printed as the program is being used.
750
750
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