Debian Codegraph @

docs/v3/examples/completions/customizations.md

raw symbols SCIP
1 ---
2 tags:
3 - v3
4 search:
5 boost: 2
6 ---
7
8 If default completion isn't sufficient additional customizations are available
9
10 - custom auto-completion
11 - customizing completion command
12
13 #### Custom auto-completion
14 <!-- {
15 "args": ["complete", "&#45;&#45;generate&#45;shell&#45;completion"],
16 "output": "laundry"
17 } -->
18 ```go
19 package main
20
21 import (
22 "fmt"
23 "log"
24 "os"
25 "context"
26
27 "github.com/urfave/cli/v3"
28 )
29
30 func main() {
31 tasks := []string{"cook", "clean", "laundry", "eat", "sleep", "code"}
32
33 cmd := &cli.Command{
34 EnableShellCompletion: true,
35 Commands: []*cli.Command{
36 {
37 Name: "complete",
38 Aliases: []string{"c"},
39 Usage: "complete a task on the list",
40 Action: func(ctx context.Context, cmd *cli.Command) error {
41 fmt.Println("completed task: ", cmd.Args().First())
42 return nil
43 },
44 ShellComplete: func(ctx context.Context, cmd *cli.Command) {
45 // This will complete if no args are passed
46 if cmd.NArg() > 0 {
47 return
48 }
49 for _, t := range tasks {
50 fmt.Println(t)
51 }
52 },
53 },
54 },
55 }
56
57 if err := cmd.Run(context.Background(), os.Args); err != nil {
58 log.Fatal(err)
59 }
60 }
61 ```
62 ![](../../images/custom-bash-autocomplete.gif)
63
64 #### Customize a completion command
65
66 By default, a completion command is hidden, meaning the command isn't included in the help message.
67 You can customize it by setting root Command's `ConfigureShellCompletionCommand`.
68
69 ```go
70 package main
71
72 import (
73 "context"
74 "fmt"
75 "log"
76 "os"
77
78 "github.com/urfave/cli/v3"
79 )
80
81 func main() {
82 cmd := &cli.Command{
83 Name: "greet",
84 // EnableShellCompletion is unnecessary
85 ConfigureShellCompletionCommand: func(cmd *cli.Command) { // cmd is a completion command
86 cmd.Hidden = false // Make a completion command public
87 cmd.Usage = "..." // Customize Usage
88 cmd.Description = "..." // Customize Description
89 },
90 Commands: []*cli.Command{
91 {
92 Name: "hello",
93 Usage: "Say hello",
94 Action: func(ctx context.Context, cmd *cli.Command) error {
95 fmt.Println("Hello")
96 return nil
97 },
98 },
99 },
100 }
101
102 if err := cmd.Run(context.Background(), os.Args); err != nil {
103 log.Fatal(err)
104 }
105 }
106 ```
107
108 #### Customization
109
110 The default shell completion flag (`--generate-shell-completion`) is defined as
111 `cli.EnableShellCompletion`, and may be redefined if desired, e.g.:
112
113 <!-- {
114 "args": ["&#45;&#45;generate&#45;shell&#45;completion"],
115 "output": "wat\nhelp:Shows a list of commands or help for one command\n"
116 } -->
117 ```go
118 package main
119
120 import (
121 "log"
122 "os"
123 "context"
124
125 "github.com/urfave/cli/v3"
126 )
127
128 func main() {
129 cmd := &cli.Command{
130 EnableShellCompletion: true,
131 Commands: []*cli.Command{
132 {
133 Name: "wat",
134 },
135 },
136 }
137
138 if err := cmd.Run(context.Background(), os.Args); err != nil {
139 log.Fatal(err)
140 }
141 }
142 ```