Skip to content

Commit

Permalink
added example for FlagCounter
Browse files Browse the repository at this point in the history
  • Loading branch information
alecbakholdin authored and akamensky committed Jul 28, 2022
1 parent febcacb commit 3497177
Showing 1 changed file with 31 additions and 0 deletions.
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 3497177

Please sign in to comment.