Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document slice flags as part of examples (v2) #1751

Merged
merged 1 commit into from Jun 12, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 42 additions & 0 deletions docs/v2/examples/flags.md
Expand Up @@ -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`

<!-- {
"args": ["&#45;&#45;greeting Hello", "&#45;&#45;greeting Hola"],
"output": "Hello, Hola"
} -->
```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:
Expand Down