@@ -7,17 +7,19 @@ search:
77
88Flags can have their default values set from different sources. The following sources are
99provided by default with ` urfave/cli `
10+
1011 - Environment
1112 - Text Files
1213
1314The library also provides a framework for users to plugin their own implementation of value sources
1415to be fetched via other mechanisms(http and so on).
1516
1617In addition there is a ` urfave/cli-altsrc ` repo which hosts some common value sources to read
18+ from files or via http/https.
19+
1720 - YAML
1821 - JSON
1922 - TOML
20- from files or via http/https.
2123
2224#### Values from the Environment
2325
@@ -136,78 +138,85 @@ Note that default values are set in the same order as they are defined in the
136138
137139#### Values from alternate input sources (YAML, TOML, and others)
138140
139- There is a separate package altsrc that adds support for getting flag values
141+ There is a separate package [ altsrc] ( https://github.com/urfave/cli-altsrc ) that adds support for getting flag values
140142from other file input sources.
141143
142- Currently supported input source formats:
144+ Currently supported input source formats by that library are :
143145
144146- YAML
145147- JSON
146148- TOML
147149
148- In order to get values for a flag from an alternate input source the following
149- code would be added to wrap an existing cli.Flag like below:
150+ A simple straight forward usage would be
150151
151152``` go
152- // --- >8 ---
153- altsrc.NewIntFlag (&cli.IntFlag {Name: " test" })
154- ```
153+ package main
155154
156- Initialization must also occur for these flags. Below is an example initializing
157- getting data from a yaml file below.
155+ import (
156+ " log"
157+ " os"
158+ " context"
158159
159- ``` go
160- // --- >8 ---
161- command.Before = func (ctx context.Context , cmd *Command) (context.Context , error ) {
162- return ctx, altsrc.InitInputSourceWithContext (command.Flags , NewYamlSourceFromFlagFunc (" load" ))
163- }
164- ```
160+ " github.com/urfave/cli/v3"
161+ " github.com/urfave/cli-altsrc/v3"
162+ )
165163
166- The code above will use the "load" string as a flag name to get the file name of
167- a yaml file from the cli.Context. It will then use that file name to initialize
168- the yaml input source for any flags that are defined on that command. As a note
169- the "load" flag used would also have to be defined on the command flags in order
170- for this code snippet to work.
164+ func main () {
165+ cmd := &cli.Command {
166+ Flags: []cli.Flag {
167+ &cli.StringFlag {
168+ Name: " password" ,
169+ Aliases: []string {" p" },
170+ Usage: " password for the mysql database" ,
171+ Sources: altsrc.YAML (" somekey" , altsrc.StringSourcer (" /path/to/filename" )),
172+ },
173+ },
174+ }
171175
172- Currently only YAML, JSON, and TOML files are supported but developers can add
173- support for other input sources by implementing the altsrc.InputSourceContext
174- for their given sources.
176+ if err := cmd.Run (context.Background (), os.Args ); err != nil {
177+ log.Fatal (err)
178+ }
179+ }
180+ ```
175181
176- Here is a more complete sample of a command using YAML support:
182+ Sometime the source name is itself provided by another CLI flag. To allow the library to "lazy-load"
183+ the file when needed we use the ` altsrc.NewStringPtrSourcer ` function to bind the value of the flag
184+ to a pointer that is set as a destination of another flag
177185
178- <!-- {
179- "args": ["--help"],
180- "output": "--test int.*default: 0"
181- } -->
182186``` go
183187package main
184188
185189import (
186- " context"
187- " fmt"
190+ " log"
188191 " os"
192+ " context"
189193
190- altsrc " github.com/urfave/cli-altsrc/v3"
191194 " github.com/urfave/cli/v3"
195+ " github.com/urfave/cli-altsrc/v3"
192196)
193197
194198func main () {
195- flags := []cli.Flag {
196- &cli.IntFlag {
197- Name: " test" ,
198- Sources: altsrc.YAML (" key" , " /path/to/file" ),
199- },
200- &cli.StringFlag {Name: " load" },
201- }
202-
199+ var filename string
203200 cmd := &cli.Command {
204- Action: func (context.Context , *cli.Command ) error {
205- fmt.Println (" --test value.*default: 0" )
206- return nil
201+ Flags: []cli.Flag {
202+ &cli.StringFlag {
203+ Name: " file" ,
204+ Aliases: []string {" f" },
205+ Value: " /path/to/default" ,
206+ Usage: " filename for mysql database" ,
207+ Destination: &filename,
208+ },
209+ &cli.StringFlag {
210+ Name: " password" ,
211+ Aliases: []string {" p" },
212+ Usage: " password for the mysql database" ,
213+ Sources: altsrc.YAML (" somekey" , altsrc.NewStringPtrSourcer (&filename)),
214+ },
207215 },
208- Flags: flags,
209216 }
210217
211- cmd.Run (context.Background (), os.Args )
218+ if err := cmd.Run (context.Background (), os.Args ); err != nil {
219+ log.Fatal (err)
220+ }
212221}
213- ```
222+ ```
0 commit comments