| 1 | --- |
| 2 | tags: |
| 3 | - v3 |
| 4 | search: |
| 5 | boost: 2 |
| 6 | --- |
| 7 | |
| 8 | The urfave/cli v3 library supports programmable completion for apps utilizing its framework. This means |
| 9 | that the completion is generated dynamically at runtime by invoking the app itself with a special hidden |
| 10 | flag. The urfave/cli searches for this flag and activates a different flow for command paths than regular flow |
| 11 | The following shells are supported |
| 12 | |
| 13 | - bash |
| 14 | - zsh |
| 15 | - fish |
| 16 | - powershell |
| 17 | |
| 18 | Enabling auto complete requires 2 things |
| 19 | |
| 20 | - Setting the `EnableShellCompletion` field on root `Command` object to `true`. |
| 21 | - Sourcing the completion script for that particular shell. |
| 22 | |
| 23 | The completion script for a particular shell can be retrieved by running the "completion" subcommand |
| 24 | on the app after the `EnableShellCompletion` field on root `Command` object has been set to `true`. |
| 25 | |
| 26 | Consider the following program |
| 27 | |
| 28 | ```go |
| 29 | package main |
| 30 | |
| 31 | import ( |
| 32 | "fmt" |
| 33 | "log" |
| 34 | "os" |
| 35 | "context" |
| 36 | |
| 37 | "github.com/urfave/cli/v3" |
| 38 | ) |
| 39 | |
| 40 | func main() { |
| 41 | cmd := &cli.Command{ |
| 42 | Name: "greet", |
| 43 | EnableShellCompletion: true, |
| 44 | Commands: []*cli.Command{ |
| 45 | { |
| 46 | Name: "add", |
| 47 | Aliases: []string{"a"}, |
| 48 | Usage: "add a task to the list", |
| 49 | Action: func(ctx context.Context, cmd *cli.Command) error { |
| 50 | fmt.Println("added task: ", cmd.Args().First()) |
| 51 | return nil |
| 52 | }, |
| 53 | }, |
| 54 | { |
| 55 | Name: "complete", |
| 56 | Aliases: []string{"c"}, |
| 57 | Usage: "complete a task on the list", |
| 58 | Action: func(ctx context.Context, cmd *cli.Command) error { |
| 59 | fmt.Println("completed task: ", cmd.Args().First()) |
| 60 | return nil |
| 61 | }, |
| 62 | }, |
| 63 | { |
| 64 | Name: "template", |
| 65 | Aliases: []string{"t"}, |
| 66 | Usage: "options for task templates", |
| 67 | Commands: []*cli.Command{ |
| 68 | { |
| 69 | Name: "add", |
| 70 | Usage: "add a new template", |
| 71 | Action: func(ctx context.Context, cmd *cli.Command) error { |
| 72 | fmt.Println("new task template: ", cmd.Args().First()) |
| 73 | return nil |
| 74 | }, |
| 75 | }, |
| 76 | { |
| 77 | Name: "remove", |
| 78 | Usage: "remove an existing template", |
| 79 | Action: func(ctx context.Context, cmd *cli.Command) error { |
| 80 | fmt.Println("removed task template: ", cmd.Args().First()) |
| 81 | return nil |
| 82 | }, |
| 83 | }, |
| 84 | }, |
| 85 | }, |
| 86 | }, |
| 87 | } |
| 88 | |
| 89 | if err := cmd.Run(context.Background(), os.Args); err != nil { |
| 90 | log.Fatal(err) |
| 91 | } |
| 92 | } |
| 93 | ``` |
| 94 | |
| 95 | After compiling this app as `greet` we can generate the autocompletion as following |
| 96 | in bash script |
| 97 | |
| 98 | ```sh-session |
| 99 | $ greet completion bash |
| 100 | ``` |
| 101 | |
| 102 | This file can be saved to /etc/bash_completion.d/greet or $HOME/.bash_completion.d/greet |
| 103 | where it will be automatically picked in new bash shells. For the current shell these |
| 104 | can be sourced either using filename or from generation command directly |
| 105 | |
| 106 | ```sh-session |
| 107 | $ source ~/.bash_completion.d/greet |
| 108 | ``` |
| 109 | |
| 110 | ```sh-session |
| 111 | $ source <(greet completion bash) |
| 112 | ``` |
| 113 | |
| 114 | The procedure for other shells is similar to bash though the specific paths for each of the |
| 115 | shells may vary. Some of the sections below detail the setup need for other shells as |
| 116 | well as examples in those shells. |
| 117 | |
| 118 | #### Default auto-completion |
| 119 | |
| 120 | ```go |
| 121 | package main |
| 122 | |
| 123 | import ( |
| 124 | "fmt" |
| 125 | "log" |
| 126 | "os" |
| 127 | "context" |
| 128 | |
| 129 | "github.com/urfave/cli/v3" |
| 130 | ) |
| 131 | |
| 132 | func main() { |
| 133 | cmd := &cli.Command{ |
| 134 | EnableShellCompletion: true, |
| 135 | Commands: []*cli.Command{ |
| 136 | { |
| 137 | Name: "add", |
| 138 | Aliases: []string{"a"}, |
| 139 | Usage: "add a task to the list", |
| 140 | Action: func(ctx context.Context, cmd *cli.Command) error { |
| 141 | fmt.Println("added task: ", cmd.Args().First()) |
| 142 | return nil |
| 143 | }, |
| 144 | }, |
| 145 | { |
| 146 | Name: "complete", |
| 147 | Aliases: []string{"c"}, |
| 148 | Usage: "complete a task on the list", |
| 149 | Action: func(ctx context.Context, cmd *cli.Command) error { |
| 150 | fmt.Println("completed task: ", cmd.Args().First()) |
| 151 | return nil |
| 152 | }, |
| 153 | }, |
| 154 | { |
| 155 | Name: "template", |
| 156 | Aliases: []string{"t"}, |
| 157 | Usage: "options for task templates", |
| 158 | Commands: []*cli.Command{ |
| 159 | { |
| 160 | Name: "add", |
| 161 | Usage: "add a new template", |
| 162 | Action: func(ctx context.Context, cmd *cli.Command) error { |
| 163 | fmt.Println("new task template: ", cmd.Args().First()) |
| 164 | return nil |
| 165 | }, |
| 166 | }, |
| 167 | { |
| 168 | Name: "remove", |
| 169 | Usage: "remove an existing template", |
| 170 | Action: func(ctx context.Context, cmd *cli.Command) error { |
| 171 | fmt.Println("removed task template: ", cmd.Args().First()) |
| 172 | return nil |
| 173 | }, |
| 174 | }, |
| 175 | }, |
| 176 | }, |
| 177 | }, |
| 178 | } |
| 179 | |
| 180 | if err := cmd.Run(context.Background(), os.Args); err != nil { |
| 181 | log.Fatal(err) |
| 182 | } |
| 183 | } |
| 184 | ``` |
| 185 |  |
| 186 | |
| 187 | #### ZSH Support |
| 188 | |
| 189 | Adding the following lines to |
| 190 | your ZSH configuration file (usually `.zshrc`) will allow the auto-completion to |
| 191 | persist across new shells: |
| 192 | |
| 193 | ```sh-session |
| 194 | $ PROG=<myprogram> |
| 195 | $ source path/to/autocomplete/zsh_autocomplete |
| 196 | ``` |
| 197 | |
| 198 | #### ZSH default auto-complete example |
| 199 |  |
| 200 | |
| 201 | #### PowerShell Support |
| 202 | |
| 203 | Generate the completion script as save it to `<my program>.ps1` . This file can be moved to |
| 204 | anywhere in your file system. The location of script does not matter, only the file name of the |
| 205 | script has to match the your program's binary name. |
| 206 | |
| 207 | To activate it, enter: |
| 208 | |
| 209 | ```powershell |
| 210 | & path/to/autocomplete/<my program>.ps1 |
| 211 | ``` |
| 212 | |
| 213 | To persist across new shells, open the PowerShell profile (with `code $profile` |
| 214 | or `notepad $profile`) and add the line: |
| 215 | |
| 216 | ```powershell |
| 217 | & path/to/autocomplete/<my program>.ps1 |
| 218 | ``` |