Skip to content

Commit 3cd32d8

Browse files
committed
Document slice flags as part of examples
Addresses urfave#1005
1 parent 96570ef commit 3cd32d8

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

docs/v2/examples/flags.md

+46
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,52 @@ That flag can then be set with `--lang spanish` or `-l spanish`. Note that
230230
giving two different forms of the same flag in the same command invocation is an
231231
error.
232232

233+
#### Multiple Values per Single Flag
234+
235+
Using a slice flag allows you to pass multiple values for a single flag; the values will be provided as a slice:
236+
237+
- `Int64SliceFlag`
238+
- `IntSliceFlag`
239+
- `StringSliceFlag`
240+
241+
<!-- {
242+
"args": ["&#45;&#45;greeting Hello", "&#45;&#45;greeting Hola"],
243+
"output": "Hello, Hola"
244+
} -->
245+
```go
246+
package main
247+
248+
import (
249+
"fmt"
250+
"log"
251+
"os"
252+
"strings"
253+
254+
"github.com/urfave/cli/v3"
255+
)
256+
257+
func main() {
258+
app := &cli.App{
259+
Flags: []cli.Flag{
260+
&cli.StringSliceFlag{
261+
Name: "greeting",
262+
Usage: "Pass multiple greetings",
263+
},
264+
},
265+
Action: func(cCtx *cli.Context) error {
266+
fmt.Println(strings.Join(cCtx.StringSlice("greeting"), `, `))
267+
return nil
268+
},
269+
}
270+
271+
if err := app.Run(os.Args); err != nil {
272+
log.Fatal(err)
273+
}
274+
}
275+
```
276+
277+
Multiple values need to be passed as separate, repeating flags, e.g. `--greeting Hello --greeting Hola`.
278+
233279
#### Ordering
234280

235281
Flags for the application and commands are shown in the order they are defined.

0 commit comments

Comments
 (0)