~mendelmaleh/opt

Simple option parser
Add README.md
Catch case where optarg has no arg
Add comments, remove Opt struct type

refs

master
browse  log 

clone

read-only
https://git.sr.ht/~mendelmaleh/opt
read/write
git@git.sr.ht:~mendelmaleh/opt

You can also use your local clone with git send-email.

usage

package main

import (
	"fmt"
	"log"
	"os"

	"git.sr.ht/~mendelmaleh/opt"
)

func main() {
	opts := opt.New()
	args, err := opts.Parse(os.Args[1:], "ab:")
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(opts, args)
}

Calling this sample program:

# the opt map will only contain opts that are given
$ go run main.go
map[] []

# the opts are stored as runes
$ go run main.go -a
map[97:] []

# the optargs are stored as strings
# you can use opt arg, opt=arg, and optarg
$ go run main.go -ab hello
map[97: 98:hello] []
$ go run main.go -ab=hello
map[97: 98:=hello] []
$ go run main.go -abhello
map[97: 98:hello] []

# optargs with no arg available will be empty strings
$ go run main.go -ab
map[97: 98:] []