Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Flagarize ¶
func Flagarize(r KingpinRegistry, s interface{}, o ...OptFunc) error
Flagarize registers flags based on `flagarize:"..."` struct tags.
If field is a type that implemented Flagarizer or ValueFlagaizer interface, the custom Flagarizer will be used instead of default one. IMPORTANT: It is expected that struct fields are filled with values only after kingpin.Application.Parse is invoked for example:
type ComponentAOptions struct {
Field1 []string `flagarize:"name=a.flag1|help=..."`
}
func ExampleFlagarize() {
// Create new kingpin app as usual.
a := kingpin.New(filepath.Base(os.Args[0]), "<Your CLI description>")
// Define you own config.
type ConfigForCLI struct {
Field1 string `flagarize:"name=flag1|help=...|default=something"`
Field2 *url.URL `flagarize:"name=flag2|help=...|placeholder=<URL>"`
Field3 int `flagarize:"name=flag3|help=...|default=2144"`
Field4 flagarize.TimeOrDuration `flagarize:"name=flag4|help=...|default=1m|placeholder=<time or duration>"`
NotFromFlags int
ComponentA ComponentAOptions
}
// Create new config.
cfg := &ConfigForCLI{}
// Flagarize it! (Register flags from config).
if err := flagarize.Flagarize(a, &cfg); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(2)
}
// You can define some fields as usual as well.
var notInConfigField time.Duration
a.Flag("some-field10", "...").
DurationVar(¬InConfigField)
// Parse flags as usual.
if _, err := a.Parse(os.Args[1:]); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(2)
}
// Config is filled with flags from value!
_ = cfg.Field1
}
Example ¶
// Create new kingpin app as usual.
a := kingpin.New(filepath.Base(os.Args[0]), "<Your CLI description>")
// Define you own config.
type ConfigForCLI struct {
Field1 string `flagarize:"name=flag1|help=...|default=something"`
Field2 *url.URL `flagarize:"name=flag2|help=...|placeholder=<URL>"`
Field3 int `flagarize:"name=flag3|help=...|default=2144"`
Field4 flagarize.TimeOrDuration `flagarize:"name=flag4|help=...|default=1m|placeholder=<time or duration>"`
NotFromFlags int
ComponentA ComponentAOptions
}
// Create new config.
cfg := &ConfigForCLI{}
// Flagarize it! (Register flags from config).
if err := flagarize.Flagarize(a, &cfg); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(2)
}
// You can define some fields as usual as well.
var notInConfigField time.Duration
a.Flag("some-field10", "...").
DurationVar(¬InConfigField)
// Parse flags as usual.
if _, err := a.Parse(os.Args[1:]); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(2)
}
// Config is filled with flags from value!
_ = cfg.Field1
Types ¶
type AnchoredRegexp ¶
func (*AnchoredRegexp) Set ¶
func (r *AnchoredRegexp) Set(v string) (err error)
Set registers anchored Regexp flag.
type FlagRegisterer ¶
type FlagRegisterer interface {
Flag(name, help string) *kingpin.FlagClause
}
FlagRegisterer allows registering a flag.
type Flagarizer ¶
type Flagarizer interface {
// Flagarize is invoked on Flagarize. If field type does not implement custom Flagarizer
// default one will be used.
// Tag argument is nil if no `flagarize` struct tag was specified. Otherwise it has parsed
//`flagarize` struct tag.
// The ptr argument is an address of the already allocated type, that can be used
// by FlagRegisterer kingping *Var methods.
Flagarize(r FlagRegisterer, tag *Tag, ptr unsafe.Pointer) error
}
Flagarizer is more advanced way to extend flagarize to parse a type. It allows to register more than one flag or register them in a custom way. It's ok for a method to register nothing. If any field implements `Flagarizer` this method will be invoked even if field does not have `flagarize:` struct tag.
If the field implements both ValueFlagarizer and Flagarizer, only Flagarizer will be used.
For an example usagesee: `./pathorcontent.go`
type KingpinRegistry ¶
type KingpinRegistry interface {
FlagRegisterer
Command(name, help string) *kingpin.CmdClause
GetFlag(name string) *kingpin.FlagClause
}
KingpinRegistry allows registering a flag, getting a flag and registering a command. Example implementation is *kingpin.App.
type OptFunc ¶
type OptFunc func(opt *opts)
OptFunc sets values in opts structure.
func WithElemSep ¶
WithElemSep sets custom divider for elements in flagarize struct tag. It is "|" by default.
type PathOrContent ¶
type PathOrContent struct {
// contains filtered or unexported fields
}
PathOrContent is a flag type that defines two flags to fetch bytes. Either from file (*-file flag) or content (* flag).
func NewPathOrContent ¶
func NewPathOrContent(flagName string, required bool, path, content *string) *PathOrContent
func (*PathOrContent) Content ¶
func (p *PathOrContent) Content() ([]byte, error)
Content returns content of the file. Flag that specifies path has priority. It returns error if the content is empty and required flag is set to true.
func (*PathOrContent) Flagarize ¶
func (p *PathOrContent) Flagarize(r FlagRegisterer, tag *Tag, _ unsafe.Pointer) error
Flagarize registers PathOrContent flag.
func (*PathOrContent) String ¶
func (p *PathOrContent) String() string
type Tag ¶
type Tag struct {
Name string
Short rune
EnvName string
Help string
DefaultValue string
PlaceHolder string
Hidden bool
Required bool
}
func (*Tag) Flag ¶
func (t *Tag) Flag(r FlagRegisterer) *kingpin.FlagClause
type TimeOrDuration ¶
TimeOrDuration is a custom kingping parser for time in RFC3339 or duration in Go's duration format, such as "300ms", "-1.5h" or "2h45m". Only one will be set.
func (*TimeOrDuration) PrometheusTimestamp ¶
func (tdv *TimeOrDuration) PrometheusTimestamp() int64
PrometheusTimestamp returns TimeOrDuration converted to PrometheusTimestamp if duration is set now+duration is converted to Timestamp.
func (*TimeOrDuration) Set ¶
func (tdv *TimeOrDuration) Set(s string) error
Set converts string to TimeOrDuration.
func (*TimeOrDuration) String ¶
func (tdv *TimeOrDuration) String() string
String returns either time or duration.
type ValueFlagarizer ¶
type ValueFlagarizer interface {
// FlagarizeSetValue is invoked on kinpgin.Parse with the flag value passed as string.
// It is expected from this method to parse the string to the underlying type.
// This method has to be a pointer receiver for the method to take effect.
// Flagarize will return error otherwise.
Set(s string) error
}
ValueFlagarizer is the simplest way to extend flagarize to parse your custom type. If any field has `flagarize:` struct tag and it implements the ValueFlagarizer, this will be used by kingping to parse the flag value.
For an example see: `./timeduration.go` or `./regexp.go`