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

Flag counter usage fix #100

Merged
merged 3 commits into from Jul 28, 2022
Merged
Show file tree
Hide file tree
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
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)
}