https://github.com/dmgk/getopt
Minimal, POSIX compatible argument parsing in Go
https://github.com/dmgk/getopt
getopt golang
Last synced: 12 months ago
JSON representation
Minimal, POSIX compatible argument parsing in Go
- Host: GitHub
- URL: https://github.com/dmgk/getopt
- Owner: dmgk
- License: 0bsd
- Created: 2022-05-26T19:23:31.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2022-06-02T13:58:57.000Z (about 4 years ago)
- Last Synced: 2025-07-08T17:06:18.504Z (12 months ago)
- Topics: getopt, golang
- Language: Go
- Homepage:
- Size: 11.7 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## getopt
Package getopt provides a minimal, getopt(3)-like argument parsing implementation with POSIX compatible semantics.
[](https://pkg.go.dev/github.com/dmgk/getopt)

#### Example
```go
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]
```