Debian Codegraph @

docs/v3/examples/flags/basics.md

raw symbols SCIP
1 ---
2 tags:
3 - v3
4 search:
5 boost: 2
6 ---
7
8 Flags, also called options, can be used to control various behaviour of the app
9 by turning on/off capabilities or setting some configuration and so on.
10 Setting and querying flags is done using the ```cmd.<FlagType>(<flagName>)```
11 function
12
13 Here is an example of using a StringFlag which accepts a string as its option value
14
15 <!-- {
16 "output": "Hello Nefertiti"
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 cmd := &cli.Command{
32 Flags: []cli.Flag{
33 &cli.StringFlag{
34 Name: "lang",
35 Value: "english",
36 Usage: "language for the greeting",
37 },
38 },
39 Action: func(ctx context.Context, cmd *cli.Command) error {
40 name := "Nefertiti"
41 if cmd.NArg() > 0 {
42 name = cmd.Args().Get(0)
43 }
44 if cmd.String("lang") == "spanish" {
45 fmt.Println("Hola", name)
46 } else {
47 fmt.Println("Hello", name)
48 }
49 return nil
50 },
51 }
52
53 if err := cmd.Run(context.Background(), os.Args); err != nil {
54 log.Fatal(err)
55 }
56 }
57 ```
58
59 This very simple program gives a lot of outputs depending on the value of the flag set.
60 ```sh-session
61 $ greet
62 Hello Nefertiti
63 ```
64 Note that the Value for the flag is the default value that will be used when the flag
65 is not set on the command line. Since in the above invocation no flag was specified the
66 value of the "lang" flag was default to "english". Now lets change the language
67
68 ```sh-session
69 $ greet --lang spanish
70 Hola Nefertiti
71 ```
72
73 Flag values can be provided with a space after the flag name or using the ```=``` sign
74 ```sh-session
75 $ greet --lang=spanish
76 Hola Nefertiti
77 $ greet --lang=spanish my-friend
78 Hola my-friend
79 ```
80
81 Boolean flags are the exception: passing a boolean flag without a value sets it to
82 `true`. Use the `=` form, such as `--flag=false`, to provide an explicit boolean value.
83
84 While the value of any flag can be retrieved using ```command.<flagType>``` sometimes
85 it is convenient to have the value of the flag automatically stored in a destination
86 variable for a flag. If the `Value` is set for the flag, it will be shown as default,
87 and destination will be set to this value before parsing flag on the command line.
88
89 <!-- {
90 "output": "Hello someone"
91 } -->
92 ```go
93 package main
94
95 import (
96 "fmt"
97 "log"
98 "os"
99 "context"
100
101 "github.com/urfave/cli/v3"
102 )
103
104 func main() {
105 var language string
106
107 cmd := &cli.Command{
108 Flags: []cli.Flag{
109 &cli.StringFlag{
110 Name: "lang",
111 Value: "english",
112 Usage: "language for the greeting",
113 Destination: &language,
114 },
115 },
116 Action: func(ctx context.Context, cmd *cli.Command) error {
117 name := "someone"
118 if cmd.NArg() > 0 {
119 name = cmd.Args().Get(0)
120 }
121 if language == "spanish" {
122 fmt.Println("Hola", name)
123 } else {
124 fmt.Println("Hello", name)
125 }
126 return nil
127 },
128 }
129
130 if err := cmd.Run(context.Background(), os.Args); err != nil {
131 log.Fatal(err)
132 }
133 }
134 ```
135
136 Note that most flag can be invoked multiple times but only the last value entered for the flag
137 will be provided to the user(with some exceptions. See flags-advanced.md)
138
139 The following basic flags are supported
140
141 - `IntFlag`
142 - `Int8Flag`
143 - `Int16Flag`
144 - `Int32Flag`
145 - `Int64Flag`
146 - `UintFlag`
147 - `Uint8Flag`
148 - `Uint16Flag`
149 - `Uint32Flag`
150 - `Uint64Flag`
151 - `BoolFlag`
152 - `DurationFlag`
153 - `FloatFlag`
154 - `Float32Flag`
155 - `Float64Flag`
156 - `StringFlag`
157 - `TimestampFlag`
158
159 For full list of flags see [`https://pkg.go.dev/github.com/urfave/cli/v3`](https://pkg.go.dev/github.com/urfave/cli/v3)
160
161 ### Timestamp Flag ###
162
163 Using the timestamp flag is similar to other flags but special attention is need
164 for the format to be provided to the flag . Please refer to
165 [`time.Parse`](https://golang.org/pkg/time/#example_Parse) to get possible
166 formats.
167
168 <!-- {
169 "args": ["&#45;&#45;meeting", "2019-08-12T15:04:05"],
170 "output": "2019\\-08\\-12 15\\:04\\:05 \\+0000 UTC"
171 } -->
172 ```go
173 package main
174
175 import (
176 "fmt"
177 "log"
178 "os"
179 "context"
180
181 "github.com/urfave/cli/v3"
182 )
183
184 func main() {
185 cmd := &cli.Command{
186 Flags: []cli.Flag{
187 &cli.TimestampFlag{
188 Name: "meeting",
189 Config: cli.TimestampConfig{
190 Layouts: []string{"2006-01-02T15:04:05"},
191 },
192 },
193 },
194 Action: func(ctx context.Context, cmd *cli.Command) error {
195 fmt.Printf("%s", cmd.Timestamp("meeting").String())
196 return nil
197 },
198 }
199
200 if err := cmd.Run(context.Background(), os.Args); err != nil {
201 log.Fatal(err)
202 }
203 }
204 ```
205
206 In this example the flag could be used like this:
207
208 ```sh-session
209 $ myapp --meeting 2019-08-12T15:04:05
210 ```
211
212 When the layout doesn't contain timezones, timestamp will render with UTC. To
213 change behavior, a default timezone can be provided with flag definition:
214
215 ```go
216 cmd := &cli.Command{
217 Flags: []cli.Flag{
218 &cli.TimestampFlag{
219 Name: "meeting",
220 Config: cli.TimestampConfig{
221 Timezone: time.Local,
222 AvailableLayouts: []string{"2006-01-02T15:04:05"},
223 },
224 },
225 },
226 }
227 ```
228
229 (time.Local contains the system's local time zone.)
230
231 Side note: quotes may be necessary around the date depending on your layout (if
232 you have spaces for instance)
233
234 ### Version Flags ###
235
236 A default version flag (`-v/--version`) is provided as `cli.VersionFlag`, which
237 is checked by the cli internals in order to print the `Command.Version` via
238 `cli.VersionPrinter` and break execution.
239
240 #### Customization
241
242 The default flag may be customized to something other than `-v/--version` by
243 setting fields of `cli.VersionFlag`, e.g.:
244
245 <!-- {
246 "args": ["&#45;&#45print-version"],
247 "output": "partay version v19\\.99\\.0"
248 } -->
249 ```go
250 package main
251
252 import (
253 "os"
254 "context"
255
256 "github.com/urfave/cli/v3"
257 )
258
259 func main() {
260 cli.VersionFlag = &cli.BoolFlag{
261 Name: "print-version",
262 Aliases: []string{"V"},
263 Usage: "print only the version",
264 }
265
266 cmd := &cli.Command{
267 Name: "partay",
268 Version: "v19.99.0",
269 }
270 cmd.Run(context.Background(), os.Args)
271 }
272 ```
273
274 Alternatively, the version printer at `cli.VersionPrinter` may be overridden,
275 e.g.:
276
277 <!-- {
278 "args": ["&#45;&#45version"],
279 "output": "version=v19\\.99\\.0 revision=fafafaf"
280 } -->
281 ```go
282 package main
283
284 import (
285 "fmt"
286 "os"
287 "context"
288
289 "github.com/urfave/cli/v3"
290 )
291
292 var (
293 Revision = "fafafaf"
294 )
295
296 func main() {
297 cli.VersionPrinter = func(cmd *cli.Command) {
298 fmt.Printf("version=%s revision=%s\n", cmd.Root().Version, Revision)
299 }
300
301 cmd := &cli.Command{
302 Name: "partay",
303 Version: "v19.99.0",
304 }
305 cmd.Run(context.Background(), os.Args)
306 }
307 ```