Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/profoak/flag2
A more traditional flag library for the go programming language
https://github.com/profoak/flag2
command-line flag golang like posix
Last synced: about 2 months ago
JSON representation
A more traditional flag library for the go programming language
- Host: GitHub
- URL: https://github.com/profoak/flag2
- Owner: ProfOak
- License: mit
- Created: 2016-06-19T23:38:31.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2021-05-30T01:35:59.000Z (over 3 years ago)
- Last Synced: 2024-06-19T00:34:33.821Z (6 months ago)
- Topics: command-line, flag, golang, like, posix
- Language: Go
- Size: 19.5 KB
- Stars: 38
- Watchers: 5
- Forks: 5
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Flag2
---
A more traditional flag library for the go programming languageWhat?
---A more traditional flag library for the Go programming language. I also have a long history with Python, so the implimentation code looks similar to Python's argparse class.
Why?
---I did not like how the flag library that comes with Go parses command line flags.
Differences
---* You can define full word flags with the `--` prefix. You can define single character flags with the `-` prefix.
* Example of a full word flag: `--help`
* Example of a single character flag: `-h`
* Single character strings can be grouped, but only for boolean types: `-abcd` is essentially `-a, -b, -c, -d`
* This only works for boolean type flags* `--` denotes the end of the command line flag options
* Everything to the right of `--` will not be counted as flagsGetting started
---To install: `go get github.com/ProfOak/flag2`
```
package mainimport (
"os"
"fmt"
"github.com/ProfOak/flag2"
)func main() {
f := flag2.NewFlag()// short flag, long flag, description, default argument
f.AddString("n", "name", "this flag wants a name as input", "billy")
f.AddBool("b", "bool", "this flag will store true", false)// a help flag is added during the parse step
options, args := f.Parse(os.Args)// A usage method is provided, with details about each flag
// unfortunate side effect of interfaces
if options["help"] == true {
f.Usage()
}fmt.Println()
if options["name"] != nil {
fmt.Println("The name is:", options["name"])
}fmt.Println()
fmt.Println("===== FINAL RESULTS =====")
fmt.Println("Options:", options)
fmt.Println("Args:", args)
}```
The result of running this program:
```
go run main.go -b -n ProfOak Extra args--- Bools ---
-b, --bool this flag will store true
-h, --help Display this message and exit--- Strings ---
-n, --name this flag wants a name as inputName is: ProfOak
===== FINAL RESULTS =====
Options: map[help:true bool:true name:ProfOak]
Args: [Extra args]```
Reference ./test/test.go for a more detailed example.