| 1 | --- |
| 2 | tags: |
| 3 | - v2 |
| 4 | search: |
| 5 | boost: 2 |
| 6 | --- |
| 7 | |
| 8 | Setting and querying flags is simple. |
| 9 | |
| 10 | <!-- { |
| 11 | "output": "Hello Nefertiti" |
| 12 | } --> |
| 13 | ```go |
| 14 | package main |
| 15 | |
| 16 | import ( |
| 17 | "fmt" |
| 18 | "log" |
| 19 | "os" |
| 20 | |
| 21 | "github.com/urfave/cli/v2" |
| 22 | ) |
| 23 | |
| 24 | func main() { |
| 25 | app := &cli.App{ |
| 26 | Flags: []cli.Flag{ |
| 27 | &cli.StringFlag{ |
| 28 | Name: "lang", |
| 29 | Value: "english", |
| 30 | Usage: "language for the greeting", |
| 31 | }, |
| 32 | }, |
| 33 | Action: func(cCtx *cli.Context) error { |
| 34 | name := "Nefertiti" |
| 35 | if cCtx.NArg() > 0 { |
| 36 | name = cCtx.Args().Get(0) |
| 37 | } |
| 38 | if cCtx.String("lang") == "spanish" { |
| 39 | fmt.Println("Hola", name) |
| 40 | } else { |
| 41 | fmt.Println("Hello", name) |
| 42 | } |
| 43 | return nil |
| 44 | }, |
| 45 | } |
| 46 | |
| 47 | if err := app.Run(os.Args); err != nil { |
| 48 | log.Fatal(err) |
| 49 | } |
| 50 | } |
| 51 | ``` |
| 52 | |
| 53 | You can also set a destination variable for a flag, to which the content will be |
| 54 | scanned. Note that if the `Value` is set for the flag, it will be shown as default, |
| 55 | and destination will be set to this value before parsing flag on the command line. |
| 56 | |
| 57 | <!-- { |
| 58 | "output": "Hello someone" |
| 59 | } --> |
| 60 | ```go |
| 61 | package main |
| 62 | |
| 63 | import ( |
| 64 | "fmt" |
| 65 | "log" |
| 66 | "os" |
| 67 | |
| 68 | "github.com/urfave/cli/v2" |
| 69 | ) |
| 70 | |
| 71 | func main() { |
| 72 | var language string |
| 73 | |
| 74 | app := &cli.App{ |
| 75 | Flags: []cli.Flag{ |
| 76 | &cli.StringFlag{ |
| 77 | Name: "lang", |
| 78 | Value: "english", |
| 79 | Usage: "language for the greeting", |
| 80 | Destination: &language, |
| 81 | }, |
| 82 | }, |
| 83 | Action: func(cCtx *cli.Context) error { |
| 84 | name := "someone" |
| 85 | if cCtx.NArg() > 0 { |
| 86 | name = cCtx.Args().Get(0) |
| 87 | } |
| 88 | if language == "spanish" { |
| 89 | fmt.Println("Hola", name) |
| 90 | } else { |
| 91 | fmt.Println("Hello", name) |
| 92 | } |
| 93 | return nil |
| 94 | }, |
| 95 | } |
| 96 | |
| 97 | if err := app.Run(os.Args); err != nil { |
| 98 | log.Fatal(err) |
| 99 | } |
| 100 | } |
| 101 | ``` |
| 102 | |
| 103 | See full list of flags at https://pkg.go.dev/github.com/urfave/cli/v2 |
| 104 | |
| 105 | For bool flags you can specify the flag multiple times to get a count(e.g -v -v -v or -vvv) |
| 106 | |
| 107 | > If you want to support the `-vvv` flag, you need to set `App.UseShortOptionHandling`. |
| 108 | |
| 109 | <!-- { |
| 110 | "args": ["--foo", "--foo", "-fff", "-f"], |
| 111 | "output": "count 6" |
| 112 | } --> |
| 113 | ```go |
| 114 | package main |
| 115 | |
| 116 | import ( |
| 117 | "fmt" |
| 118 | "log" |
| 119 | "os" |
| 120 | |
| 121 | "github.com/urfave/cli/v2" |
| 122 | ) |
| 123 | |
| 124 | func main() { |
| 125 | var count int |
| 126 | |
| 127 | app := &cli.App{ |
| 128 | UseShortOptionHandling: true, |
| 129 | Flags: []cli.Flag{ |
| 130 | &cli.BoolFlag{ |
| 131 | Name: "foo", |
| 132 | Usage: "foo greeting", |
| 133 | Aliases: []string{"f"}, |
| 134 | Count: &count, |
| 135 | }, |
| 136 | }, |
| 137 | Action: func(cCtx *cli.Context) error { |
| 138 | fmt.Println("count", count) |
| 139 | return nil |
| 140 | }, |
| 141 | } |
| 142 | |
| 143 | if err := app.Run(os.Args); err != nil { |
| 144 | log.Fatal(err) |
| 145 | } |
| 146 | } |
| 147 | ``` |
| 148 | |
| 149 | #### Placeholder Values |
| 150 | |
| 151 | Sometimes it's useful to specify a flag's value within the usage string itself. |
| 152 | Such placeholders are indicated with back quotes. |
| 153 | |
| 154 | For example this: |
| 155 | |
| 156 | <!-- { |
| 157 | "args": ["--help"], |
| 158 | "output": "--config FILE, -c FILE" |
| 159 | } --> |
| 160 | ```go |
| 161 | package main |
| 162 | |
| 163 | import ( |
| 164 | "log" |
| 165 | "os" |
| 166 | |
| 167 | "github.com/urfave/cli/v2" |
| 168 | ) |
| 169 | |
| 170 | func main() { |
| 171 | app := &cli.App{ |
| 172 | Flags: []cli.Flag{ |
| 173 | &cli.StringFlag{ |
| 174 | Name: "config", |
| 175 | Aliases: []string{"c"}, |
| 176 | Usage: "Load configuration from `FILE`", |
| 177 | }, |
| 178 | }, |
| 179 | } |
| 180 | |
| 181 | if err := app.Run(os.Args); err != nil { |
| 182 | log.Fatal(err) |
| 183 | } |
| 184 | } |
| 185 | ``` |
| 186 | |
| 187 | Will result in help output like: |
| 188 | |
| 189 | ``` |
| 190 | --config FILE, -c FILE Load configuration from FILE |
| 191 | ``` |
| 192 | |
| 193 | Note that only the first placeholder is used. Subsequent back-quoted words will |
| 194 | be left as-is. |
| 195 | |
| 196 | #### Alternate Names |
| 197 | |
| 198 | You can set alternate (or short) names for flags by providing a comma-delimited |
| 199 | list for the `Name`. e.g. |
| 200 | |
| 201 | <!-- { |
| 202 | "args": ["--help"], |
| 203 | "output": "--lang value, -l value.*language for the greeting.*default: \"english\"" |
| 204 | } --> |
| 205 | ```go |
| 206 | package main |
| 207 | |
| 208 | import ( |
| 209 | "log" |
| 210 | "os" |
| 211 | |
| 212 | "github.com/urfave/cli/v2" |
| 213 | ) |
| 214 | |
| 215 | func main() { |
| 216 | app := &cli.App{ |
| 217 | Flags: []cli.Flag{ |
| 218 | &cli.StringFlag{ |
| 219 | Name: "lang", |
| 220 | Aliases: []string{"l"}, |
| 221 | Value: "english", |
| 222 | Usage: "language for the greeting", |
| 223 | }, |
| 224 | }, |
| 225 | } |
| 226 | |
| 227 | if err := app.Run(os.Args); err != nil { |
| 228 | log.Fatal(err) |
| 229 | } |
| 230 | } |
| 231 | ``` |
| 232 | |
| 233 | That flag can then be set with `--lang spanish` or `-l spanish`. Note that |
| 234 | giving two different forms of the same flag in the same command invocation is an |
| 235 | error. |
| 236 | |
| 237 | #### Multiple Values per Single Flag |
| 238 | |
| 239 | Using a slice flag allows you to pass multiple values for a single flag; the values will be provided as a slice: |
| 240 | |
| 241 | - `Int64SliceFlag` |
| 242 | - `IntSliceFlag` |
| 243 | - `StringSliceFlag` |
| 244 | |
| 245 | <!-- { |
| 246 | "args": ["--greeting Hello", "--greeting Hola"], |
| 247 | "output": "Hello, Hola" |
| 248 | } --> |
| 249 | ```go |
| 250 | package main |
| 251 | |
| 252 | import ( |
| 253 | "fmt" |
| 254 | "log" |
| 255 | "os" |
| 256 | "strings" |
| 257 | |
| 258 | "github.com/urfave/cli/v3" |
| 259 | ) |
| 260 | |
| 261 | func main() { |
| 262 | app := &cli.App{ |
| 263 | Flags: []cli.Flag{ |
| 264 | &cli.StringSliceFlag{ |
| 265 | Name: "greeting", |
| 266 | Usage: "Pass multiple greetings", |
| 267 | }, |
| 268 | }, |
| 269 | Action: func(cCtx *cli.Context) error { |
| 270 | fmt.Println(strings.Join(cCtx.StringSlice("greeting"), `, `)) |
| 271 | return nil |
| 272 | }, |
| 273 | } |
| 274 | |
| 275 | if err := app.Run(os.Args); err != nil { |
| 276 | log.Fatal(err) |
| 277 | } |
| 278 | } |
| 279 | ``` |
| 280 | |
| 281 | Multiple values need to be passed as separate, repeating flags, e.g. `--greeting Hello --greeting Hola`. |
| 282 | |
| 283 | #### Ordering |
| 284 | |
| 285 | Flags for the application and commands are shown in the order they are defined. |
| 286 | However, it's possible to sort them from outside this library by using `FlagsByName` |
| 287 | or `CommandsByName` with `sort`. |
| 288 | |
| 289 | For example this: |
| 290 | |
| 291 | <!-- { |
| 292 | "args": ["--help"], |
| 293 | "output": ".*Load configuration from FILE\n.*\n.*Language for the greeting.*" |
| 294 | } --> |
| 295 | ```go |
| 296 | package main |
| 297 | |
| 298 | import ( |
| 299 | "log" |
| 300 | "os" |
| 301 | "sort" |
| 302 | |
| 303 | "github.com/urfave/cli/v2" |
| 304 | ) |
| 305 | |
| 306 | func main() { |
| 307 | app := &cli.App{ |
| 308 | Flags: []cli.Flag{ |
| 309 | &cli.StringFlag{ |
| 310 | Name: "lang", |
| 311 | Aliases: []string{"l"}, |
| 312 | Value: "english", |
| 313 | Usage: "Language for the greeting", |
| 314 | }, |
| 315 | &cli.StringFlag{ |
| 316 | Name: "config", |
| 317 | Aliases: []string{"c"}, |
| 318 | Usage: "Load configuration from `FILE`", |
| 319 | }, |
| 320 | }, |
| 321 | Commands: []*cli.Command{ |
| 322 | { |
| 323 | Name: "complete", |
| 324 | Aliases: []string{"c"}, |
| 325 | Usage: "complete a task on the list", |
| 326 | Action: func(*cli.Context) error { |
| 327 | return nil |
| 328 | }, |
| 329 | }, |
| 330 | { |
| 331 | Name: "add", |
| 332 | Aliases: []string{"a"}, |
| 333 | Usage: "add a task to the list", |
| 334 | Action: func(*cli.Context) error { |
| 335 | return nil |
| 336 | }, |
| 337 | }, |
| 338 | }, |
| 339 | } |
| 340 | |
| 341 | sort.Sort(cli.FlagsByName(app.Flags)) |
| 342 | sort.Sort(cli.CommandsByName(app.Commands)) |
| 343 | |
| 344 | if err := app.Run(os.Args); err != nil { |
| 345 | log.Fatal(err) |
| 346 | } |
| 347 | } |
| 348 | ``` |
| 349 | |
| 350 | Will result in help output like: |
| 351 | |
| 352 | ``` |
| 353 | --config FILE, -c FILE Load configuration from FILE |
| 354 | --lang value, -l value Language for the greeting (default: "english") |
| 355 | ``` |
| 356 | |
| 357 | #### Values from the Environment |
| 358 | |
| 359 | You can also have the default value set from the environment via `EnvVars`. e.g. |
| 360 | |
| 361 | <!-- { |
| 362 | "args": ["--help"], |
| 363 | "output": "language for the greeting.*APP_LANG" |
| 364 | } --> |
| 365 | ```go |
| 366 | package main |
| 367 | |
| 368 | import ( |
| 369 | "log" |
| 370 | "os" |
| 371 | |
| 372 | "github.com/urfave/cli/v2" |
| 373 | ) |
| 374 | |
| 375 | func main() { |
| 376 | app := &cli.App{ |
| 377 | Flags: []cli.Flag{ |
| 378 | &cli.StringFlag{ |
| 379 | Name: "lang", |
| 380 | Aliases: []string{"l"}, |
| 381 | Value: "english", |
| 382 | Usage: "language for the greeting", |
| 383 | EnvVars: []string{"APP_LANG"}, |
| 384 | }, |
| 385 | }, |
| 386 | } |
| 387 | |
| 388 | if err := app.Run(os.Args); err != nil { |
| 389 | log.Fatal(err) |
| 390 | } |
| 391 | } |
| 392 | ``` |
| 393 | |
| 394 | If `EnvVars` contains more than one string, the first environment variable that |
| 395 | resolves is used. |
| 396 | |
| 397 | <!-- { |
| 398 | "args": ["--help"], |
| 399 | "output": "language for the greeting.*LEGACY_COMPAT_LANG.*APP_LANG.*LANG" |
| 400 | } --> |
| 401 | ```go |
| 402 | package main |
| 403 | |
| 404 | import ( |
| 405 | "log" |
| 406 | "os" |
| 407 | |
| 408 | "github.com/urfave/cli/v2" |
| 409 | ) |
| 410 | |
| 411 | func main() { |
| 412 | app := &cli.App{ |
| 413 | Flags: []cli.Flag{ |
| 414 | &cli.StringFlag{ |
| 415 | Name: "lang", |
| 416 | Aliases: []string{"l"}, |
| 417 | Value: "english", |
| 418 | Usage: "language for the greeting", |
| 419 | EnvVars: []string{"LEGACY_COMPAT_LANG", "APP_LANG", "LANG"}, |
| 420 | }, |
| 421 | }, |
| 422 | } |
| 423 | |
| 424 | if err := app.Run(os.Args); err != nil { |
| 425 | log.Fatal(err) |
| 426 | } |
| 427 | } |
| 428 | ``` |
| 429 | |
| 430 | #### Values from files |
| 431 | |
| 432 | You can also have the default value set from file via `FilePath`. e.g. |
| 433 | |
| 434 | <!-- { |
| 435 | "args": ["--help"], |
| 436 | "output": "password for the mysql database" |
| 437 | } --> |
| 438 | ```go |
| 439 | package main |
| 440 | |
| 441 | import ( |
| 442 | "log" |
| 443 | "os" |
| 444 | |
| 445 | "github.com/urfave/cli/v2" |
| 446 | ) |
| 447 | |
| 448 | func main() { |
| 449 | app := &cli.App{ |
| 450 | Flags: []cli.Flag{ |
| 451 | &cli.StringFlag{ |
| 452 | Name: "password", |
| 453 | Aliases: []string{"p"}, |
| 454 | Usage: "password for the mysql database", |
| 455 | FilePath: "/etc/mysql/password", |
| 456 | }, |
| 457 | }, |
| 458 | } |
| 459 | |
| 460 | if err := app.Run(os.Args); err != nil { |
| 461 | log.Fatal(err) |
| 462 | } |
| 463 | } |
| 464 | ``` |
| 465 | |
| 466 | Note that default values set from file (e.g. `FilePath`) take precedence over |
| 467 | default values set from the environment (e.g. `EnvVar`). |
| 468 | |
| 469 | #### Values from alternate input sources (YAML, TOML, and others) |
| 470 | |
| 471 | There is a separate package altsrc that adds support for getting flag values |
| 472 | from other file input sources. |
| 473 | |
| 474 | Currently supported input source formats: |
| 475 | |
| 476 | - YAML |
| 477 | - JSON |
| 478 | - TOML |
| 479 | |
| 480 | In order to get values for a flag from an alternate input source the following |
| 481 | code would be added to wrap an existing cli.Flag like below: |
| 482 | |
| 483 | ```go |
| 484 | // --- >8 --- |
| 485 | altsrc.NewIntFlag(&cli.IntFlag{Name: "test"}) |
| 486 | ``` |
| 487 | |
| 488 | Initialization must also occur for these flags. Below is an example initializing |
| 489 | getting data from a yaml file below. |
| 490 | |
| 491 | ```go |
| 492 | // --- >8 --- |
| 493 | command.Before = altsrc.InitInputSourceWithContext(command.Flags, NewYamlSourceFromFlagFunc("load")) |
| 494 | ``` |
| 495 | |
| 496 | The code above will use the "load" string as a flag name to get the file name of |
| 497 | a yaml file from the cli.Context. It will then use that file name to initialize |
| 498 | the yaml input source for any flags that are defined on that command. As a note |
| 499 | the "load" flag used would also have to be defined on the command flags in order |
| 500 | for this code snippet to work. |
| 501 | |
| 502 | Currently only YAML, JSON, and TOML files are supported but developers can add |
| 503 | support for other input sources by implementing the altsrc.InputSourceContext |
| 504 | for their given sources. |
| 505 | |
| 506 | Here is a more complete sample of a command using YAML support: |
| 507 | |
| 508 | <!-- { |
| 509 | "args": ["--help"], |
| 510 | "output": "--test value.*default: 0" |
| 511 | } --> |
| 512 | ```go |
| 513 | package main |
| 514 | |
| 515 | import ( |
| 516 | "fmt" |
| 517 | "os" |
| 518 | |
| 519 | "github.com/urfave/cli/v2" |
| 520 | "github.com/urfave/cli/v2/altsrc" |
| 521 | ) |
| 522 | |
| 523 | func main() { |
| 524 | flags := []cli.Flag{ |
| 525 | altsrc.NewIntFlag(&cli.IntFlag{Name: "test"}), |
| 526 | &cli.StringFlag{Name: "load"}, |
| 527 | } |
| 528 | |
| 529 | app := &cli.App{ |
| 530 | Action: func(*cli.Context) error { |
| 531 | fmt.Println("--test value.*default: 0") |
| 532 | return nil |
| 533 | }, |
| 534 | Before: altsrc.InitInputSourceWithContext(flags, altsrc.NewYamlSourceFromFlagFunc("load")), |
| 535 | Flags: flags, |
| 536 | } |
| 537 | |
| 538 | app.Run(os.Args) |
| 539 | } |
| 540 | ``` |
| 541 | |
| 542 | #### Required Flags |
| 543 | |
| 544 | You can make a flag required by setting the `Required` field to `true`. If a user |
| 545 | does not provide a required flag, they will be shown an error message. |
| 546 | |
| 547 | Take for example this app that requires the `lang` flag: |
| 548 | |
| 549 | <!-- { |
| 550 | "error": "Required flag \"lang\" not set" |
| 551 | } --> |
| 552 | ```go |
| 553 | package main |
| 554 | |
| 555 | import ( |
| 556 | "fmt" |
| 557 | "log" |
| 558 | "os" |
| 559 | |
| 560 | "github.com/urfave/cli/v2" |
| 561 | ) |
| 562 | |
| 563 | func main() { |
| 564 | app := &cli.App{ |
| 565 | Flags: []cli.Flag{ |
| 566 | &cli.StringFlag{ |
| 567 | Name: "lang", |
| 568 | Value: "english", |
| 569 | Usage: "language for the greeting", |
| 570 | Required: true, |
| 571 | }, |
| 572 | }, |
| 573 | Action: func(cCtx *cli.Context) error { |
| 574 | output := "Hello" |
| 575 | if cCtx.String("lang") == "spanish" { |
| 576 | output = "Hola" |
| 577 | } |
| 578 | fmt.Println(output) |
| 579 | return nil |
| 580 | }, |
| 581 | } |
| 582 | |
| 583 | if err := app.Run(os.Args); err != nil { |
| 584 | log.Fatal(err) |
| 585 | } |
| 586 | } |
| 587 | ``` |
| 588 | |
| 589 | If the app is run without the `lang` flag, the user will see the following message |
| 590 | |
| 591 | ``` |
| 592 | Required flag "lang" not set |
| 593 | ``` |
| 594 | |
| 595 | #### Default Values for help output |
| 596 | |
| 597 | Sometimes it's useful to specify a flag's default help-text value within the |
| 598 | flag declaration. This can be useful if the default value for a flag is a |
| 599 | computed value. The default value can be set via the `DefaultText` struct field. |
| 600 | |
| 601 | For example this: |
| 602 | |
| 603 | <!-- { |
| 604 | "args": ["--help"], |
| 605 | "output": "--port value" |
| 606 | } --> |
| 607 | ```go |
| 608 | package main |
| 609 | |
| 610 | import ( |
| 611 | "log" |
| 612 | "os" |
| 613 | |
| 614 | "github.com/urfave/cli/v2" |
| 615 | ) |
| 616 | |
| 617 | func main() { |
| 618 | app := &cli.App{ |
| 619 | Flags: []cli.Flag{ |
| 620 | &cli.IntFlag{ |
| 621 | Name: "port", |
| 622 | Usage: "Use a randomized port", |
| 623 | Value: 0, |
| 624 | DefaultText: "random", |
| 625 | }, |
| 626 | }, |
| 627 | } |
| 628 | |
| 629 | if err := app.Run(os.Args); err != nil { |
| 630 | log.Fatal(err) |
| 631 | } |
| 632 | } |
| 633 | ``` |
| 634 | |
| 635 | Will result in help output like: |
| 636 | |
| 637 | ``` |
| 638 | --port value Use a randomized port (default: random) |
| 639 | ``` |
| 640 | |
| 641 | #### Precedence |
| 642 | |
| 643 | The precedence for flag value sources is as follows (highest to lowest): |
| 644 | |
| 645 | 0. Command line flag value from user |
| 646 | 0. Environment variable (if specified) |
| 647 | 0. Configuration file (if specified) |
| 648 | 0. Default defined on the flag |
| 649 | |
| 650 | #### Flag Actions |
| 651 | |
| 652 | Handlers can be registered per flag which are triggered after a flag has been processed. |
| 653 | This can be used for a variety of purposes, one of which is flag validation |
| 654 | |
| 655 | <!-- { |
| 656 | "args": ["--port","70000"], |
| 657 | "error": "Flag port value 70000 out of range[0-65535]" |
| 658 | } --> |
| 659 | ```go |
| 660 | package main |
| 661 | |
| 662 | import ( |
| 663 | "log" |
| 664 | "os" |
| 665 | "fmt" |
| 666 | |
| 667 | "github.com/urfave/cli/v2" |
| 668 | ) |
| 669 | |
| 670 | func main() { |
| 671 | app := &cli.App{ |
| 672 | Flags: []cli.Flag{ |
| 673 | &cli.IntFlag{ |
| 674 | Name: "port", |
| 675 | Usage: "Use a randomized port", |
| 676 | Value: 0, |
| 677 | DefaultText: "random", |
| 678 | Action: func(ctx *cli.Context, v int) error { |
| 679 | if v >= 65536 { |
| 680 | return fmt.Errorf("Flag port value %v out of range[0-65535]", v) |
| 681 | } |
| 682 | return nil |
| 683 | }, |
| 684 | }, |
| 685 | }, |
| 686 | } |
| 687 | |
| 688 | if err := app.Run(os.Args); err != nil { |
| 689 | log.Fatal(err) |
| 690 | } |
| 691 | } |
| 692 | ``` |
| 693 | |
| 694 | Will result in help output like: |
| 695 | |
| 696 | ``` |
| 697 | Flag port value 70000 out of range[0-65535] |
| 698 | ``` |