~mendelmaleh/opt

b5502e354231f2c3482d140719d07771453c27ca — Mendel E 9 months ago 1ea2994 master
Add README.md
1 files changed, 49 insertions(+), 0 deletions(-)

A README.md
A README.md => README.md +49 -0
@@ 0,0 1,49 @@
## usage

```go
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:

```sh
# 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:] []
```