|
| 1 | +// Copyright © 2016 Dropbox, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package cmd |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + |
| 20 | + "github.com/spf13/cobra" |
| 21 | +) |
| 22 | + |
| 23 | +func newCompletionCmd() *cobra.Command { |
| 24 | + noDesc := false |
| 25 | + programName := RootCmd.Name() |
| 26 | + |
| 27 | + completionCmd := &cobra.Command{ |
| 28 | + Use: "completion [bash|zsh|fish|powershell]", |
| 29 | + Short: "Generate the autocompletion script for the specified shell", |
| 30 | + Long: completionLong(programName), |
| 31 | + Args: cobra.NoArgs, |
| 32 | + ValidArgsFunction: cobra.NoFileCompletions, |
| 33 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 34 | + return cmd.Help() |
| 35 | + }, |
| 36 | + } |
| 37 | + |
| 38 | + addCompletionNoDescFlag := func(cmd *cobra.Command) { |
| 39 | + cmd.Flags().BoolVar(&noDesc, "no-descriptions", false, "disable completion descriptions") |
| 40 | + } |
| 41 | + |
| 42 | + bash := &cobra.Command{ |
| 43 | + Use: "bash", |
| 44 | + Short: "Generate the autocompletion script for bash", |
| 45 | + Long: bashCompletionLong(programName), |
| 46 | + Args: cobra.NoArgs, |
| 47 | + DisableFlagsInUseLine: true, |
| 48 | + ValidArgsFunction: cobra.NoFileCompletions, |
| 49 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 50 | + return cmd.Root().GenBashCompletionV2(cmd.Root().OutOrStdout(), !noDesc) |
| 51 | + }, |
| 52 | + } |
| 53 | + addCompletionNoDescFlag(bash) |
| 54 | + |
| 55 | + zsh := &cobra.Command{ |
| 56 | + Use: "zsh", |
| 57 | + Short: "Generate the autocompletion script for zsh", |
| 58 | + Long: zshCompletionLong(programName), |
| 59 | + Args: cobra.NoArgs, |
| 60 | + ValidArgsFunction: cobra.NoFileCompletions, |
| 61 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 62 | + if noDesc { |
| 63 | + return cmd.Root().GenZshCompletionNoDesc(cmd.Root().OutOrStdout()) |
| 64 | + } |
| 65 | + return cmd.Root().GenZshCompletion(cmd.Root().OutOrStdout()) |
| 66 | + }, |
| 67 | + } |
| 68 | + addCompletionNoDescFlag(zsh) |
| 69 | + |
| 70 | + fish := &cobra.Command{ |
| 71 | + Use: "fish", |
| 72 | + Short: "Generate the autocompletion script for fish", |
| 73 | + Long: fishCompletionLong(programName), |
| 74 | + Args: cobra.NoArgs, |
| 75 | + ValidArgsFunction: cobra.NoFileCompletions, |
| 76 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 77 | + return cmd.Root().GenFishCompletion(cmd.Root().OutOrStdout(), !noDesc) |
| 78 | + }, |
| 79 | + } |
| 80 | + addCompletionNoDescFlag(fish) |
| 81 | + |
| 82 | + powershell := &cobra.Command{ |
| 83 | + Use: "powershell", |
| 84 | + Short: "Generate the autocompletion script for powershell", |
| 85 | + Long: powershellCompletionLong(programName), |
| 86 | + Args: cobra.NoArgs, |
| 87 | + ValidArgsFunction: cobra.NoFileCompletions, |
| 88 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 89 | + if noDesc { |
| 90 | + return cmd.Root().GenPowerShellCompletion(cmd.Root().OutOrStdout()) |
| 91 | + } |
| 92 | + return cmd.Root().GenPowerShellCompletionWithDesc(cmd.Root().OutOrStdout()) |
| 93 | + }, |
| 94 | + } |
| 95 | + addCompletionNoDescFlag(powershell) |
| 96 | + |
| 97 | + completionCmd.AddCommand(bash, zsh, fish, powershell) |
| 98 | + return completionCmd |
| 99 | +} |
| 100 | + |
| 101 | +func completionLong(programName string) string { |
| 102 | + return fmt.Sprintf(`Generate the autocompletion script for %s for the specified shell. |
| 103 | +See each sub-command's help for details on how to use the generated script. |
| 104 | +`, programName) |
| 105 | +} |
| 106 | + |
| 107 | +func bashCompletionLong(programName string) string { |
| 108 | + return fmt.Sprintf(`Generate the autocompletion script for the bash shell. |
| 109 | +
|
| 110 | +This script depends on the 'bash-completion' package. |
| 111 | +If it is not installed already, you can install it via your OS's package manager. |
| 112 | +
|
| 113 | +To load completions in your current shell session: |
| 114 | +
|
| 115 | + source <(%[1]s completion bash) |
| 116 | +
|
| 117 | +To load completions for every new session, execute once: |
| 118 | +
|
| 119 | +#### Linux: |
| 120 | +
|
| 121 | + %[1]s completion bash > /etc/bash_completion.d/%[1]s |
| 122 | +
|
| 123 | +#### macOS: |
| 124 | +
|
| 125 | + %[1]s completion bash > $(brew --prefix)/etc/bash_completion.d/%[1]s |
| 126 | +
|
| 127 | +You will need to start a new shell for this setup to take effect. |
| 128 | +`, programName) |
| 129 | +} |
| 130 | + |
| 131 | +func zshCompletionLong(programName string) string { |
| 132 | + return fmt.Sprintf(`Generate the autocompletion script for the zsh shell. |
| 133 | +
|
| 134 | +If shell completion is not already enabled in your environment you will need |
| 135 | +to enable it. You can execute the following once: |
| 136 | +
|
| 137 | + echo "autoload -U compinit; compinit" >> ~/.zshrc |
| 138 | +
|
| 139 | +To load completions in your current shell session: |
| 140 | +
|
| 141 | + source <(%[1]s completion zsh) |
| 142 | +
|
| 143 | +To load completions for every new session, execute once: |
| 144 | +
|
| 145 | +#### Linux: |
| 146 | +
|
| 147 | + %[1]s completion zsh > "${fpath[1]}/_%[1]s" |
| 148 | +
|
| 149 | +#### macOS: |
| 150 | +
|
| 151 | + %[1]s completion zsh > $(brew --prefix)/share/zsh/site-functions/_%[1]s |
| 152 | +
|
| 153 | +You will need to start a new shell for this setup to take effect. |
| 154 | +`, programName) |
| 155 | +} |
| 156 | + |
| 157 | +func fishCompletionLong(programName string) string { |
| 158 | + return fmt.Sprintf(`Generate the autocompletion script for the fish shell. |
| 159 | +
|
| 160 | +To load completions in your current shell session: |
| 161 | +
|
| 162 | + %[1]s completion fish | source |
| 163 | +
|
| 164 | +To load completions for every new session, execute once: |
| 165 | +
|
| 166 | + %[1]s completion fish > ~/.config/fish/completions/%[1]s.fish |
| 167 | +
|
| 168 | +You will need to start a new shell for this setup to take effect. |
| 169 | +`, programName) |
| 170 | +} |
| 171 | + |
| 172 | +func powershellCompletionLong(programName string) string { |
| 173 | + return fmt.Sprintf(`Generate the autocompletion script for powershell. |
| 174 | +
|
| 175 | +To load completions in your current shell session: |
| 176 | +
|
| 177 | + %[1]s completion powershell | Out-String | Invoke-Expression |
| 178 | +
|
| 179 | +To load completions for every new session, add the output of the above command |
| 180 | +to your powershell profile. |
| 181 | +`, programName) |
| 182 | +} |
| 183 | + |
| 184 | +func init() { |
| 185 | + RootCmd.AddCommand(newCompletionCmd()) |
| 186 | +} |
0 commit comments