Skip to content

Commit

Permalink
Document slice flags as part of examples
Browse files Browse the repository at this point in the history
Addresses #1005
  • Loading branch information
carhartl committed Jun 12, 2023
1 parent 96570ef commit bc83065
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions docs/v2/examples/flags.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,53 @@ 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`

<!-- {
"args": ["&#45;&#45;greeting Hello", "&#45;&#45;greeting Hola"],
"output": "Hello, Hola"
} -->

```go
package main

import (
"fmt"
"log"
"os"
"strings"

"github.com/urfave/cli/v3"
)

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`.

#### Ordering

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

0 comments on commit bc83065

Please sign in to comment.