A simple library for parsing command-line arguments in Go.
To install the library, run:
go get github.com/philipelima/go-args/pkg/argsYou can use go-args to convert command-line arguments into a map or directly into a struct.
package main
import (
"fmt""log""github.com/philipelima/go-args/pkg/args"
)
funcmain() {
// show CLI arguments like a mapargMap:=args.AsMap()
fmt.Println("\nArgs as a Map:", argMap)
// Define the structure to map the argumentsvaruserstruct {
Namestring`go_arg:"name"`Ageint`go_arg:"age"`
}
// Create a parser and parse the argumentsparser:=args.NewParser(&user)
iferr:=parser.Parse(); err!=nil {
log.Fatalf("Error parsing arguments: %v", err)
}
fmt.Println("\nArgs as a Struct:", user)
}To test, run the following command in the terminal:
go run example/main.go --name=Jhon --age=30Args as a Map: map[name:Jhon age:30]
Args as a Struct: {Jhon 30}