func main() {
// Create new parser object
parser := argparse.NewParser("print", "Prints provided string to stdout")
// Create string flag
s := parser.String("s", "string", &argparse.Options{Required: true, Help: "String to print"})
// Parse input
err := parser.Parse(os.Args)
if err != nil {
// In case of error print error and print usage
// This can also be done by passing -h or --help flags
fmt.Print(parser.Usage(err))
}
// Finally print the collected string
fmt.Println(*s)
}
above is the source
go build first_argparse.go
output:
imports github.com/akamensky/argparse
imports github.com/akamensky/argparse: import cycle not allowed
~/go/src/cmdline_argparse $ go version
go version go1.17.1 darwin/amd64
package main
import ( "fmt" "github.com/akamensky/argparse" "os" )
func main() { // Create new parser object parser := argparse.NewParser("print", "Prints provided string to stdout") // Create string flag s := parser.String("s", "string", &argparse.Options{Required: true, Help: "String to print"}) // Parse input err := parser.Parse(os.Args) if err != nil { // In case of error print error and print usage // This can also be done by passing -h or --help flags fmt.Print(parser.Usage(err)) } // Finally print the collected string fmt.Println(*s) }
above is the source
go build first_argparse.go output: imports github.com/akamensky/argparse imports github.com/akamensky/argparse: import cycle not allowed
~/go/src/cmdline_argparse $ go version go version go1.17.1 darwin/amd64