Documentation ¶
Overview ¶
Console Line Input Framework for rapid development of small or large scale CLI applications.
See the Github page for a full documentation with examples and patterns (https://github.com/ukautz/clif).
clif's design was influenced by Symfony Console (http://symfony.com/doc/current/components/console/introduction.html).
Index ¶
- Variables
- func InputAny(s string) error
- func InputEmptyOk(s string) error
- func IsFloat(name, value string) (string, error)
- func IsInt(name, value string) (string, error)
- type Argument
- func (this *Argument) Assign(val string) error
- func (this *Argument) Bool() bool
- func (this *Argument) Bools() []bool
- func (this *Argument) Count() int
- func (this *Argument) Float() float64
- func (this *Argument) Floats() []float64
- func (this *Argument) Int() int
- func (this *Argument) Ints() []int
- func (this *Argument) Json() (map[string]interface{}, error)
- func (this *Argument) Jsons() ([]map[string]interface{}, error)
- func (this *Argument) Provided() bool
- func (this *Argument) SetDefault(v string) *Argument
- func (this *Argument) SetDescription(v string) *Argument
- func (this *Argument) SetEnv(v string) *Argument
- func (this *Argument) SetParse(v ParseMethod) *Argument
- func (this *Argument) SetRegex(r *regexp.Regexp) *Argument
- func (this *Argument) SetUsage(v string) *Argument
- func (this *Argument) String() string
- func (this *Argument) Strings() []string
- func (this *Argument) Time(format ...string) (*time.Time, error)
- func (this *Argument) Times(format ...string) ([]time.Time, error)
- type CallMethod
- type Cli
- func (this *Cli) Add(cmd ...*Command) *Cli
- func (this *Cli) AddDefaultOptions(opts ...*Option) *Cli
- func (this *Cli) Call(c *Command) ([]reflect.Value, error)
- func (this *Cli) Herald(cmd ...HeraldCallback) *Cli
- func (this *Cli) New(name, usage string, call CallMethod) *Cli
- func (this *Cli) NewDefaultOption(name, alias, usage, _default string, required, multiple bool) *Cli
- func (this *Cli) Output() Output
- func (this *Cli) Register(v interface{}) *Cli
- func (this *Cli) RegisterAs(n string, v interface{}) *Cli
- func (this *Cli) RegisterNamed(n string, v interface{}) *Cli
- func (this *Cli) Run()
- func (this *Cli) RunWith(args []string)
- func (this *Cli) SetDefaultCommand(v string) *Cli
- func (this *Cli) SetDescription(v string) *Cli
- func (this *Cli) SetInput(in Input) *Cli
- func (this *Cli) SetOnInterrupt(cb func() error) *Cli
- func (this *Cli) SetOutput(out Output) *Cli
- type Command
- func (this *Command) AddArgument(v *Argument) *Command
- func (this *Command) AddOption(v *Option) *Command
- func (this *Command) Argument(name string) *Argument
- func (this *Command) Input() map[string][]string
- func (this *Command) NewArgument(name, usage, _default string, required, multiple bool) *Command
- func (this *Command) NewOption(name, alias, usage, _default string, required, multiple bool) *Command
- func (this *Command) Option(name string) *Option
- func (this *Command) Parse(args []string) error
- func (this *Command) SetCli(c *Cli) *Command
- func (this *Command) SetDescription(desc string) *Command
- type CommandsSort
- type DefaultFormatter
- type DefaultInput
- type DefaultOutput
- type Formatter
- type HeraldCallback
- type Input
- type NamedParameters
- type Option
- func (this *Option) Assign(val string) error
- func (this *Option) Bool() bool
- func (this *Option) Bools() []bool
- func (this *Option) Count() int
- func (this *Option) Float() float64
- func (this *Option) Floats() []float64
- func (this *Option) Int() int
- func (this *Option) Ints() []int
- func (this *Option) IsFlag() *Option
- func (this *Option) Json() (map[string]interface{}, error)
- func (this *Option) Jsons() ([]map[string]interface{}, error)
- func (this *Option) Provided() bool
- func (this *Option) SetDefault(v string) *Option
- func (this *Option) SetDescription(v string) *Option
- func (this *Option) SetEnv(v string) *Option
- func (this *Option) SetParse(v ParseMethod) *Option
- func (this *Option) SetRegex(r *regexp.Regexp) *Option
- func (this *Option) SetUsage(v string) *Option
- func (this *Option) String() string
- func (this *Option) Strings() []string
- func (this *Option) Time(format ...string) (*time.Time, error)
- func (this *Option) Times(format ...string) ([]time.Time, error)
- type Output
- type ParseMethod
- type ReduceMethod
- type Registry
- func (this *Registry) Alias(alias string, v interface{})
- func (this *Registry) Get(s string) reflect.Value
- func (this *Registry) Has(s string) bool
- func (this *Registry) Names() []string
- func (this *Registry) Reduce(cb ReduceMethod) []interface{}
- func (this *Registry) ReduceAsync(cb ReduceMethod) chan interface{}
- func (this *Registry) Register(v interface{})
Constants ¶
This section is empty.
Variables ¶
var ConfirmNoRegex = regexp.MustCompile(`^(?i)no?$`)
ConfirmNoRegex is the regular expression used to check if the user replied negative
var ConfirmRejection = "<warn>Please respond with \"yes\" or \"no\"<reset>\n\n"
ConfirmRejection is the message replied to the user if she does not answer with "yes", "y", "no" or "n" (case insensitive)
var ConfirmYesRegex = regexp.MustCompile(`^(?i)y(es)?$`)
ConfirmYesRegex is the regular expression used to check if the user replied positive
var DefaultFormatterEscape = func(msg string) string { return DefaultFormatterTokenRegex.ReplaceAllStringFunc(msg, func(token string) string { return `\` + token }) }
DefaultFormatterEscape is a callback to replace all tokens with escaped versions
var DefaultFormatterPost = func(msg string) string { return strings.Replace(msg, "~~~~#~~~~", "<", -1) }
DefaultFormatterPost is a post-callback, after DefaultFormatterTokenRegex is used
var DefaultFormatterPre = func(msg string) string { return strings.Replace(msg, `\<`, "~~~~#~~~~", -1) }
DefaultFormatterPre is a pre-callback, before DefaultFormatterTokenRegex is used
var DefaultFormatterTokenRegex = regexp.MustCompile(`(<[^>]+>)`)
DefaultFormatterTokenRegex is a regex to find all tokens, used by the DefaultFormatter
var DefaultHelpOption = &Option{ parameter: parameter{ Name: "help", Usage: "Display this help message", Description: "Display this help message", }, Alias: "h", Flag: true, }
DefaultHelpOption is the "--help" option, which is (per default) added to any command.
var DefaultOptions = []*Option{DefaultHelpOption}
DefaultOptions are prepended to any newly created command. Will be added immediately in the `NewCommand` call. See also `cli.NewDefaultOption()` and `cli.AddDefaultOptions()`.
var DefaultStyles = map[string]string{
"error": "\033[31;1m",
"warn": "\033[33m",
"info": "\033[34m",
"success": "\033[32m",
"debug": "\033[30;1m",
"headline": "\033[4;1m",
"subline": "\033[4m",
"important": "\033[47;30;1m",
"query": "\033[36m",
"reset": "\033[0m",
}
var DescribeCli = func(c *Cli) string { line := "<headline>" + c.Name + "<reset>" if c.Version != "" { line += " <debug>(" + c.Version + ")<reset>" } lines := []string{line} if c.Description != "" { lines = append(lines, "<info>"+c.Description+"<reset>\n") } prog := filepath.Base(os.Args[0]) lines = append(lines, fmt.Sprintf("<subline>Usage:<reset>\n %s command [arg ..] [--opt val ..]\n", prog)) lines = append(lines, "<subline>Available commands:<reset>") max := 0 ordered := make(map[string][]*Command) prefices := make([]string, 0) for _, cmd := range c.Commands { if l := len(cmd.Name); l > max { max = l } prefix := "" if i := strings.Index(cmd.Name, ":"); i > -1 { prefix = cmd.Name[0:i] } if ordered[prefix] == nil { prefices = append(prefices, prefix) ordered[prefix] = make([]*Command, 0) } ordered[prefix] = append(ordered[prefix], cmd) } sort.Strings(prefices) for _, prefix := range prefices { if prefix != "" { lines = append(lines, fmt.Sprintf(" <subline>%s<reset>", prefix)) } sort.Sort(CommandsSort(ordered[prefix])) for _, cmd := range ordered[prefix] { lines = append(lines, fmt.Sprintf(" <info>%-"+fmt.Sprintf("%d", max)+"s<reset> %s", cmd.Name, cmd.Usage)) } } return strings.Join(lines, "\n") + "\n" }
DescribeCli command implements the string rendering of a cli which help uses. Can be overwritten at users discretion.
var DescribeCommand = func(c *Command) string { lines := []string{"Command: <headline>" + c.Name + "<reset>"} if c.Description != "" { lines = append(lines, []string{"<info>" + c.Description + "<reset>", ""}...) } else if c.Usage != "" { lines = append(lines, []string{"<info>" + c.Usage + "<reset>", ""}...) } lines = append(lines, "<subline>Usage:<reset>") usage := []string{c.Name} args := make([][]string, 0) argMax := 0 opts := make([][]string, 0) optMax := 0 for _, p := range c.Arguments { var short string usg := p.Usage short = p.Name usgInfo := []string{} if p.Multiple { short = short + " ..." usgInfo = append(usgInfo, `<debug>mult<reset>`) } if p.Required { usgInfo = append(usgInfo, `<important>req<reset>`) } else { short = fmt.Sprintf("[%s]", short) } if p.Env != "" { usgInfo = append(usgInfo, fmt.Sprintf(`env: <debug>%s<reset>`, p.Env)) } if p.Default != "" { usgInfo = append(usgInfo, fmt.Sprintf(`default: <debug>"%s"<reset>`, p.Default)) } if l := len(p.Name); l > argMax { argMax = l } usage = append(usage, short) if len(usgInfo) > 0 { usg += " (" + strings.Join(usgInfo, ", ") + ")" } args = append(args, []string{p.Name, usg}) } for _, p := range c.Options { short := fmt.Sprintf("--%s", p.Name) if p.Alias != "" { short += "|-" + p.Alias } if !p.Flag { short += " val" } long := short usg := p.Usage usgInfo := []string{} if p.Multiple { short = short + " ..." usgInfo = append(usgInfo, `<debug>mult<reset>`) } if !p.Required { short = "[" + short + "]" } else { usgInfo = append(usgInfo, `<important>req<reset>`) } if p.Env != "" { usgInfo = append(usgInfo, fmt.Sprintf(`env: <debug>%s<reset>`, p.Env)) } if p.Default != "" { usgInfo = append(usgInfo, fmt.Sprintf(`default: <debug>"%s"<reset>`, p.Default)) } if l := len(long); l > optMax { optMax = l } usage = append(usage, short) if len(usgInfo) > 0 { usg += " (" + strings.Join(usgInfo, ", ") + ")" } opts = append(opts, []string{long, usg}) } lines = append(lines, " "+strings.Join(usage, " ")) lines = append(lines, "") if len(args) > 0 { lines = append(lines, "<subline>Arguments:<reset>") for _, l := range args { lines = append(lines, fmt.Sprintf(" <info>%-"+fmt.Sprintf("%d", argMax)+"s<reset> %s", l[0], l[1])) } lines = append(lines, "") } if len(opts) > 0 { lines = append(lines, "<subline>Options:<reset>") for _, l := range opts { lines = append(lines, fmt.Sprintf(" <info>%-"+fmt.Sprintf("%d", optMax)+"s<reset> %s", l[0], l[1])) } lines = append(lines, "") } return strings.Join(lines, "\n") + "\n" }
DescribeCommand implements the string rendering of a command which help uses. Can be overwritten at users discretion.
var Die = func(msg string, args ...interface{}) { NewColorOutput(os.Stderr).Printf("<error>"+msg+"<reset>\n", args...) Exit(1) }
Die is the default function executed on die. It can be used as a shorthand via `clif.Die("foo %s", "bar")` and can be overwritten to change the failure exit handling CLI-wide.
var Exit = func(s int) { os.Exit(s) }
Exit is wrapper for os.Exit, so it can be overwritten for tests or edge use cases
var RenderAskQuestion = func(question string) string { return "<query>" + strings.TrimRight(question, " ") + "<reset> " }
var RenderChooseOption = func(key, value string, size int) string { return fmt.Sprintf(" <query>%-"+fmt.Sprintf("%d", size+1)+"s<reset> %s\n", key+")", value) }
RenderChooseOption is the method used by default input `Choose()` method to to render a singular choice into a string. Can be overwritten at users discretion.
var RenderChooseQuery = func() string {
return "Choose: "
}
RenderChooseQuery is the method used by default input `Choose()` method to to render the query prompt choice (after the choices) into a string. Can be overwritten at users discretion.
var RenderChooseQuestion = func(question string) string {
return question + "\n"
}
RenderChooseQuestion is the method used by default input `Choose()` method to to render the question (displayed before listing the choices) into a string. Can be overwritten at users discretion.
var SunburnStyles = map[string]string{
"error": "\033[97;48;5;196;1m",
"warn": "\033[30;48;5;208;2m",
"info": "\033[38;5;142;2m",
"success": "\033[38;5;2;2m",
"debug": "\033[38;5;242;2m",
"headline": "\033[38;5;226;1m",
"subline": "\033[38;5;228;1m",
"important": "\033[38;5;15;2;4m",
"query": "\033[38;5;77m",
"reset": "\033[0m",
}
http://misc.flogisoft.com/bash/tip_colors_and_formatting#colors1
var WinterStyles = map[string]string{
"error": "\033[97;48;5;89;1m",
"warn": "\033[30;48;5;97;2m",
"info": "\033[38;5;69;2m",
"success": "\033[38;5;45;1m",
"debug": "\033[38;5;239;2m",
"headline": "\033[38;5;21;1m",
"subline": "\033[38;5;27;1m",
"important": "\033[38;5;15;2;4m",
"query": "\033[38;5;111m",
"reset": "\033[0m",
}
http://misc.flogisoft.com/bash/tip_colors_and_formatting#colors1
Functions ¶
func InputEmptyOk ¶
Types ¶
type Argument ¶
type Argument struct {
// contains filtered or unexported fields
}
Arguments must be provided immediately after the command in the order they were added. Non required arguments must be ordered after required arguments. Only one argument is allowed to contain multiple values and it needs to be the last one.
func NewArgument ¶
NewArgument constructs a new argument
func (*Argument) Assign ¶
Assign tries to add value to parameter and returns error if it fails due to invalid format or invalid amount (single vs multiple parameters)
func (*Argument) Bool ¶
func (this *Argument) Bool() bool
Bool representation of the value (will be false, if not given or not parsable)
func (*Argument) Bools ¶
func (this *Argument) Bools() []bool
Bools returns values as bool array (values will be false, if not parsable to float64)
func (*Argument) Count ¶
func (this *Argument) Count() int
Provided returns amount of values provided
func (*Argument) Float ¶
func (this *Argument) Float() float64
Float representation of the value (will be 0.0, if not given or not parsable)
func (*Argument) Floats ¶
func (this *Argument) Floats() []float64
Floats returns values as float64 array (values will be 0.0, if not parsable to float64)
func (*Argument) Int ¶
func (this *Argument) Int() int
Int representation of the value (will be 0, if not given or not parsable)
func (*Argument) Ints ¶
func (this *Argument) Ints() []int
Ints returns values as int array (values will be 0, if not parsable to int)
func (*Argument) Json ¶
Json assumes the input is a JSON string and parses into a standard map[string]interface{} Returns error, if not parsable (or eg array JSON).
Helpful to allow complex inputs: `my-app do --foo '{"bar": "baz"}'`
func (*Argument) Provided ¶
func (this *Argument) Provided() bool
Provided returns bool whether argument was provided
func (*Argument) SetDefault ¶
SetDefault is a builder method to set default value. Default value is used if the argument is not provided (after environment variable).
func (*Argument) SetDescription ¶
SetDescription is a builder method to sets argument description. Description is an elaborate explanation which is used in help generation.
func (*Argument) SetEnv ¶
SetEnv is a builder method to set environment variable name, from which to take the value, if not provided (before default).
func (*Argument) SetParse ¶
func (this *Argument) SetParse(v ParseMethod) *Argument
SetParse is a builder method to set setup call on value. The setup call must return a replace value (can be unchanged) or an error, which stops evaluation and returns error to user.
func (*Argument) SetRegex ¶
SetRegex is a builder method to set regular expression which is used to check the the argument input (in case of multiple: each will be checked)
func (*Argument) SetUsage ¶
SetUsage is builder method to set the usage description. Usage is a short account of what the argument is used for, for help generaiton.
func (*Argument) String ¶
func (this *Argument) String() string
String representation of the value (can be empty string)
func (*Argument) Strings ¶
func (this *Argument) Strings() []string
Strings returns values as array of strings
type CallMethod ¶
type CallMethod interface{}
CallMethod is the interface for functions used as command callbacks. `interface{}` is used so that those functions can have arbitrary input and output signatures. However, they still must be functions.
type Cli ¶
type Cli struct { // Name is the name of the console application used in the generated help Name string // Version is used in the generated help Version string // Description is used in the generated help Description string // Commands contain all registered commands and can be manipulated directly Commands map[string]*Command // Heralds contain list of command-create-callbacks which will be executed on `Run()` Heralds []HeraldCallback // Registry is a container holding objects for injection Registry *Registry // DefaultOptions contains options which are added to all commands early in the `Run()` call. DefaultOptions []*Option // DefaultCommand contains name of the command which is executed if non is given. Defaults to "list" DefaultCommand string // contains filtered or unexported fields }
Cli is a command line interface object
func (*Cli) AddDefaultOptions ¶
AddDefaultOptions adds a list of options to default options.
func (*Cli) Call ¶
Call executes command by building all input parameters based on objects registered in the container and running the callback.
func (*Cli) Herald ¶
func (this *Cli) Herald(cmd ...HeraldCallback) *Cli
Herald registers command constructors, which will be executed in `Run()`.
func (*Cli) New ¶
func (this *Cli) New(name, usage string, call CallMethod) *Cli
New creates and adds a new command
func (*Cli) NewDefaultOption ¶
func (this *Cli) NewDefaultOption(name, alias, usage, _default string, required, multiple bool) *Cli
NewDefaultOption creates and adds a new option to default list.
func (*Cli) RegisterAs ¶
RegisterAs is builder method and registers object under alias in registry
func (*Cli) RegisterNamed ¶
RegisterNamed registers a parameter for injecting in a named map[string]inteface{}
func (*Cli) SetDefaultCommand ¶
SetDefaultCommand is builder method and overwrites the default command ("list") with something else
func (*Cli) SetDescription ¶
SetDescription is builder method and sets description
func (*Cli) SetOnInterrupt ¶
SetOnInterrupt sets callback for interrupt signal (ctrl+c)
type Command ¶
type Command struct { // Cli back-references the Cli in which the command is registered Cli *Cli // Name is the unique (within Cli scope) call-name of the command Name string // Usage is a shorthand description of what the command does. Used in help output. Usage string // Description is a long elaboration on what the command does. Used in help output. Description string // Options contain all the registered options of the command. Options []*Option // Arguments contain all the registered arguments of the command. Arguments []*Argument // Call holds reflections of the callback. Call reflect.Value }
Command represents a named callback with a set of arguments and options
func NewCommand ¶
func NewCommand(name, usage string, call CallMethod) *Command
NewCommand constructs a new command
func NewHelpCommand ¶
func NewHelpCommand() *Command
NewHelpCommand returns the default help command
func NewListCommand ¶
func NewListCommand() *Command
NewListCommand returns the default help command
func (*Command) AddArgument ¶
AddArgument is builder method to add a new argument
func (*Command) Input ¶
Input returns map containing whole input values (of all options, all arguments)
func (*Command) NewArgument ¶
NewArgument is builder method to construct and add a new argument
func (*Command) NewOption ¶
func (this *Command) NewOption(name, alias, usage, _default string, required, multiple bool) *Command
NewOption is builder method to construct and add a new option
func (*Command) SetDescription ¶
SetDescription is builder method setting description
type CommandsSort ¶
type CommandsSort []*Command
CommandSort implements the `sort.Sortable` interface for commands, based on the command `Name` attribute
func (CommandsSort) Len ¶
func (this CommandsSort) Len() int
func (CommandsSort) Less ¶
func (this CommandsSort) Less(i, j int) bool
func (CommandsSort) Swap ¶
func (this CommandsSort) Swap(i, j int)
type DefaultFormatter ¶
type DefaultFormatter struct {
// contains filtered or unexported fields
}
DefaultFormatter strips all formatting from the output message
func NewDefaultFormatter ¶
func NewDefaultFormatter(styles map[string]string) *DefaultFormatter
NewDefaultFormatter constructs a new constructor with the given styles
func (*DefaultFormatter) Escape ¶
func (this *DefaultFormatter) Escape(msg string) string
func (*DefaultFormatter) Format ¶
func (this *DefaultFormatter) Format(msg string) string
type DefaultInput ¶
type DefaultInput struct {
// contains filtered or unexported fields
}
DefaultInput is the default used input implementation
func NewDefaultInput ¶
func NewDefaultInput(in io.Reader, out Output) *DefaultInput
NewDefaultInput constructs a new default input implementation on given io reader (if nil, fall back to `os.Stdin`). Requires Output for issuing questions to user.
func (*DefaultInput) Ask ¶
func (this *DefaultInput) Ask(question string, check func(string) error) string
func (*DefaultInput) AskRegex ¶
func (this *DefaultInput) AskRegex(question string, rx *regexp.Regexp) string
func (*DefaultInput) Choose ¶
func (this *DefaultInput) Choose(question string, choices map[string]string) string
func (*DefaultInput) Confirm ¶
func (this *DefaultInput) Confirm(question string) bool
type DefaultOutput ¶
type DefaultOutput struct {
// contains filtered or unexported fields
}
DefaultOutput is the default used output type
func NewColorOutput ¶
func NewColorOutput(io io.Writer) *DefaultOutput
NewColoredOutput returns default output (on `os.Stdout`, if io is nil) using a formatter which applies the default color styles to style tokens on output.
func NewMonochromeOutput ¶
func NewMonochromeOutput(io io.Writer) *DefaultOutput
NewMonochromeOutput returns default output (on `os.Stdout`, if io is nil) using a formatter which strips all style tokens (hence rendering non-colored, plain strings)
func NewOutput ¶
func NewOutput(io io.Writer, f Formatter) *DefaultOutput
NewOutput generates a new (default) output with provided io writer (if nil then `os.Stdout` is used) and a formatter
func (*DefaultOutput) Escape ¶
func (this *DefaultOutput) Escape(msg string) string
func (*DefaultOutput) Printf ¶
func (this *DefaultOutput) Printf(msg string, args ...interface{})
func (*DefaultOutput) SetFormatter ¶
func (this *DefaultOutput) SetFormatter(f Formatter) Output
func (*DefaultOutput) Sprintf ¶
func (this *DefaultOutput) Sprintf(msg string, args ...interface{}) string
type Formatter ¶
type Formatter interface { // Escape escapes a string, so that no formatter tokens will be interpolated (eg `<foo>` -> `\<foo>`) Escape(msg string) string // Format renders message for output by applying <style> tokens Format(msg string) string }
Formatter is used by Output for rendering. It supports style directives in the form <info> or <end> or suchlike
type HeraldCallback ¶
HeraldCallback is type for creator method, which can be registered with the `Herald()` method.
type Input ¶
type Input interface { // Ask prints question to user and then reads user input and returns as soon // as it's non empty or queries again until it is Ask(question string, check func(string) error) string // AskRegex prints question to user and then reads user input, compares it // against regex and return if matching or queries again until it does AskRegex(question string, rx *regexp.Regexp) string // Choose renders choices for user and returns what was choosen Choose(question string, choices map[string]string) string // Confirm prints question to user until she replies with "yes", "y", "no" or "n" Confirm(question string) bool }
Input is an interface for input helping. It provides shorthand methods for often used CLI interactions.
type Option ¶
type Option struct { // Alias can be a shorter name Alias string // If is a flag, then no value can be assigned (if present, then bool true) Flag bool // contains filtered or unexported fields }
Option is a user input which is initialized with a single or double dash (eg "--foo" or "-f"). It may not be followed by a value, in which case it is considered a flag (see `IsFlag`). Options can be multiple inputs (no restrictions as there are for Arguments). An option can be required or optional. Options do not need to have any particular order.
func (*Option) Assign ¶
Assign tries to add value to parameter and returns error if it fails due to invalid format or invalid amount (single vs multiple parameters)
func (*Option) Bool ¶
func (this *Option) Bool() bool
Bool representation of the value (will be false, if not given or not parsable)
func (*Option) Bools ¶
func (this *Option) Bools() []bool
Bools returns values as bool array (values will be false, if not parsable to float64)
func (*Option) Float ¶
func (this *Option) Float() float64
Float representation of the value (will be 0.0, if not given or not parsable)
func (*Option) Floats ¶
func (this *Option) Floats() []float64
Floats returns values as float64 array (values will be 0.0, if not parsable to float64)
func (*Option) Int ¶
func (this *Option) Int() int
Int representation of the value (will be 0, if not given or not parsable)
func (*Option) Ints ¶
func (this *Option) Ints() []int
Ints returns values as int array (values will be 0, if not parsable to int)
func (*Option) IsFlag ¶
IsFlag marks an option as a flag. A Flag does not have any values. If it exists (eg "--verbose"), then it is automatically initialized with the string "true", which then can be checked with the `Bool()` method for actual `bool`
func (*Option) Json ¶
Json assumes the input is a JSON string and parses into a standard map[string]interface{} Returns error, if not parsable (or eg array JSON).
Helpful to allow complex inputs: `my-app do --foo '{"bar": "baz"}'`
func (*Option) Provided ¶
func (this *Option) Provided() bool
Provided returns bool whether argument was provided
func (*Option) SetDefault ¶
SetDefault is a builder method to set default value. Default value is used if the option is not provided (after environment variable).
func (*Option) SetDescription ¶
SetDescription is a builder method to sets option description. Description is an elaborate explanation which is used in help generation.
func (*Option) SetEnv ¶
SetEnv is a builder method to set environment variable name, from which to take the value, if not provided (before default).
func (*Option) SetParse ¶
func (this *Option) SetParse(v ParseMethod) *Option
SetParse is a builder method to set setup call on value. The setup call must return a replace value (can be unchanged) or an error, which stops evaluation and returns error to user.
func (*Option) SetRegex ¶
SetRegex is a builder method to set regular expression which is used to check the the option input (in case of multiple: each will be checked)
func (*Option) SetUsage ¶
SetUsage is builder method to set the usage description. Usage is a short account of what the option is used for, for help generaiton.
func (*Option) String ¶
func (this *Option) String() string
String representation of the value (can be empty string)
func (*Option) Strings ¶
func (this *Option) Strings() []string
Strings returns values as array of strings
type Output ¶
type Output interface { // Escape escapes a string, so that no formatter tokens will be interpolated (eg `<foo>` -> `\<foo>`) Escape(s string) string // Printf applies format (renders styles) and writes to output Printf(msg string, args ...interface{}) // Sprintf applies format (renders styles) and returns as string Sprintf(msg string, args ...interface{}) string // SetFormatter is builder method and replaces current formatter SetFormatter(f Formatter) Output }
Output is interface for
type ParseMethod ¶
SetupMethod is type for callback on Setup of Argument or Option. The return string replaces the original input. Called on each value (in case of multiple).
func IsAll ¶
func IsAll(v ...ParseMethod) ParseMethod
IsAll joins a set of validators methods and returns true if ALL of them match
func IsAny ¶
func IsAny(v ...ParseMethod) ParseMethod
IsAny joins a set of validator methods and returns true if ANY of them matches
type ReduceMethod ¶
ReduceMethod is type of the callback of the `Reduce` and `ReduceAsync` methods
type Registry ¶
Registry is a small container holding objects which are injected into command calls.
func (*Registry) Get ¶
Get returns registered object by their type name (`reflect.TypeOf(..).String()`)
func (*Registry) Reduce ¶
func (this *Registry) Reduce(cb ReduceMethod) []interface{}
Reduce calls a bool-returning function on all registered objects in alphanumerical order and returns all selected as a slice
func (*Registry) ReduceAsync ¶
func (this *Registry) ReduceAsync(cb ReduceMethod) chan interface{}
ReduceAsync calls a bool-returning function on all registered objects concurrently and writes all selected objects in a return channel