Documentation ¶
Overview ¶
Package act is a library for parsing command line flags, environment variables and default values defined by struct tag "def" respecting the order of precedence according to the 12-factor principles. It generates predefined flag names and environment variables names by the fields path of the supplied config, but allows this to be overridden by struct tags "flag" and "env".
Example (Advanced) ¶
package main import ( "flag" "log" "time" "go.ectobit.com/act" ) func main() { type config struct { Env string `help:"environment [development|production]" def:"development"` Port uint `def:"3000"` Mongo struct { Hosts act.StringSlice `def:"mongo"` ConnectionTimeout time.Duration `def:"10s"` ReplicaSet string MaxPoolSize uint64 `def:"100"` TLS bool Username string Password string Database string `def:"cool"` } JWT struct { Secret string TokenExpiration time.Duration `def:"24h"` RefreshTokenExpiration time.Duration `def:"168h"` } AWS struct { Region string `def:"eu-central-1"` } } cfg := &config{} //nolint:exhaustivestruct cmd := act.New("cool", act.WithErrorHandling(flag.ContinueOnError)) if err := cmd.Parse(cfg, []string{"-h"}); err != nil { log.Println(err) } }
Output:
Example (Basic) ¶
package main import ( "flag" "log" "go.ectobit.com/act" ) func main() { type config struct { Host string Port int DB struct { Kind string Postgres struct { Host string } Mongo struct { Host act.StringSlice } } } cfg := &config{} //nolint:exhaustivestruct cmd := act.New("mycmd", act.WithErrorHandling(flag.ContinueOnError)) if err := cmd.Parse(cfg, []string{"-h"}); err != nil { log.Println(err) } }
Output:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrInvalidConfigType = errors.New("invalid config type") ErrUnsupportedType = errors.New("type not supported") )
Errors.
Functions ¶
This section is empty.
Types ¶
type Act ¶
type Act struct {
// contains filtered or unexported fields
}
Act is an abstraction of a CLI command.
type IntSlice ¶
type IntSlice []int
IntSlice implements flag.Getter interface for []int type.
type Option ¶
type Option func(a *Act)
Option defines optional parameters to the constructor.
func WithErrorHandling ¶
func WithErrorHandling(errorHandling flag.ErrorHandling) Option
WithErrorHandling is an option to change error handling similar to flag package.
func WithLookupEnvFunc ¶
WithLookupEnvFunc may be used to override default os.LookupEnv function to read environment variables values.
func WithOutput ¶
WithOutput is an option to change the output writer similar as flag.SetOutput does.
type StringSlice ¶
type StringSlice []string
StringSlice implements flag.Getter interface for []string type.
func (*StringSlice) Set ¶
func (f *StringSlice) Set(s string) error
Set sets flag's value by splitting provided comma separated string.