Debian Codegraph @

docs/v2/examples/bash-completions.md

raw symbols SCIP
1 ---
2 tags:
3 - v2
4 search:
5 boost: 2
6 ---
7
8 You can enable completion commands by setting the `EnableBashCompletion` flag on
9 the `App` object to `true`. By default, this setting will allow auto-completion
10 for an app's subcommands, but you can write your own completion methods for the
11 App or its subcommands as well.
12
13 #### Default auto-completion
14
15 ```go
16 package main
17
18 import (
19 "fmt"
20 "log"
21 "os"
22
23 "github.com/urfave/cli/v2"
24 )
25
26 func main() {
27 app := &cli.App{
28 EnableBashCompletion: true,
29 Commands: []*cli.Command{
30 {
31 Name: "add",
32 Aliases: []string{"a"},
33 Usage: "add a task to the list",
34 Action: func(cCtx *cli.Context) error {
35 fmt.Println("added task: ", cCtx.Args().First())
36 return nil
37 },
38 },
39 {
40 Name: "complete",
41 Aliases: []string{"c"},
42 Usage: "complete a task on the list",
43 Action: func(cCtx *cli.Context) error {
44 fmt.Println("completed task: ", cCtx.Args().First())
45 return nil
46 },
47 },
48 {
49 Name: "template",
50 Aliases: []string{"t"},
51 Usage: "options for task templates",
52 Subcommands: []*cli.Command{
53 {
54 Name: "add",
55 Usage: "add a new template",
56 Action: func(cCtx *cli.Context) error {
57 fmt.Println("new task template: ", cCtx.Args().First())
58 return nil
59 },
60 },
61 {
62 Name: "remove",
63 Usage: "remove an existing template",
64 Action: func(cCtx *cli.Context) error {
65 fmt.Println("removed task template: ", cCtx.Args().First())
66 return nil
67 },
68 },
69 },
70 },
71 },
72 }
73
74 if err := app.Run(os.Args); err != nil {
75 log.Fatal(err)
76 }
77 }
78 ```
79 ![](../images/default-bash-autocomplete.gif)
80
81 #### Custom auto-completion
82 <!-- {
83 "args": ["complete", "&#45;&#45;generate&#45;bash&#45;completion"],
84 "output": "laundry"
85 } -->
86 ```go
87 package main
88
89 import (
90 "fmt"
91 "log"
92 "os"
93
94 "github.com/urfave/cli/v2"
95 )
96
97 func main() {
98 tasks := []string{"cook", "clean", "laundry", "eat", "sleep", "code"}
99
100 app := &cli.App{
101 EnableBashCompletion: true,
102 Commands: []*cli.Command{
103 {
104 Name: "complete",
105 Aliases: []string{"c"},
106 Usage: "complete a task on the list",
107 Action: func(cCtx *cli.Context) error {
108 fmt.Println("completed task: ", cCtx.Args().First())
109 return nil
110 },
111 BashComplete: func(cCtx *cli.Context) {
112 // This will complete if no args are passed
113 if cCtx.NArg() > 0 {
114 return
115 }
116 for _, t := range tasks {
117 fmt.Println(t)
118 }
119 },
120 },
121 },
122 }
123
124 if err := app.Run(os.Args); err != nil {
125 log.Fatal(err)
126 }
127 }
128 ```
129 ![](../images/custom-bash-autocomplete.gif)
130
131 #### Enabling
132
133 To enable auto-completion for the current shell session, a bash script,
134 `autocomplete/bash_autocomplete` is included in this repo.
135
136 To use `autocomplete/bash_autocomplete` set an environment variable named `PROG`
137 to the name of your program and then `source` the
138 `autocomplete/bash_autocomplete` file.
139
140 For example, if your cli program is called `myprogram`:
141
142 ```sh-session
143 $ PROG=myprogram source path/to/cli/autocomplete/bash_autocomplete
144 ```
145
146 Auto-completion is now enabled for the current shell, but will not persist into
147 a new shell.
148
149 #### Distribution and Persistent Autocompletion
150
151 Copy `autocomplete/bash_autocomplete` into `/etc/bash_completion.d/` and rename
152 it to the name of the program you wish to add autocomplete support for (or
153 automatically install it there if you are distributing a package). Don't forget
154 to source the file or restart your shell to activate the auto-completion.
155
156 ```sh-session
157 $ sudo cp path/to/autocomplete/bash_autocomplete /etc/bash_completion.d/<myprogram>
158 $ source /etc/bash_completion.d/<myprogram>
159 ```
160
161 Alternatively, you can just document that users should `source` the generic
162 `autocomplete/bash_autocomplete` and set `$PROG` within their bash configuration
163 file, adding these lines:
164
165 ```sh-session
166 $ PROG=<myprogram>
167 $ source path/to/cli/autocomplete/bash_autocomplete
168 ```
169
170 Keep in mind that if they are enabling auto-completion for more than one
171 program, they will need to set `PROG` and source
172 `autocomplete/bash_autocomplete` for each program, like so:
173
174 ```sh-session
175 $ PROG=<program1>
176 $ source path/to/cli/autocomplete/bash_autocomplete
177
178 $ PROG=<program2>
179 $ source path/to/cli/autocomplete/bash_autocomplete
180 ```
181
182 #### Customization
183
184 The default shell completion flag (`--generate-bash-completion`) is defined as
185 `cli.EnableBashCompletion`, and may be redefined if desired, e.g.:
186
187 <!-- {
188 "args": ["&#45;&#45;generate&#45;bash&#45;completion"],
189 "output": "wat\nhelp\nh"
190 } -->
191 ```go
192 package main
193
194 import (
195 "log"
196 "os"
197
198 "github.com/urfave/cli/v2"
199 )
200
201 func main() {
202 app := &cli.App{
203 EnableBashCompletion: true,
204 Commands: []*cli.Command{
205 {
206 Name: "wat",
207 },
208 },
209 }
210
211 if err := app.Run(os.Args); err != nil {
212 log.Fatal(err)
213 }
214 }
215 ```
216
217 #### ZSH Support
218
219 Auto-completion for ZSH is also supported using the
220 `autocomplete/zsh_autocomplete` file included in this repo. One environment
221 variable is used, `PROG`. Set `PROG` to the program name as before, and then
222 `source path/to/autocomplete/zsh_autocomplete`. Adding the following lines to
223 your ZSH configuration file (usually `.zshrc`) will allow the auto-completion to
224 persist across new shells:
225
226 ```sh-session
227 $ PROG=<myprogram>
228 $ source path/to/autocomplete/zsh_autocomplete
229 ```
230
231 #### ZSH default auto-complete example
232 ![](../images/default-zsh-autocomplete.gif)
233
234 #### ZSH custom auto-complete example
235 ![](../images/custom-zsh-autocomplete.gif)
236
237 #### PowerShell Support
238
239 Auto-completion for PowerShell is also supported using the
240 `autocomplete/powershell_autocomplete.ps1` file included in this repo.
241
242 Rename the script to `<my program>.ps1` and move it anywhere in your file
243 system. The location of script does not matter, only the file name of the
244 script has to match the your program's binary name.
245
246 To activate it, enter:
247
248 ```powershell
249 & path/to/autocomplete/<my program>.ps1
250 ```
251
252 To persist across new shells, open the PowerShell profile (with `code $profile`
253 or `notepad $profile`) and add the line:
254
255 ```powershell
256 & path/to/autocomplete/<my program>.ps1
257 ```