A POSIX-compatible getopt implementation for Go
Print error when ExitOnError is set
Improve output of flagset.Usage()
Fix godoc for getopt.go

refs

master
browse log

clone

read-only
https://git.sr.ht/~sircmpwn/getopt
read/write
git@git.sr.ht:~sircmpwn/getopt

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

getopt godoc builds.sr.ht status

A POSIX-compatible getopt implementation for Go, because POSIX getopt is The Correct Way to interpret arguments to command line utilities.

Please send patches/bugs/feedback to ~sircmpwn/public-inbox@lists.sr.ht.

Example Usage

import (
    "os"
    "git.sr.ht/~sircmpwn/getopt"
)

func main() {
    opts, optind, err := getopt.Getopts(os.Args, "abc:d:")
    if err != nil {
        panic(err)
    }
    for _, opt := range opts {
        switch opt.Option {
        case 'a':
            println("Option -a specified")
        case 'b':
            println("Option -b specified")
        case 'c':
            println("Option -c specified: " + opt.Value)
        case 'd':
            println("Option -d specified: " + opt.Value)
        }
    }
    println("Remaining arguments:")
    for _, arg := range os.Args[optind:] {
        println(arg)
    }
}

A flag-like interface is also supported.

import (
    "git.sr.ht/~sircmpwn/getopt"
)

func main() {
    a := getopt.Bool("a", false, "turn on option a")
    b := getopt.Int("b", 1, "set b to a numerical value")
    var opt string
    getopt.StringVar(&opt, "c", "", "let c be specified string")

    err := getopt.Parse()
    if err != nil {
        panic(err)
    }

    print("Value of a: ")
    println(*a)
    print("Value of b: ")
    println(*b)
    println("Value of c: " + opt)

    println("Remaining arguments:")
    for _, arg := range getopt.Args() {
        println(arg)
    }
}