OptionSet -> OptSet
stop --long allowing -l, code tweaks
add go.mod, README.md and LICENSE
Command-line --option parsing for Go
package main
import (
"errors"
"fmt"
"git.sr.ht/~geb/opt"
"os"
)
type Butty struct {
Bread string
Bacon, Egg bool
}
func main() {
butty := Butty{"brown", true, false}
o := opt.NewOptSet()
o.BoolFunc("bacon", func(b bool) error {
butty.Bacon = b
return nil
})
o.Func("bread", func(s string) error {
if s != "brown" && s != "white" {
return errors.New("unknown bread type: " + s)
}
butty.Bread = s
return nil
})
o.BoolFunc("egg", func(b bool) error {
butty.Egg = b
return nil
})
o.Alias("egg", "e")
o.FlagFunc("help", func() error {
fmt.Println("Command to make butties.")
os.Exit(0)
panic("unreachable")
})
o.Alias("help", "h")
err := o.Parse(true, os.Args[1:])
fmt.Println(err)
fmt.Println(butty)
fmt.Println(o.Args())
fmt.Println(o.Leftovers())
}
You can ask a question or send a patch by composing an email to ~geb/public-inbox@lists.sr.ht.
GPLv3 only, see LICENSE.
Copyright (c) 2022 John Gebbie