| 1 | --- |
| 2 | tags: |
| 3 | - v1 |
| 4 | --- |
| 5 | |
| 6 | Setting and querying flags is simple. |
| 7 | |
| 8 | <!-- { |
| 9 | "output": "Hello Nefertiti" |
| 10 | } --> |
| 11 | ``` go |
| 12 | package main |
| 13 | |
| 14 | import ( |
| 15 | "fmt" |
| 16 | "log" |
| 17 | "os" |
| 18 | |
| 19 | "github.com/urfave/cli" |
| 20 | ) |
| 21 | |
| 22 | func main() { |
| 23 | app := cli.NewApp() |
| 24 | |
| 25 | app.Flags = []cli.Flag { |
| 26 | cli.StringFlag{ |
| 27 | Name: "lang", |
| 28 | Value: "english", |
| 29 | Usage: "language for the greeting", |
| 30 | }, |
| 31 | } |
| 32 | |
| 33 | app.Action = func(c *cli.Context) error { |
| 34 | name := "Nefertiti" |
| 35 | if c.NArg() > 0 { |
| 36 | name = c.Args().Get(0) |
| 37 | } |
| 38 | if c.String("lang") == "spanish" { |
| 39 | fmt.Println("Hola", name) |
| 40 | } else { |
| 41 | fmt.Println("Hello", name) |
| 42 | } |
| 43 | return nil |
| 44 | } |
| 45 | |
| 46 | err := app.Run(os.Args) |
| 47 | if 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. |
| 55 | |
| 56 | <!-- { |
| 57 | "output": "Hello someone" |
| 58 | } --> |
| 59 | ``` go |
| 60 | package main |
| 61 | |
| 62 | import ( |
| 63 | "log" |
| 64 | "os" |
| 65 | "fmt" |
| 66 | |
| 67 | "github.com/urfave/cli" |
| 68 | ) |
| 69 | |
| 70 | func main() { |
| 71 | var language string |
| 72 | |
| 73 | app := cli.NewApp() |
| 74 | |
| 75 | app.Flags = []cli.Flag { |
| 76 | cli.StringFlag{ |
| 77 | Name: "lang", |
| 78 | Value: "english", |
| 79 | Usage: "language for the greeting", |
| 80 | Destination: &language, |
| 81 | }, |
| 82 | } |
| 83 | |
| 84 | app.Action = func(c *cli.Context) error { |
| 85 | name := "someone" |
| 86 | if c.NArg() > 0 { |
| 87 | name = c.Args()[0] |
| 88 | } |
| 89 | if language == "spanish" { |
| 90 | fmt.Println("Hola", name) |
| 91 | } else { |
| 92 | fmt.Println("Hello", name) |
| 93 | } |
| 94 | return nil |
| 95 | } |
| 96 | |
| 97 | err := app.Run(os.Args) |
| 98 | if err != nil { |
| 99 | log.Fatal(err) |
| 100 | } |
| 101 | } |
| 102 | ``` |
| 103 | |
| 104 | See full list of flags at https://pkg.go.dev/github.com/urfave/cli |
| 105 | |
| 106 | #### Placeholder Values |
| 107 | |
| 108 | Sometimes it's useful to specify a flag's value within the usage string itself. |
| 109 | Such placeholders are indicated with back quotes. |
| 110 | |
| 111 | For example this: |
| 112 | |
| 113 | <!-- { |
| 114 | "args": ["--help"], |
| 115 | "output": "--config FILE, -c FILE" |
| 116 | } --> |
| 117 | ```go |
| 118 | package main |
| 119 | |
| 120 | import ( |
| 121 | "log" |
| 122 | "os" |
| 123 | |
| 124 | "github.com/urfave/cli" |
| 125 | ) |
| 126 | |
| 127 | func main() { |
| 128 | app := cli.NewApp() |
| 129 | |
| 130 | app.Flags = []cli.Flag{ |
| 131 | cli.StringFlag{ |
| 132 | Name: "config, c", |
| 133 | Usage: "Load configuration from `FILE`", |
| 134 | }, |
| 135 | } |
| 136 | |
| 137 | err := app.Run(os.Args) |
| 138 | if err != nil { |
| 139 | log.Fatal(err) |
| 140 | } |
| 141 | } |
| 142 | ``` |
| 143 | |
| 144 | Will result in help output like: |
| 145 | |
| 146 | ``` |
| 147 | --config FILE, -c FILE Load configuration from FILE |
| 148 | ``` |
| 149 | |
| 150 | Note that only the first placeholder is used. Subsequent back-quoted words will |
| 151 | be left as-is. |
| 152 | |
| 153 | #### Alternate Names |
| 154 | |
| 155 | You can set alternate (or short) names for flags by providing a comma-delimited |
| 156 | list for the `Name`. e.g. |
| 157 | |
| 158 | <!-- { |
| 159 | "args": ["--help"], |
| 160 | "output": "--lang value, -l value.*language for the greeting.*default: \"english\"" |
| 161 | } --> |
| 162 | ``` go |
| 163 | package main |
| 164 | |
| 165 | import ( |
| 166 | "log" |
| 167 | "os" |
| 168 | |
| 169 | "github.com/urfave/cli" |
| 170 | ) |
| 171 | |
| 172 | func main() { |
| 173 | app := cli.NewApp() |
| 174 | |
| 175 | app.Flags = []cli.Flag { |
| 176 | cli.StringFlag{ |
| 177 | Name: "lang, l", |
| 178 | Value: "english", |
| 179 | Usage: "language for the greeting", |
| 180 | }, |
| 181 | } |
| 182 | |
| 183 | err := app.Run(os.Args) |
| 184 | if err != nil { |
| 185 | log.Fatal(err) |
| 186 | } |
| 187 | } |
| 188 | ``` |
| 189 | |
| 190 | That flag can then be set with `--lang spanish` or `-l spanish`. Note that |
| 191 | giving two different forms of the same flag in the same command invocation is an |
| 192 | error. |
| 193 | |
| 194 | #### Ordering |
| 195 | |
| 196 | Flags for the application and commands are shown in the order they are defined. |
| 197 | However, it's possible to sort them from outside this library by using `FlagsByName` |
| 198 | or `CommandsByName` with `sort`. |
| 199 | |
| 200 | For example this: |
| 201 | |
| 202 | <!-- { |
| 203 | "args": ["--help"], |
| 204 | "output": "add a task to the list\n.*complete a task on the list\n.*\n\n.*\n.*Load configuration from FILE\n.*Language for the greeting.*" |
| 205 | } --> |
| 206 | ``` go |
| 207 | package main |
| 208 | |
| 209 | import ( |
| 210 | "log" |
| 211 | "os" |
| 212 | "sort" |
| 213 | |
| 214 | "github.com/urfave/cli" |
| 215 | ) |
| 216 | |
| 217 | func main() { |
| 218 | app := cli.NewApp() |
| 219 | |
| 220 | app.Flags = []cli.Flag { |
| 221 | cli.StringFlag{ |
| 222 | Name: "lang, l", |
| 223 | Value: "english", |
| 224 | Usage: "Language for the greeting", |
| 225 | }, |
| 226 | cli.StringFlag{ |
| 227 | Name: "config, c", |
| 228 | Usage: "Load configuration from `FILE`", |
| 229 | }, |
| 230 | } |
| 231 | |
| 232 | app.Commands = []cli.Command{ |
| 233 | { |
| 234 | Name: "complete", |
| 235 | Aliases: []string{"c"}, |
| 236 | Usage: "complete a task on the list", |
| 237 | Action: func(c *cli.Context) error { |
| 238 | return nil |
| 239 | }, |
| 240 | }, |
| 241 | { |
| 242 | Name: "add", |
| 243 | Aliases: []string{"a"}, |
| 244 | Usage: "add a task to the list", |
| 245 | Action: func(c *cli.Context) error { |
| 246 | return nil |
| 247 | }, |
| 248 | }, |
| 249 | } |
| 250 | |
| 251 | sort.Sort(cli.FlagsByName(app.Flags)) |
| 252 | sort.Sort(cli.CommandsByName(app.Commands)) |
| 253 | |
| 254 | err := app.Run(os.Args) |
| 255 | if err != nil { |
| 256 | log.Fatal(err) |
| 257 | } |
| 258 | } |
| 259 | ``` |
| 260 | |
| 261 | Will result in help output like: |
| 262 | |
| 263 | ``` |
| 264 | --config FILE, -c FILE Load configuration from FILE |
| 265 | --lang value, -l value Language for the greeting (default: "english") |
| 266 | ``` |
| 267 | |
| 268 | #### Values from the Environment |
| 269 | |
| 270 | You can also have the default value set from the environment via `EnvVar`. e.g. |
| 271 | |
| 272 | <!-- { |
| 273 | "args": ["--help"], |
| 274 | "output": "language for the greeting.*APP_LANG" |
| 275 | } --> |
| 276 | ``` go |
| 277 | package main |
| 278 | |
| 279 | import ( |
| 280 | "log" |
| 281 | "os" |
| 282 | |
| 283 | "github.com/urfave/cli" |
| 284 | ) |
| 285 | |
| 286 | func main() { |
| 287 | app := cli.NewApp() |
| 288 | |
| 289 | app.Flags = []cli.Flag { |
| 290 | cli.StringFlag{ |
| 291 | Name: "lang, l", |
| 292 | Value: "english", |
| 293 | Usage: "language for the greeting", |
| 294 | EnvVar: "APP_LANG", |
| 295 | }, |
| 296 | } |
| 297 | |
| 298 | err := app.Run(os.Args) |
| 299 | if err != nil { |
| 300 | log.Fatal(err) |
| 301 | } |
| 302 | } |
| 303 | ``` |
| 304 | |
| 305 | The `EnvVar` may also be given as a comma-delimited "cascade", where the first |
| 306 | environment variable that resolves is used as the default. |
| 307 | |
| 308 | <!-- { |
| 309 | "args": ["--help"], |
| 310 | "output": "language for the greeting.*LEGACY_COMPAT_LANG.*APP_LANG.*LANG" |
| 311 | } --> |
| 312 | ``` go |
| 313 | package main |
| 314 | |
| 315 | import ( |
| 316 | "log" |
| 317 | "os" |
| 318 | |
| 319 | "github.com/urfave/cli" |
| 320 | ) |
| 321 | |
| 322 | func main() { |
| 323 | app := cli.NewApp() |
| 324 | |
| 325 | app.Flags = []cli.Flag { |
| 326 | cli.StringFlag{ |
| 327 | Name: "lang, l", |
| 328 | Value: "english", |
| 329 | Usage: "language for the greeting", |
| 330 | EnvVar: "LEGACY_COMPAT_LANG,APP_LANG,LANG", |
| 331 | }, |
| 332 | } |
| 333 | |
| 334 | err := app.Run(os.Args) |
| 335 | if err != nil { |
| 336 | log.Fatal(err) |
| 337 | } |
| 338 | } |
| 339 | ``` |
| 340 | |
| 341 | #### Values from files |
| 342 | |
| 343 | You can also have the default value set from file via `FilePath`. e.g. |
| 344 | |
| 345 | <!-- { |
| 346 | "args": ["--help"], |
| 347 | "output": "password for the mysql database" |
| 348 | } --> |
| 349 | ``` go |
| 350 | package main |
| 351 | |
| 352 | import ( |
| 353 | "log" |
| 354 | "os" |
| 355 | |
| 356 | "github.com/urfave/cli" |
| 357 | ) |
| 358 | |
| 359 | func main() { |
| 360 | app := cli.NewApp() |
| 361 | |
| 362 | app.Flags = []cli.Flag { |
| 363 | cli.StringFlag{ |
| 364 | Name: "password, p", |
| 365 | Usage: "password for the mysql database", |
| 366 | FilePath: "/etc/mysql/password", |
| 367 | }, |
| 368 | } |
| 369 | |
| 370 | err := app.Run(os.Args) |
| 371 | if err != nil { |
| 372 | log.Fatal(err) |
| 373 | } |
| 374 | } |
| 375 | ``` |
| 376 | |
| 377 | Note that default values set from file (e.g. `FilePath`) take precedence over |
| 378 | default values set from the environment (e.g. `EnvVar`). |
| 379 | |
| 380 | #### Values from alternate input sources (YAML, TOML, and others) |
| 381 | |
| 382 | There is a separate package altsrc that adds support for getting flag values |
| 383 | from other file input sources. |
| 384 | |
| 385 | Currently supported input source formats: |
| 386 | * YAML |
| 387 | * JSON |
| 388 | * TOML |
| 389 | |
| 390 | In order to get values for a flag from an alternate input source the following |
| 391 | code would be added to wrap an existing cli.Flag like below: |
| 392 | |
| 393 | ``` go |
| 394 | altsrc.NewIntFlag(cli.IntFlag{Name: "test"}) |
| 395 | ``` |
| 396 | |
| 397 | Initialization must also occur for these flags. Below is an example initializing |
| 398 | getting data from a yaml file below. |
| 399 | |
| 400 | ``` go |
| 401 | command.Before = altsrc.InitInputSourceWithContext(command.Flags, NewYamlSourceFromFlagFunc("load")) |
| 402 | ``` |
| 403 | |
| 404 | The code above will use the "load" string as a flag name to get the file name of |
| 405 | a yaml file from the cli.Context. It will then use that file name to initialize |
| 406 | the yaml input source for any flags that are defined on that command. As a note |
| 407 | the "load" flag used would also have to be defined on the command flags in order |
| 408 | for this code snippet to work. |
| 409 | |
| 410 | Currently only YAML, JSON, and TOML files are supported but developers can add support |
| 411 | for other input sources by implementing the altsrc.InputSourceContext for their |
| 412 | given sources. |
| 413 | |
| 414 | Here is a more complete sample of a command using YAML support: |
| 415 | |
| 416 | <!-- { |
| 417 | "args": ["--help"], |
| 418 | "output": "--test value.*default: 0" |
| 419 | } --> |
| 420 | ``` go |
| 421 | package notmain |
| 422 | |
| 423 | import ( |
| 424 | "fmt" |
| 425 | "log" |
| 426 | "os" |
| 427 | |
| 428 | "github.com/urfave/cli" |
| 429 | "github.com/urfave/cli/altsrc" |
| 430 | ) |
| 431 | |
| 432 | func main() { |
| 433 | app := cli.NewApp() |
| 434 | |
| 435 | flags := []cli.Flag{ |
| 436 | altsrc.NewIntFlag(cli.IntFlag{Name: "test"}), |
| 437 | cli.StringFlag{Name: "load"}, |
| 438 | } |
| 439 | |
| 440 | app.Action = func(c *cli.Context) error { |
| 441 | fmt.Println("yaml is rad") |
| 442 | return nil |
| 443 | } |
| 444 | |
| 445 | app.Before = altsrc.InitInputSourceWithContext(flags, altsrc.NewYamlSourceFromFlagFunc("load")) |
| 446 | app.Flags = flags |
| 447 | |
| 448 | err := app.Run(os.Args) |
| 449 | if err != nil { |
| 450 | log.Fatal(err) |
| 451 | } |
| 452 | } |
| 453 | ``` |
| 454 | |
| 455 | #### Precedence |
| 456 | |
| 457 | The precedence for flag value sources is as follows (highest to lowest): |
| 458 | |
| 459 | 0. Command line flag value from user |
| 460 | 0. Environment variable (if specified) |
| 461 | 0. Configuration file (if specified) |
| 462 | 0. Default defined on the flag |