Skip to content
/ getopt Public

Minimal, POSIX compatible argument parsing in Go

License

Notifications You must be signed in to change notification settings

dmgk/getopt

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

getopt

Package getopt provides a minimal, getopt(3)-like argument parsing implementation with POSIX compatible semantics.

Go Reference Tests

Example

package main

import (
	"fmt"

	"github.com/dmgk/getopt"
)

// go run example.go -ba42 -v -z -- -w arg1 arg2
func main() {
	// -a requires an argument
	// -b and -v have no arguments
	// -z may have an optional argument
	opts, err := getopt.New("a:bz::v")
	if err != nil {
		fmt.Printf("error creating scanner: %s\n", err)
		return
	}

	for opts.Scan() {
		opt, err := opts.Option()
		if err != nil {
			fmt.Printf("%s: error parsing option: %s\n", opts.ProgramName(), err)
			continue
		}

		if opt.HasArg() {
			fmt.Printf("%s: got option %q with arg %q\n", opts.ProgramName(), opt.Opt, opt)
		} else {
			fmt.Printf("%s: got option %q\n", opts.ProgramName(), opt.Opt)
		}
	}

	fmt.Printf("%s: remaining arguments: %v\n", opts.ProgramName(), opts.Args())
}
$ go run example.go -ba42 -v -z -- -w arg1 arg2
example: got option 'b'
example: got option 'a' with arg "42"
example: got option 'v'
example: got option 'z'
example: remaining arguments: [-w arg1 arg2]

About

Minimal, POSIX compatible argument parsing in Go

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages