8000 Merge pull request #1751 from carhartl/document-slice-flags-v2 · urfave/cli@25fcea0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 25fcea0

Browse files
authored
Merge pull request #1751 from carhartl/document-slice-flags-v2
2 parents a10e584 + 944c668 commit 25fcea0

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

docs/v2/examples/flags.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,48 @@ 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+
import (
248+
"fmt"
249+
"log"
250+
"os"
251+
"strings"
252+
"github.com/urfave/cli/v2"
253+
)
254+
func main() {
255+
app := &cli.App{
256+
Flags: []cli.Flag{
257+
&cli.StringSliceFlag{
258+
Name: "greeting",
259+
Usage: "Pass multiple greetings",
260+
},
261+
},
262+
Action: func(cCtx *cli.Context) error {
263+
fmt.Println(strings.Join(cCtx.StringSlice("greeting"), `, `))
264+
return nil
265+
},
266+
}
267+
if err := app.Run(os.Args); err != nil {
268+
log.Fatal(err)
269+
}
270+
}
271+
```
272+
273+
Multiple values need to be passed as separate, repeating flags, e.g. `--greeting Hello --greeting Hola`.
274+
233275
#### Grouping
234276

235277
You can associate a category for each flag to group them together in the help output, e.g:

0 commit comments

Comments
 (0)
0