https://github.com/gregoryv/cmdline
package cmdline provides a way to parse command line arguments
https://github.com/gregoryv/cmdline
Last synced: 11 months ago
JSON representation
package cmdline provides a way to parse command line arguments
- Host: GitHub
- URL: https://github.com/gregoryv/cmdline
- Owner: gregoryv
- License: mit
- Created: 2019-08-04T15:31:11.000Z (almost 7 years ago)
- Default Branch: main
- Last Pushed: 2024-12-21T06:02:28.000Z (over 1 year ago)
- Last Synced: 2025-06-14T06:38:24.066Z (about 1 year ago)
- Language: Go
- Size: 200 KB
- Stars: 1
- Watchers: 2
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: changelog.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://github.com/gregoryv/cmdline/actions/workflows/go.yml)
[](https://codecov.io/gh/gregoryv/cmdline)
[](https://codeclimate.com/github/gregoryv/cmdline/maintainability)
Package [cmdline](https://pkg.go.dev/pkg/github.com/gregoryv/cmdline) provides a parser for command line arguments.
This package is different from the builtin package flag.
- Options are used when parsing arguments
- Flag is a boolean option
- Self documenting options are preferred
- Multiname options, e.g. -n, --dry-run map to same option
- Easy way to default to environment variables
- There are no pointer variations
- Parsing non option arguments
- Usage output with optional examples and preface
## Example
func ExampleNewBasicParser() {
os.Setenv("VERBOSE", "yes")
var (
cli = NewBasicParser()
uid = cli.Option("--uid", "Generated if not given").Int(0)
password = cli.Option("-p, --password, $PASSWORD").String("")
verbose = cli.Flag("-V, --verbose, $VERBOSE")
role = cli.Option("-r, --role").Enum("guest",
"guest", "admin", "nobody",
)
url = cli.Option("--test-host").Url("tcp://example.com:123")
dur = cli.Option("--pause").Duration("200ms")
// parse and name non options
username = cli.NamedArg("USERNAME").String("")
note = cli.NamedArg("NOTE").String("")
)
cli.Parse()
if !verbose {
log.SetOutput(ioutil.Discard)
}
fmt.Fprintln(os.Stdout, uid, username, password, note, role, url, dur)
}