Skip to content

Commit 48761dc

Browse files
authored
Merge pull request urfave#2119 from dearchap/cleanup_docs_04272025_2
Add docs for advanced value source
2 parents d855698 + 32315ca commit 48761dc

File tree

3 files changed

+66
-57
lines changed

3 files changed

+66
-57
lines changed

docs/v3/examples/arguments/advanced.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ search:
77

88
The [Basics] showed how to access arguments for a command. They are all retrieved as strings which is fine
99
but it we need to say get integers or timestamps the user would have to convert from string to desired type.
10-
To ease the burden on users the `cli` library offers predefined <Type>Arg and <Type>Args structure to faciliate this
11-
The value of the argument can be retrieved using the command.<Type>Arg() function. For e.g
10+
To ease the burden on users the `cli` library offers predefined `{Type}Arg` and `{Type}Args` structure to faciliate this
11+
The value of the argument can be retrieved using the `command.{Type}Arg()` function. For e.g
1212

1313
<!-- {
1414
"args" : ["10"],
@@ -52,7 +52,7 @@ $ greet 10
5252
We got 10
5353
```
5454

55-
Instead of using the cmd.XXXArg() function to retrieve the argument value a destination for the argument can be set
55+
Instead of using the `cmd.{Type}Arg()` function to retrieve the argument value a destination for the argument can be set
5656
for e.g
5757

5858
<!-- {
@@ -152,9 +152,9 @@ func main() {
152152

153153
Some things to note about multi value arguments
154154

155-
1. They have XXXArgs type instead of XXXArg to differentiate them from single value arguments
156-
2. The Max field needs to be defined to a non zero value without which it cannot be parsed
157-
3. Max field value needs to be greater then the Min field value
155+
1. They are of `{Type}Args` type rather than `{Type}Arg` to differentiate them from single value arguments
156+
2. The `Max` field needs to be defined to a non zero value without which it cannot be parsed
157+
3. `Max` field value needs to be greater than the `Min` field value
158158

159159
As with single value args the destination field can be set
160160

@@ -214,11 +214,11 @@ Following multi value arguments are supported
214214
- `TimestampArgs`
215215

216216
It goes without saying that the chain of arguments set in the Arguments slice need to be consistent. Generally a glob
217-
argument(max=-1) should be set for the argument at the end of the slice. To glob args we arent interested in we coud add
217+
argument(`max=-1`) should be set for the argument at the end of the slice. To glob args we arent interested in we coud add
218218
the following to the end of the Arguments slice and retrieve them as a slice
219219

220220
```
221-
&StringArgs{
222-
Max: -1,
223-
},
221+
&StringArgs{
222+
Max: -1,
223+
},
224224
```

docs/v3/examples/flags/basics.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ The following basic flags are supported
153153
- `StringFlag`
154154
- `TimestampFlag`
155155

156-
For full list of flags see https://pkg.go.dev/github.com/urfave/cli/v3
156+
For full list of flags see [`https://pkg.go.dev/github.com/urfave/cli/v3`](https://pkg.go.dev/github.com/urfave/cli/v3)
157157

158158
### Timestamp Flag ###
159159

docs/v3/examples/flags/value-sources.md

Lines changed: 55 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,19 @@ search:
77

88
Flags can have their default values set from different sources. The following sources are
99
provided by default with `urfave/cli`
10+
1011
- Environment
1112
- Text Files
1213

1314
The library also provides a framework for users to plugin their own implementation of value sources
1415
to be fetched via other mechanisms(http and so on).
1516

1617
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+
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
140142
from 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": ["&#45;&#45;help"],
180-
"output": "&#45&#45;test int.*default: 0"
181-
} -->
182186
```go
183187
package main
184188

185189
import (
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

194198
func 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

Comments
 (0)