Documentation
¶
Index ¶
- func GetConfig[T any](cmd *Command) (*T, bool)
- func MustConfig[T any](cmd *Command) *T
- type Command
- func (c *Command) AddSubcommands(commands ...*Command)
- func (c *Command) Args() []string
- func (c *Command) BindConfig(cfg any) error
- func (c *Command) Config() any
- func (c *Command) Exec() error
- func (c *Command) Flags() *FlagSet
- func (c *Command) IsSubcommand() bool
- func (c *Command) TraverseToRoot() *Command
- type ConfigValidator
- type Error
- type FlagSet
- func (f *FlagSet) BindConfig(cfg any) error
- func (f *FlagSet) BoolVarE(p *bool, flagName, envName string, value bool, usage string)
- func (f *FlagSet) Config() any
- func (f *FlagSet) DurationVarE(p *time.Duration, flagName, envName string, value time.Duration, usage string)
- func (f *FlagSet) Float64VarE(p *float64, flagName, envName string, value float64, usage string)
- func (f *FlagSet) Int64VarE(p *int64, flagName, envName string, value int64, usage string)
- func (f *FlagSet) IntVarE(p *int, flagName, envName string, value int, usage string)
- func (f *FlagSet) StringVarE(p *string, flagName, envName, value, usage string)
- func (f *FlagSet) Uint64VarE(p *uint64, flagName, envName string, value uint64, usage string)
- func (f *FlagSet) UintVarE(p *uint, flagName, envName string, value uint, usage string)
- type RequiredFieldError
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetConfig ¶ added in v0.3.0
GetConfig returns the bound config with type assertion. Returns nil and false if config is not bound or wrong type.
func MustConfig ¶ added in v0.3.0
MustConfig returns the bound config with type assertion. Panics if config is not bound or wrong type.
Types ¶
type Command ¶
type Command struct {
// Name represents command name and argument by which command will be called.
Name string
// Short represents short description of the command.
Short string
// Long represents short description of the command.
Long string
// SetFlags represents function which can be used to set flags.
SetFlags func(flags *FlagSet)
// SetPersistentFlags represents function which can be used to set
// persistent flags. Persistent flags are inherited by all subcommands.
SetPersistentFlags func(flags *FlagSet)
// Run represents a function which wraps and executes the logic of the command.
Run func(cmd *Command, args []string) error
// contains filtered or unexported fields
}
Command represents a program command.
func (*Command) AddSubcommands ¶
AddSubcommands takes variadic slice of commands and add them as subcommands.
func (*Command) Args ¶
Args returns the non flag positional arguments which are passed to the command.
func (*Command) BindConfig ¶ added in v0.3.0
BindConfig binds a config struct to the command's flagset. The cfg argument must be a pointer to a struct with appropriate tags. Tags supported: flag, env, default, usage, required. Call this before Exec() to set up the binding.
func (*Command) Config ¶ added in v0.3.0
Config returns the bound config. Returns nil if no config was bound. Use type assertion or the generic helpers MustConfig/GetConfig for typed access.
func (*Command) IsSubcommand ¶
IsSubcommand return whether the command is subcommand for another command.
func (*Command) TraverseToRoot ¶
TraverseToRoot traverse all command chain until it reaches root.
type ConfigValidator ¶ added in v0.3.0
type ConfigValidator interface {
// Validate validates the config parameters.
Validate() error
}
ConfigValidator holds logic of validation the config parameters.
type Error ¶ added in v0.3.0
type Error string
Error is a custom error type for scotty errors.
const ErrRequiredField Error = "required field not set"
ErrRequiredField is returned when a required field is not set.
type FlagSet ¶ added in v0.2.0
FlagSet wraps flag.FlagSet and adds a few methods like StringVarE, BoolVarE and similar methods for other types.
func (*FlagSet) BindConfig ¶ added in v0.3.0
BindConfig binds a config struct to the flagset. The cfg argument must be a pointer to a struct with appropriate tags. Tags supported: flag, env, default, usage, required.
func (*FlagSet) BoolVarE ¶ added in v0.2.0
BoolVarE defines a bool flag and environment variable with specified name, default value, and usage string. The argument p points to a bool variable in which to store the value of the flag or environment variable. Flag has priority over environment variable. If flag not set the environment variable value will be used. If the value of environment variable can't be parsed to destination type the default value will be used.
func (*FlagSet) Config ¶ added in v0.3.0
Config returns the bound config. Returns nil if no config was bound. Use type assertion for typed access.
func (*FlagSet) DurationVarE ¶ added in v0.2.0
func (f *FlagSet) DurationVarE(p *time.Duration, flagName, envName string, value time.Duration, usage string)
DurationVarE defines a time.Duration flag and environment variable with specified name, default value, and usage string. The argument p points to a time.Duration variable in which to store the value of the flag or environment variable. Flag has priority over environment variable. If flag not set the environment variable value will be used. If the value of environment variable can't be parsed to destination type the default value will be used.
func (*FlagSet) Float64VarE ¶ added in v0.2.0
Float64VarE defines a float64 flag and environment variable with specified name, default value, and usage string. The argument p points to a float64 variable in which to store the value of the flag or environment variable. Flag has priority over environment variable. If flag not set the environment variable value will be used. If the value of environment variable can't be parsed to destination type the default value will be used.
func (*FlagSet) Int64VarE ¶ added in v0.2.0
Int64VarE defines an int64 flag and environment variable with specified name, default value, and usage string. The argument p points to an int64 variable in which to store the value of the flag or environment variable. Flag has priority over environment variable. If flag not set the environment variable value will be used. If the value of environment variable can't be parsed to destination type the default value will be used.
func (*FlagSet) IntVarE ¶ added in v0.2.0
IntVarE defines an int flag and environment variable with specified name, default value, and usage string. The argument p points to an int variable in which to store the value of the flag or environment variable. Flag has priority over environment variable. If flag not set the environment variable value will be used. If the value of environment variable can't be parsed to destination type the default value will be used.
func (*FlagSet) StringVarE ¶ added in v0.2.0
StringVarE defines a string flag and environment variable with specified name, default value, and usage string. The argument p points to a string variable in which to store the value of the flag or environment variable. Flag has priority over environment variable. If flag not set the environment variable value will be used. If the value of environment variable can't be parsed to destination type the default value will be used.
func (*FlagSet) Uint64VarE ¶ added in v0.2.0
Uint64VarE defines an uint64 flag and environment variable with specified name, default value, and usage string. The argument p points to an uint64 variable in which to store the value of the flag or environment variable. Flag has priority over environment variable. If flag not set the environment variable value will be used. If the value of environment variable can't be parsed to destination type the default value will be used.
func (*FlagSet) UintVarE ¶ added in v0.2.0
UintVarE defines an uint flag and environment variable with specified name, default value, and usage string. The argument p points to an uint variable in which to store the value of the flag or environment variable. Flag has priority over environment variable. If flag not set the environment variable value will be used. If the value of environment variable can't be parsed to destination type the default value will be used.
type RequiredFieldError ¶ added in v0.3.0
RequiredFieldError provides details about which required field was not set.
func (*RequiredFieldError) Error ¶ added in v0.3.0
func (e *RequiredFieldError) Error() string
func (*RequiredFieldError) Unwrap ¶ added in v0.3.0
func (*RequiredFieldError) Unwrap() error