vendor/github.com/spf13/cobra/shell_completions.md
changeset 260 445e01aede7e
parent 256 6d9efbef00a9
child 265 05c40b36d3b2
equal deleted inserted replaced
259:db4911b0c721 260:445e01aede7e
    14 
    14 
    15 If you do not wish to use the default `completion` command, you can choose to
    15 If you do not wish to use the default `completion` command, you can choose to
    16 provide your own, which will take precedence over the default one. (This also provides
    16 provide your own, which will take precedence over the default one. (This also provides
    17 backwards-compatibility with programs that already have their own `completion` command.)
    17 backwards-compatibility with programs that already have their own `completion` command.)
    18 
    18 
    19 If you are using the generator, you can create a completion command by running
    19 If you are using the `cobra-cli` generator,
    20 
    20 which can be found at [spf13/cobra-cli](https://github.com/spf13/cobra-cli),
    21 ```bash
    21 you can create a completion command by running
    22 cobra add completion
    22 
       
    23 ```bash
       
    24 cobra-cli add completion
    23 ```
    25 ```
    24 and then modifying the generated `cmd/completion.go` file to look something like this
    26 and then modifying the generated `cmd/completion.go` file to look something like this
    25 (writing the shell script to stdout allows the most flexible use):
    27 (writing the shell script to stdout allows the most flexible use):
    26 
    28 
    27 ```go
    29 ```go
    28 var completionCmd = &cobra.Command{
    30 var completionCmd = &cobra.Command{
    29 	Use:   "completion [bash|zsh|fish|powershell]",
    31 	Use:   "completion [bash|zsh|fish|powershell]",
    30 	Short: "Generate completion script",
    32 	Short: "Generate completion script",
    31 	Long: `To load completions:
    33 	Long: fmt.Sprintf(`To load completions:
    32 
    34 
    33 Bash:
    35 Bash:
    34 
    36 
    35   $ source <(yourprogram completion bash)
    37   $ source <(%[1]s completion bash)
    36 
    38 
    37   # To load completions for each session, execute once:
    39   # To load completions for each session, execute once:
    38   # Linux:
    40   # Linux:
    39   $ yourprogram completion bash > /etc/bash_completion.d/yourprogram
    41   $ %[1]s completion bash > /etc/bash_completion.d/%[1]s
    40   # macOS:
    42   # macOS:
    41   $ yourprogram completion bash > /usr/local/etc/bash_completion.d/yourprogram
    43   $ %[1]s completion bash > $(brew --prefix)/etc/bash_completion.d/%[1]s
    42 
    44 
    43 Zsh:
    45 Zsh:
    44 
    46 
    45   # If shell completion is not already enabled in your environment,
    47   # If shell completion is not already enabled in your environment,
    46   # you will need to enable it.  You can execute the following once:
    48   # you will need to enable it.  You can execute the following once:
    47 
    49 
    48   $ echo "autoload -U compinit; compinit" >> ~/.zshrc
    50   $ echo "autoload -U compinit; compinit" >> ~/.zshrc
    49 
    51 
    50   # To load completions for each session, execute once:
    52   # To load completions for each session, execute once:
    51   $ yourprogram completion zsh > "${fpath[1]}/_yourprogram"
    53   $ %[1]s completion zsh > "${fpath[1]}/_%[1]s"
    52 
    54 
    53   # You will need to start a new shell for this setup to take effect.
    55   # You will need to start a new shell for this setup to take effect.
    54 
    56 
    55 fish:
    57 fish:
    56 
    58 
    57   $ yourprogram completion fish | source
    59   $ %[1]s completion fish | source
    58 
    60 
    59   # To load completions for each session, execute once:
    61   # To load completions for each session, execute once:
    60   $ yourprogram completion fish > ~/.config/fish/completions/yourprogram.fish
    62   $ %[1]s completion fish > ~/.config/fish/completions/%[1]s.fish
    61 
    63 
    62 PowerShell:
    64 PowerShell:
    63 
    65 
    64   PS> yourprogram completion powershell | Out-String | Invoke-Expression
    66   PS> %[1]s completion powershell | Out-String | Invoke-Expression
    65 
    67 
    66   # To load completions for every new session, run:
    68   # To load completions for every new session, run:
    67   PS> yourprogram completion powershell > yourprogram.ps1
    69   PS> %[1]s completion powershell > %[1]s.ps1
    68   # and source this file from your PowerShell profile.
    70   # and source this file from your PowerShell profile.
    69 `,
    71 `,cmd.Root().Name()),
    70 	DisableFlagsInUseLine: true,
    72 	DisableFlagsInUseLine: true,
    71 	ValidArgs:             []string{"bash", "zsh", "fish", "powershell"},
    73 	ValidArgs:             []string{"bash", "zsh", "fish", "powershell"},
    72 	Args:                  cobra.ExactValidArgs(1),
    74 	Args:                  cobra.ExactValidArgs(1),
    73 	Run: func(cmd *cobra.Command, args []string) {
    75 	Run: func(cmd *cobra.Command, args []string) {
    74 		switch args[0] {
    76 		switch args[0] {
   118 Cobra allows you to provide a pre-defined list of completion choices for your nouns using the `ValidArgs` field.
   120 Cobra allows you to provide a pre-defined list of completion choices for your nouns using the `ValidArgs` field.
   119 For example, if you want `kubectl get [tab][tab]` to show a list of valid "nouns" you have to set them.
   121 For example, if you want `kubectl get [tab][tab]` to show a list of valid "nouns" you have to set them.
   120 Some simplified code from `kubectl get` looks like:
   122 Some simplified code from `kubectl get` looks like:
   121 
   123 
   122 ```go
   124 ```go
   123 validArgs []string = { "pod", "node", "service", "replicationcontroller" }
   125 validArgs = []string{ "pod", "node", "service", "replicationcontroller" }
   124 
   126 
   125 cmd := &cobra.Command{
   127 cmd := &cobra.Command{
   126 	Use:     "get [(-o|--output=)json|yaml|template|...] (RESOURCE [NAME] | RESOURCE/NAME ...)",
   128 	Use:     "get [(-o|--output=)json|yaml|template|...] (RESOURCE [NAME] | RESOURCE/NAME ...)",
   127 	Short:   "Display one or many resources",
   129 	Short:   "Display one or many resources",
   128 	Long:    get_long,
   130 	Long:    get_long,
   144 #### Aliases for nouns
   146 #### Aliases for nouns
   145 
   147 
   146 If your nouns have aliases, you can define them alongside `ValidArgs` using `ArgAliases`:
   148 If your nouns have aliases, you can define them alongside `ValidArgs` using `ArgAliases`:
   147 
   149 
   148 ```go
   150 ```go
   149 argAliases []string = { "pods", "nodes", "services", "svc", "replicationcontrollers", "rc" }
   151 argAliases = []string { "pods", "nodes", "services", "svc", "replicationcontrollers", "rc" }
   150 
   152 
   151 cmd := &cobra.Command{
   153 cmd := &cobra.Command{
   152     ...
   154     ...
   153 	ValidArgs:  validArgs,
   155 	ValidArgs:  validArgs,
   154 	ArgAliases: argAliases
   156 	ArgAliases: argAliases