Documentation
¶
Index ¶
- Variables
- func IsFloat(name, value string) error
- func IsInt(name, value 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) *parameter
- func (this *Argument) SetDescription(v string) *parameter
- func (this *Argument) SetParser(v ParserMethod) *parameter
- func (this *Argument) SetRegex(r *regexp.Regexp) *parameter
- func (this *Argument) SetUsage(v string) *parameter
- func (this *Argument) SetValidator(v ValidatorMethod) *parameter
- 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) New(name, usage string, call CallMethod) *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) Run()
- func (this *Cli) RunWith(args []string)
- func (this *Cli) SetInput(in Input) *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 Input
- 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) *parameter
- func (this *Option) SetDescription(v string) *parameter
- func (this *Option) SetParser(v ParserMethod) *parameter
- func (this *Option) SetRegex(r *regexp.Regexp) *parameter
- func (this *Option) SetUsage(v string) *parameter
- func (this *Option) SetValidator(v ValidatorMethod) *parameter
- 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 ParserMethod
- type Registry
- type ValidatorMethod
Constants ¶
This section is empty.
Variables ¶
var DefaultHelpOption = &Option{ parameter: parameter{ Name: "help", Usage: "Display this help message", Description: "Display this help message", }, Alias: "h", Flag: true, }
DefaultOptions are prepended to any newly created command
var DefaultOptions = []*Option{DefaultHelpOption}
DefaultOptions are prepended to any newly created command
var DefaultStyles = map[string]string{
"error": "\033[31;1m",
"warn": "\033[33m",
"info": "\033[36m",
"success": "\033[32m",
"debug": "\033[30;1m",
"headline": "\033[4;1m",
"subline": "\033[4m",
"important": "\033[47;30;1m",
"query": "\033[34m",
"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" }
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 if p.Multiple { short = short + " ..." usg += " (mult)" } if p.Required { usg += " (req)" } else { short = fmt.Sprintf("[%s]", short) } if p.Default != "" { usg += fmt.Sprintf(" (default: \"%s\")", p.Default) } if l := len(p.Name); l > argMax { argMax = l } usage = append(usage, short) 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 if !p.Required { short = "[" + short + "]" } else { usg += " (req)" } if p.Multiple { short = short + " ..." usg += " (mult)" } if p.Default != "" { usg += fmt.Sprintf(" (default: \"%s\")", p.Default) } if l := len(long); l > optMax { optMax = l } usage = append(usage, short) 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" }
var Die = func(msg string, args ...interface{}) { fmt.Printf(msg+"\n", args...) os.Exit(1) }
var RenderChooseOption = func(key, value string, size int) string { return fmt.Sprintf(" <query>%-"+fmt.Sprintf("%d", size+1)+"s<reset> %s\n", key+")", value) }
var RenderChooseQuery = func() string {
return "Choose: "
}
var RenderChooseQuestion = func(question string) string {
return question + "\n"
}
Functions ¶
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 orderd after required arguments. Only one argument is allowed to contain multiple values and it needs to be the last one.
func NewArgument ¶
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
Float representation of the value (will be false, if not given or not parsable)
func (*Argument) Bools ¶
func (this *Argument) Bools() []bool
Ints representation of the value (can be nil, if non given or slice of 0-values for)
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, if not given or not parsable)
func (*Argument) Floats ¶
func (this *Argument) Floats() []float64
Ints representation of the value (can be nil, if non given or slice of 0-values for)
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 representation of the value (can be nil, if non given or slice of 0-values for)
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)
func (*Argument) Provided ¶
func (this *Argument) Provided() bool
Provided returns bool whether argument was provided
func (*Argument) SetDefault ¶
func (this *Argument) SetDefault(v string) *parameter
SetDefault is a builder method to set default value
func (*Argument) SetDescription ¶
func (this *Argument) SetDescription(v string) *parameter
SetDescription is a builder method to set description
func (*Argument) SetParser ¶
func (this *Argument) SetParser(v ParserMethod) *parameter
SetDefault is a builder method to set default value
func (*Argument) SetUsage ¶
func (this *Argument) SetUsage(v string) *parameter
SetUsage is a builder method to set usage
func (*Argument) SetValidator ¶
func (this *Argument) SetValidator(v ValidatorMethod) *parameter
SetDefault is a builder method to set default value
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 representation of the value (can be nil)
type CallMethod ¶
type CallMethod interface{}
type Cli ¶
type Cli struct {
Name string
Version string
Description string
Commands map[string]*Command
Registry *Registry
}
Cli is a command line interface object
func (*Cli) New ¶
func (this *Cli) New(name, usage string, call CallMethod) *Cli
New creates and adds a new command
func (*Cli) RegisterAs ¶
RegisterAs is builder method and registers object under alias in registry
type Command ¶
type Command struct {
Cli *Cli
Name string
Usage string
Description string
Options []*Option
Arguments []*Argument
Call reflect.Value
}
Command ..
func NewCommand ¶
func NewCommand(name, usage string, call CallMethod) *Command
NewCommand constructs new command
func NewHelpCommand ¶
func NewHelpCommand() *Command
func (*Command) AddArgument ¶
AddArgument is builder method to add a new argument
func (*Command) NewArgument ¶
AddArgument is builder method to add a new argument
func (*Command) NewOption ¶
func (this *Command) NewOption(name, alias, usage, _default string, required, multiple bool) *Command
AddOption is builder method to add a new option
func (*Command) SetDescription ¶
SetDescription is builder method setting desciption
type CommandsSort ¶
type CommandsSort []*Command
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
func (*DefaultFormatter) Format ¶
func (this *DefaultFormatter) Format(msg string) string
type DefaultInput ¶
type DefaultInput struct {
// contains filtered or unexported fields
}
func NewDefaultInput ¶
func NewDefaultInput(in io.Reader, out Output) *DefaultInput
func (*DefaultInput) Ask ¶
func (this *DefaultInput) Ask(question string, check func(string) error) string
type DefaultOutput ¶
type DefaultOutput struct {
// contains filtered or unexported fields
}
func NewFancyOutput ¶
func NewFancyOutput(io io.Writer) *DefaultOutput
func NewPlainOutput ¶
func NewPlainOutput(io io.Writer) *DefaultOutput
func (*DefaultOutput) Printf ¶
func (this *DefaultOutput) Printf(msg string, args ...interface{})
func (*DefaultOutput) SetFormatter ¶
func (this *DefaultOutput) SetFormatter(f Formatter) Output
type Formatter ¶
type Formatter interface {
// Format renders message with args for output
Format(msg string) string
}
Formatter is used by Output for rendering. It supports style directives in the form <info> or <end> or suchlike
type Input ¶
type Input interface {
// Ask 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 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
}
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 like an argument, but with a single or double dash in front
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
Float representation of the value (will be false, if not given or not parsable)
func (*Option) Bools ¶
func (this *Option) Bools() []bool
Ints representation of the value (can be nil, if non given or slice of 0-values for)
func (*Option) Float ¶
func (this *Option) Float() float64
Float representation of the value (will be 0, if not given or not parsable)
func (*Option) Floats ¶
func (this *Option) Floats() []float64
Ints representation of the value (can be nil, if non given or slice of 0-values for)
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 representation of the value (can be nil, if non given or slice of 0-values for)
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)
func (*Option) Provided ¶
func (this *Option) Provided() bool
Provided returns bool whether argument was provided
func (*Option) SetDefault ¶
func (this *Option) SetDefault(v string) *parameter
SetDefault is a builder method to set default value
func (*Option) SetDescription ¶
func (this *Option) SetDescription(v string) *parameter
SetDescription is a builder method to set description
func (*Option) SetParser ¶
func (this *Option) SetParser(v ParserMethod) *parameter
SetDefault is a builder method to set default value
func (*Option) SetUsage ¶
func (this *Option) SetUsage(v string) *parameter
SetUsage is a builder method to set usage
func (*Option) SetValidator ¶
func (this *Option) SetValidator(v ValidatorMethod) *parameter
SetDefault is a builder method to set default value
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 representation of the value (can be nil)
type Output ¶
type Output interface {
// Printf writes to output
Printf(msg string, args ...interface{})
// SetFormatter is builder method and replaces current formatter
SetFormatter(f Formatter) Output
}
Output is interface for outputter classes
type ParserMethod ¶
type Registry ¶
type Registry struct {
// contains filtered or unexported fields
}
func NewRegistry ¶
func NewRegistry() *Registry
type ValidatorMethod ¶
func IsAll ¶
func IsAll(v ...ValidatorMethod) ValidatorMethod
IsAll joins a set of validators methods and returns true if ALL of them match
func IsAny ¶
func IsAny(v ...ValidatorMethod) ValidatorMethod
IsAny joins a set of validator methods and returns true if ANY of them matches

