Skip to content

Latest commit

History

18 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

go-args

A simple library for parsing command-line arguments in Go.

Installation

To install the library, run:

go get github.com/philipelima/go-args/pkg/args

Usage

You can use go-args to convert command-line arguments into a map or directly into a struct.

Example

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)
}

Running the code

To test, run the following command in the terminal:

go run example/main.go --name=Jhon --age=30

Expected output:

Args as a Map: map[name:Jhon age:30]
Args as a Struct: {Jhon 30}

About

A lightweight and user-friendly Go library for parsing command-line arguments. πŸ‘¨β€πŸ’»

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages