| 1 | --- |
| 2 | tags: |
| 3 | - v3 |
| 4 | search: |
| 5 | boost: 2 |
| 6 | --- |
| 7 | |
| 8 | Flags can have their default values set from different sources. The following sources are |
| 9 | provided by default with `urfave/cli` |
| 10 | |
| 11 | - Environment |
| 12 | - Text Files |
| 13 | |
| 14 | The library also provides a framework for users to plugin their own implementation of value sources |
| 15 | to be fetched via other mechanisms(http and so on). |
| 16 | |
| 17 | In addition there is a `urfave/cli-altsrc` repo which hosts some common value sources to read |
| 18 | from files or via http/https. |
| 19 | |
| 20 | - YAML |
| 21 | - JSON |
| 22 | - TOML |
| 23 | |
| 24 | #### Values from the Environment |
| 25 | |
| 26 | To set a value from the environment use `cli.EnvVars`. e.g. |
| 27 | |
| 28 | <!-- { |
| 29 | "args": ["--help"], |
| 30 | "output": "language for the greeting.*APP_LANG" |
| 31 | } --> |
| 32 | ```go |
| 33 | package main |
| 34 | |
| 35 | import ( |
| 36 | "log" |
| 37 | "os" |
| 38 | "context" |
| 39 | |
| 40 | "github.com/urfave/cli/v3" |
| 41 | ) |
| 42 | |
| 43 | func main() { |
| 44 | cmd := &cli.Command{ |
| 45 | Flags: []cli.Flag{ |
| 46 | &cli.StringFlag{ |
| 47 | Name: "lang", |
| 48 | Aliases: []string{"l"}, |
| 49 | Value: "english", |
| 50 | Usage: "language for the greeting", |
| 51 | Sources: cli.EnvVars("APP_LANG"), |
| 52 | }, |
| 53 | }, |
| 54 | } |
| 55 | |
| 56 | if err := cmd.Run(context.Background(), os.Args); err != nil { |
| 57 | log.Fatal(err) |
| 58 | } |
| 59 | } |
| 60 | ``` |
| 61 | |
| 62 | If `cli.EnvVars` contains more than one string, the first environment variable that |
| 63 | resolves is used. |
| 64 | |
| 65 | <!-- { |
| 66 | "args": ["--help"], |
| 67 | "output": "language for the greeting.*LEGACY_COMPAT_LANG.*APP_LANG.*LANG" |
| 68 | } --> |
| 69 | ```go |
| 70 | package main |
| 71 | |
| 72 | import ( |
| 73 | "log" |
| 74 | "os" |
| 75 | "context" |
| 76 | |
| 77 | "github.com/urfave/cli/v3" |
| 78 | ) |
| 79 | |
| 80 | func main() { |
| 81 | cmd := &cli.Command{ |
| 82 | Flags: []cli.Flag{ |
| 83 | &cli.StringFlag{ |
| 84 | Name: "lang", |
| 85 | Aliases: []string{"l"}, |
| 86 | Value: "english", |
| 87 | Usage: "language for the greeting", |
| 88 | Sources: cli.EnvVars("LEGACY_COMPAT_LANG", "APP_LANG", "LANG"), |
| 89 | }, |
| 90 | }, |
| 91 | } |
| 92 | |
| 93 | if err := cmd.Run(context.Background(), os.Args); err != nil { |
| 94 | log.Fatal(err) |
| 95 | } |
| 96 | } |
| 97 | ``` |
| 98 | |
| 99 | #### Values from files |
| 100 | |
| 101 | You can also have the default value set from file via `cli.File`. e.g. |
| 102 | |
| 103 | <!-- { |
| 104 | "args": ["--help"], |
| 105 | "output": "password for the mysql database" |
| 106 | } --> |
| 107 | ```go |
| 108 | package main |
| 109 | |
| 110 | import ( |
| 111 | "log" |
| 112 | "os" |
| 113 | "context" |
| 114 | |
| 115 | "github.com/urfave/cli/v3" |
| 116 | ) |
| 117 | |
| 118 | func main() { |
| 119 | cmd := &cli.Command{ |
| 120 | Flags: []cli.Flag{ |
| 121 | &cli.StringFlag{ |
| 122 | Name: "password", |
| 123 | Aliases: []string{"p"}, |
| 124 | Usage: "password for the mysql database", |
| 125 | Sources: cli.Files("/etc/mysql/password"), |
| 126 | }, |
| 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 default values are set in the same order as they are defined in the |
| 137 | `Sources` param. This allows the user to choose order of priority |
| 138 | |
| 139 | #### Values from alternate input sources (YAML, TOML, and others) |
| 140 | |
| 141 | There is a separate package [altsrc](https://github.com/urfave/cli-altsrc) that adds support for getting flag values |
| 142 | from other file input sources. |
| 143 | |
| 144 | Currently supported input source formats by that library are: |
| 145 | |
| 146 | - YAML |
| 147 | - JSON |
| 148 | - TOML |
| 149 | |
| 150 | A simple straight forward usage would be |
| 151 | |
| 152 | ```go |
| 153 | package main |
| 154 | |
| 155 | import ( |
| 156 | "log" |
| 157 | "os" |
| 158 | "context" |
| 159 | |
| 160 | "github.com/urfave/cli/v3" |
| 161 | "github.com/urfave/cli-altsrc/v3" |
| 162 | yaml "github.com/urfave/cli-altsrc/v3/yaml" |
| 163 | ) |
| 164 | |
| 165 | func main() { |
| 166 | cmd := &cli.Command{ |
| 167 | Flags: []cli.Flag{ |
| 168 | &cli.StringFlag{ |
| 169 | Name: "password", |
| 170 | Aliases: []string{"p"}, |
| 171 | Usage: "password for the mysql database", |
| 172 | Sources: cli.NewValueSourceChain(yaml.YAML("somekey", altsrc.StringSourcer("/path/to/filename"))), |
| 173 | }, |
| 174 | }, |
| 175 | } |
| 176 | |
| 177 | if err := cmd.Run(context.Background(), os.Args); err != nil { |
| 178 | log.Fatal(err) |
| 179 | } |
| 180 | } |
| 181 | ``` |
| 182 | |
| 183 | Sometime the source name is itself provided by another CLI flag. To allow the library to "lazy-load" |
| 184 | the file when needed we use the `altsrc.NewStringPtrSourcer` function to bind the value of the flag |
| 185 | to a pointer that is set as a destination of another flag |
| 186 | |
| 187 | ```go |
| 188 | package main |
| 189 | |
| 190 | import ( |
| 191 | "log" |
| 192 | "os" |
| 193 | "context" |
| 194 | |
| 195 | "github.com/urfave/cli/v3" |
| 196 | "github.com/urfave/cli-altsrc/v3" |
| 197 | yaml "github.com/urfave/cli-altsrc/v3/yaml" |
| 198 | ) |
| 199 | |
| 200 | func main() { |
| 201 | var filename string |
| 202 | cmd := &cli.Command{ |
| 203 | Flags: []cli.Flag{ |
| 204 | &cli.StringFlag{ |
| 205 | Name: "file", |
| 206 | Aliases: []string{"f"}, |
| 207 | Value: "/path/to/default", |
| 208 | Usage: "filename for mysql database", |
| 209 | Destination: &filename, |
| 210 | }, |
| 211 | &cli.StringFlag{ |
| 212 | Name: "password", |
| 213 | Aliases: []string{"p"}, |
| 214 | Usage: "password for the mysql database", |
| 215 | Sources: cli.NewValueSourceChain(yaml.YAML("somekey", altsrc.NewStringPtrSourcer(&filename))), |
| 216 | }, |
| 217 | }, |
| 218 | } |
| 219 | |
| 220 | if err := cmd.Run(context.Background(), os.Args); err != nil { |
| 221 | log.Fatal(err) |
| 222 | } |
| 223 | } |
| 224 | ``` |