https://github.com/lesiw/flag
POSIX+GNU flag parser.
https://github.com/lesiw/flag
cli command-line commandline go golang golang-library optarg posix-compliant posix-compliant-flags
Last synced: 7 months ago
JSON representation
POSIX+GNU flag parser.
- Host: GitHub
- URL: https://github.com/lesiw/flag
- Owner: lesiw
- License: mit
- Created: 2024-04-09T01:31:35.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-07-04T19:54:39.000Z (over 1 year ago)
- Last Synced: 2024-08-14T09:56:08.081Z (about 1 year ago)
- Topics: cli, command-line, commandline, go, golang, golang-library, optarg, posix-compliant, posix-compliant-flags
- Language: Go
- Homepage: https://lesiw.io/flag
- Size: 26.4 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# lesiw.io/flag
[](https://pkg.go.dev/lesiw.io/flag)
A flag parser for the lazy. Follows the POSIX [utility conventions][utilconv]
plus GNU `--longopts`.So, for example, `-abcd` could be four boolean flags, or `-b` could be a string
flag that considers `cd` to be the argument passed to it.Pass in an `io.Writer` when creating a `flag.Set` and the `Parse` method will
write errors and usage help messages directly to that writer. If `Parse` returns
an error, it is recommended that the program author exit as soon as possible
without additional output.Aliases can be made at flag definition time by passing in comma-separated flag
names.## Example
``` go
package mainimport (
"fmt"
"os""lesiw.io/flag"
)var (
flags = flag.NewSet(os.Stderr, "sandbox [-w WORD] [-n NUM] [-b] ARGS...")
word = flags.String("w,word", "a string")
num = flags.Int("n,num", "an int")
bool = flags.Bool("b,bool", "a bool")
)func main() {
os.Exit(run())
}func run() int {
// Playground note: Replace os.Args with other strings to test.
if err := flags.Parse(os.Args[1:]...); err != nil {
return 1
}
if len(flags.Args) < 1 {
flags.PrintError("at least one arg is required")
return 1
}fmt.Println("word:", *word)
fmt.Println("num:", *num)
fmt.Println("bool:", *bool)
fmt.Println("args:", flags.Args)return 0
}
```[▶️ Run this example on the Go Playground](https://go.dev/play/p/YN0C5I0L-Et)
[utilconv]: https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html