Skip to content

Commit

Permalink
Merge pull request #100 from Alec-Bakholdin/flag-counter-usage-fix
Browse files Browse the repository at this point in the history
Flag counter usage fix
  • Loading branch information
akamensky committed Jul 28, 2022
2 parents ca8d8e6 + af1d684 commit d561062
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
5 changes: 4 additions & 1 deletion argument.go
Expand Up @@ -435,7 +435,10 @@ func (o *arg) usage() string {
case *bool:
break
case *int:
result = result + " <integer>"
isFlagCounter := !o.unique && o.size == 1
if !isFlagCounter {
result = result + " <integer>"
}
case *float64:
result = result + " <float>"
case *string:
Expand Down
31 changes: 31 additions & 0 deletions examples/flag-counter/flag-counter.go
@@ -0,0 +1,31 @@
package main

import (
"fmt"
"github.com/akamensky/argparse"
"os"
)

func main() {
// initialize parser
parser := argparse.NewParser("FlagCounter", "Example of FlagCounter usage")

// create FlagCounter argument
opts := &argparse.Options{
Required: true,
Help: "Will print out how many instances of the flag are found. For example, both -nn and --number --number will be 2",
}
count := parser.FlagCounter("n", "number", opts)

// parse arguments
err := parser.Parse(os.Args)

// check for errors in parsing
if err != nil {
fmt.Printf("Error parsing: [%+v]\n", err)
return
}

// print out the number of occurrences of the flag
fmt.Printf("Number of flags detected: [%d]\n", *count)
}

0 comments on commit d561062

Please sign in to comment.