1
0
Fork
You've already forked opts
0
  • Go 100%
Find a file
2023年12月05日 01:32:52 +01:00
v2 Replace ‘optind’ with ‘rest’ in Get() and GetLong() returns 2023年12月05日 01:32:52 +01:00
err.go Add long-option support 2023年11月30日 00:59:30 +01:00
go.mod Genesis commit 2023年11月29日 20:30:50 +01:00
LICENSE Genesis commit 2023年11月29日 20:30:50 +01:00
opts.go More typo fixes... 2023年11月30日 01:53:10 +01:00
opts_test.go Add long-option support 2023年11月30日 00:59:30 +01:00
README.md Replace ‘optind’ with ‘rest’ in Get() and GetLong() returns 2023年12月05日 01:32:52 +01:00

Update 6th December, 2023

The v2 of this library has been released, and you should probably use that instead:

$ go get -u git.sr.ht/~mango/opts/v2

Opts

Opts is a simple Go library implementing unicode-aware getopt(3)- and getopt_long(3) flag parsing in Go. For details, check out the godoc documentation.

Note that unlike the getopt() C function, the ‘:’ and ‘?’ flags are not returned on errors — the errors are instead returned via the err return value of opts.Get() and opts.GetLong(). Additionally, a leading ‘:’ in the opt-string provided to opts.Get() is not supported; you are responsible for your own I/O.

Example Usage

The following demonstrates an example usage of the opts.Get() function...

packagemainimport("fmt""os""git.sr.ht/~mango/opts/v2")funcusage(){fmt.Fprintf(os.Stderr,"Usage: %s [-ßλ] [-a arg]\n",os.Args[0])os.Exit(1)}funcmain(){flags,rest,err:=opts.Get(os.Args,"a:ßλ")iferr!=nil{fmt.Fprintf(os.Stderr,"%s: %s\n",os.Args[0],err)usage()}for_,f:=rangeflags{switchf.Key{case'a':fmt.Println("-a given with argument",f.Value)case'ß':fmt.Println("-ß given")case'λ':fmt.Println("-λ given")}}fmt.Println("The remaining arguments are:",rest)}

...and the following demonstrates an example usage of the opts.GetLong() function:

packagemainimport("fmt""os""git.sr.ht/~mango/opts")funcusage(){fmt.Fprintf(os.Stderr,"Usage: %s [-ßλ] [-a arg] [--no-short]\n",os.Args[0])os.Exit(1)}funcmain(){// The fourth long-option has no short-option equivalentflags,rest,err:=opts.GetLong(os.Args,[]opts.LongOpt{{Short:'a',Long:"add",Arg:opts.Required},{Short:'ß',Long:"sheiße",Arg:opts.None},{Short:'λ',Long:"λεωνίδας",Arg:opts.None},{Short:-1,Long:"no-short",Arg:opts.None},})iferr!=nil{fmt.Fprintf(os.Stderr,"%s: %s\n",os.Args[0],err)usage()}for_,f:=rangeflags{switchf.Key{case'a':fmt.Println("-a or --add given with argument",f.Value)case'ß':fmt.Println("-ß or --sheiße given")case'λ':fmt.Println("-λ or --λεωνίδας given")case-1:fmt.Println("--no-short given")}}// The remaining argumentsfmt.Println("The remaining arguments are:",rest)}