Merge pull request #1257 from dearchap/bool_count

Add count option for bool flags
This commit is contained in:
dearchap
2022-09-06 09:26:21 -04:00
committed by GitHub
10 changed files with 197 additions and 13 deletions

View File

@@ -101,6 +101,46 @@ func main() {
See full list of flags at https://pkg.go.dev/github.com/urfave/cli/v2
For bool flags you can specify the flag multiple times to get a count(e.g -v -v -v or -vvv)
<!-- {
"args": ["&#45;&#45;foo", "&#45;&#45;foo"],
"output": "count 2"
} -->
```go
package main
import (
"fmt"
"log"
"os"
"github.com/urfave/cli/v2"
)
func main() {
var count int
app := &cli.App{
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "foo",
Usage: "foo greeting",
Count: &count,
},
},
Action: func(cCtx *cli.Context) error {
fmt.Println("count", count)
return nil
},
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}
```
#### Placeholder Values
Sometimes it's useful to specify a flag's value within the usage string itself.