Skip to content

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

Notifications You must be signed in to change notification settings

Philipelima/go-args

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

18 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

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

func main() {
	// show CLI arguments like a map
	argMap := args.AsMap()
	fmt.Println("\nArgs as a Map:", argMap)

	// Define the structure to map the arguments
	var user struct {
		Name string `go_arg:"name"`
		Age  int    `go_arg:"age"`
	}

	// Create a parser and parse the arguments
	parser := args.NewParser(&user)
	if err := 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

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages