https://github.com/adamslevy/flagbind
Define rich flags with struct tags to make writing CLIs a breeze
https://github.com/adamslevy/flagbind
Last synced: about 1 month ago
JSON representation
Define rich flags with struct tags to make writing CLIs a breeze
- Host: GitHub
- URL: https://github.com/adamslevy/flagbind
- Owner: AdamSLevy
- License: mit
- Created: 2020-03-02T00:04:22.000Z (about 5 years ago)
- Default Branch: main
- Last Pushed: 2024-11-02T18:07:59.000Z (7 months ago)
- Last Synced: 2025-04-13T23:37:54.232Z (about 1 month ago)
- Language: Go
- Homepage:
- Size: 79.1 KB
- Stars: 4
- Watchers: 1
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# `package flagbind`
[](https://godoc.org/github.com/AdamSLevy/flagbind)
[](https://travis-ci.org/AdamSLevy/flagbind)
[](https://coveralls.io/github/AdamSLevy/flagbind?branch=master)Package `flagbind` parses the exported fields of a struct and binds them to
flags in a `flag.FlagSet` or `pflag.FlagSet`.`Bind` allows for creating flags declaratively right alongside the definition
of their containing struct. For example, the following stuct could be passed to
`Bind` to populate a `flag.FlagSet` or `pflag.FlagSet`.```go
flags := struct {
StringFlag string `flag:"flag-name;default value;Usage for string-flag"`
Int int `flag:"integer;5"`// Flag names default to `auto-kebab-case`
AutoKebabCase int// If pflag is used, -s is be used as the shorthand flag name,
// otherwise it is ignored for use with the standard flag package.
ShortName bool `flag:"short,s"`// Optionally extende the usage tag with subsequent `use` tags
// on _ fields.
URL string `flag:"url,u;http://www.example.com/;Start usage here"
_ struct{} `use:"continue longer usage string for --url below it",// Nested and Embedded structs can add a flag name prefix, or not.
Nested StructA
NestedFlat StructB `flag:";;;flatten"`
StructA // Flat by default
StructB `flag:"embedded"` // Add prefix to nested field flag names.// Ignored
ExplicitlyIgnored bool `flag:"-"`
unexported bool
}{
// Default values may also be set directly to override the tag.
StringFlag: "override default",
_URL: "Include a longer usage string for --url here",
}fs := pflag.NewFlagSet("", pflag.ContinueOnError)
flagbind.Bind(fs, &flags)
fs.Parse([]string{"--auto-kebab-case"})
````Bind` works seemlessly with both the standard library `flag` package and the
popular `[github.com/spf13/pflag](https://github.com/spf13/pflag)` package.If `pflag` is used, for types that implement `flag.Value` but not
`pflag.Value`, `Bind` wraps them in an adapter so that they can still be used
as a `pflag.Value`. The return value of the additional function `Type() string`
is the type name of the struct field.Additional options may be set for each flag. See `Bind` for the full
documentation details.