@@ -136,78 +136,85 @@ Note that default values are set in the same order as they are defined in the
136136
137137#### Values from alternate input sources (YAML, TOML, and others)
138138
139- There is a separate package altsrc that adds support for getting flag values
139+ There is a separate package [ altsrc] ( https://github.com/urfave/cli-altsrc ) that adds support for getting flag values
140140from other file input sources.
141141
142- Currently supported input source formats:
142+ Currently supported input source formats by that library are :
143143
144144- YAML
145145- JSON
146146- TOML
147147
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:
148+ A simple straight forward usage would be
150149
151150``` go
152- // --- >8 ---
153- altsrc.NewIntFlag (&cli.IntFlag {Name: " test" })
154- ```
151+ package main
155152
156- Initialization must also occur for these flags. Below is an example initializing
157- getting data from a yaml file below.
153+ import (
154+ " log"
155+ " os"
156+ " context"
158157
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- ```
158+ " github.com/urfave/cli/v3"
159+ " github.com/urfave/cli-altsrc/v3"
160+ )
165161
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.
162+ func main () {
163+ cmd := &cli.Command {
164+ Flags: []cli.Flag {
165+ &cli.StringFlag {
166+ Name: " password" ,
167+ Aliases: []string {" p" },
168+ Usage: " password for the mysql database" ,
169+ Sources: altsrc.YAML (" somekey" , altsrc.StringSourcer (" /path/to/filename" )),
170+ },
171+ },
172+ }
171173
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.
174+ if err := cmd.Run (context.Background (), os.Args ); err != nil {
175+ log.Fatal (err)
176+ }
177+ }
178+ ```
175179
176- Here is a more complete sample of a command using YAML support:
180+ Sometime the source name is itself provided by another CLI flag. To allow the library to "lazy-load"
181+ the file when needed we use the ` altsrc.NewStringPtrSourcer ` function to bind the value of the flag
182+ to a pointer that is set as a destination of another flag
177183
178- <!-- {
179- "args": ["--help"],
180- "output": "--test int.*default: 0"
181- } -->
182184``` go
183185package main
184186
185187import (
186- " context"
187- " fmt"
188+ " log"
188189 " os"
190+ " context"
189191
190- altsrc " github.com/urfave/cli-altsrc/v3"
191192 " github.com/urfave/cli/v3"
193+ " github.com/urfave/cli-altsrc/v3"
192194)
193195
194196func 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-
197+ var filename string
203198 cmd := &cli.Command {
204- Action: func (context.Context , *cli.Command ) error {
205- fmt.Println (" --test value.*default: 0" )
206- return nil
199+ Flags: []cli.Flag {
200+ &cli.StringFlag {
201+ Name: " file" ,
202+ Aliases: []string {" f" },
203+ Value: " /path/to/default" ,
204+ Usage: " filename for mysql database" ,
205+ Destination: &filename,
206+ },
207+ &cli.StringFlag {
208+ Name: " password" ,
209+ Aliases: []string {" p" },
210+ Usage: " password for the mysql database" ,
211+ Sources: altsrc.YAML (" somekey" , altsrc.NewStringPtrSourcer (&filename)),
212+ },
207213 },
208- Flags: flags,
209214 }
210215
211- cmd.Run (context.Background (), os.Args )
216+ if err := cmd.Run (context.Background (), os.Args ); err != nil {
217+ log.Fatal (err)
218+ }
212219}
213- ```
220+ ```
0 commit comments