Merge pull request #1257 from dearchap/bool_count
Add count option for bool flags
This commit is contained in:
@@ -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": ["--foo", "--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.
|
||||
|
Reference in New Issue
Block a user