From 944c6684fe14446e66be5b371d0cceb919b51bc1 Mon Sep 17 00:00:00 2001 From: Klaus Hartl Date: Mon, 12 Jun 2023 14:19:22 +0200 Subject: [PATCH] Document slice flags as part of examples Addresses #1005 --- docs/v2/examples/flags.md | 42 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/docs/v2/examples/flags.md b/docs/v2/examples/flags.md index f048def27c..3d732237ac 100644 --- a/docs/v2/examples/flags.md +++ b/docs/v2/examples/flags.md @@ -230,6 +230,48 @@ That flag can then be set with `--lang spanish` or `-l spanish`. Note that giving two different forms of the same flag in the same command invocation is an error. +#### Multiple Values per Single Flag + +Using a slice flag allows you to pass multiple values for a single flag; the values will be provided as a slice: + +- `Int64SliceFlag` +- `IntSliceFlag` +- `StringSliceFlag` + + +```go +package main +import ( + "fmt" + "log" + "os" + "strings" + "github.com/urfave/cli/v2" +) +func main() { + app := &cli.App{ + Flags: []cli.Flag{ + &cli.StringSliceFlag{ + Name: "greeting", + Usage: "Pass multiple greetings", + }, + }, + Action: func(cCtx *cli.Context) error { + fmt.Println(strings.Join(cCtx.StringSlice("greeting"), `, `)) + return nil + }, + } + if err := app.Run(os.Args); err != nil { + log.Fatal(err) + } +} +``` + +Multiple values need to be passed as separate, repeating flags, e.g. `--greeting Hello --greeting Hola`. + #### Grouping You can associate a category for each flag to group them together in the help output, e.g: