Documentation ¶
Overview ¶
* Copyright (C) 1999-2019 Alibaba Group Holding Limited
* Copyright (C) 1999-2019 Alibaba Group Holding Limited
* Copyright (C) 1999-2019 Alibaba Group Holding Limited
* Copyright (C) 1999-2019 Alibaba Group Holding Limited
* Copyright (C) 1999-2019 Alibaba Group Holding Limited
* Copyright (C) 1999-2019 Alibaba Group Holding Limited
* Copyright (C) 1999-2019 Alibaba Group Holding Limited
* Copyright (C) 1999-2019 Alibaba Group Holding Limited
* Copyright (C) 1999-2019 Alibaba Group Holding Limited
* Copyright (C) 1999-2019 Alibaba Group Holding Limited
* Copyright (C) 1999-2019 Alibaba Group Holding Limited
* Copyright (C) 1999-2019 Alibaba Group Holding Limited
* Copyright (C) 1999-2019 Alibaba Group Holding Limited
Index ¶
- Constants
- Variables
- func APIListColor() string
- func CalculateStringDistance(source string, target string) int
- func Debug(w io.Writer, a ...interface{}) (n int, err error)
- func Debugf(w io.Writer, format string, args ...interface{}) (n int, err error)
- func DefaultWriter() io.Writer
- func DisableColor()
- func DisableExitCode()
- func DistanceForMatrix(matrix [][]int) int
- func DistanceForStrings(source []rune, target []rune, op Options) int
- func EnableColor()
- func EnableExitCode()
- func Error(w io.Writer, a ...interface{}) (n int, err error)
- func Errorf(w io.Writer, format string, args ...interface{}) (n int, err error)
- func Exit(code int)
- func GetVersion() string
- func Info(w io.Writer, a ...interface{}) (n int, err error)
- func Infof(w io.Writer, format string, args ...interface{}) (n int, err error)
- func LogMatrix(source []rune, target []rune, matrix [][]int)
- func MatrixForStrings(source []rune, target []rune, op Options) [][]int
- func NewErrorWithTip(err error, tipFormat string, args ...interface{}) error
- func NewInvalidCommandError(name string, ctx *Context) error
- func NewInvalidFlagError(name string, ctx *Context) error
- func Notice(w io.Writer, a ...interface{}) (n int, err error)
- func Noticef(w io.Writer, format string, args ...interface{}) (n int, err error)
- func PlatformCompatible()
- func Print(w io.Writer, a ...interface{}) (n int, err error)
- func PrintSuggestions(ctx *Context, lang string, ss []string)
- func PrintWithColor(w io.Writer, color string, a ...interface{}) (n int, err error)
- func Printf(w io.Writer, format string, args ...interface{}) (n int, err error)
- func PrintfWithColor(w io.Writer, color string, format string, args ...interface{}) (n int, err error)
- func Println(w io.Writer, a ...interface{}) (n int, err error)
- func ProductListColor() string
- func RatioForMatrix(matrix [][]int) float64
- func RatioForStrings(source []rune, target []rune, op Options) float64
- func SetAPIListColor(color string)
- func SetProductListColor(color string)
- func SplitString(s string, sep string) []string
- func SplitStringWithPrefix(s string, splitters string) (string, string, bool)
- func UnquoteString(s string) string
- func Warning(w io.Writer, a ...interface{}) (n int, err error)
- func Warningf(w io.Writer, format string, args ...interface{}) (n int, err error)
- func WriteMatrix(source []rune, target []rune, matrix [][]int, writer io.Writer)
- type AssignedMode
- type Command
- func (c *Command) AddSubCommand(cmd *Command)
- func (c *Command) Execute(ctx *Context, args []string)
- func (c *Command) ExecuteComplete(ctx *Context, args []string)
- func (c *Command) Flags() *FlagSet
- func (c *Command) GetSubCommand(s string) *Command
- func (c *Command) GetSuggestDistance() int
- func (c *Command) GetSuggestions(s string) []string
- func (c *Command) GetUsageWithParent() string
- func (c *Command) PrintFailed(ctx *Context, err error, suggestion string)
- func (c *Command) PrintFlags(ctx *Context)
- func (c *Command) PrintHead(ctx *Context)
- func (c *Command) PrintSample(ctx *Context)
- func (c *Command) PrintSubCommands(ctx *Context)
- func (c *Command) PrintTail(ctx *Context)
- func (c *Command) PrintUsage(ctx *Context)
- type Completion
- type Context
- func (ctx *Context) CheckFlags() error
- func (ctx *Context) Command() *Command
- func (ctx *Context) Completion() *Completion
- func (ctx *Context) EnterCommand(cmd *Command)
- func (ctx *Context) Flags() *FlagSet
- func (ctx *Context) IsHelp() bool
- func (ctx *Context) SetCompletion(completion *Completion)
- func (ctx *Context) SetUnknownFlags(flags *FlagSet)
- func (ctx *Context) UnknownFlags() *FlagSet
- func (ctx *Context) Writer() io.Writer
- type EditOperation
- type EditScript
- type ErrorWithTip
- type Field
- type Flag
- func (f *Flag) GetFieldValue(key string) (string, bool)
- func (f *Flag) GetFieldValues(key string) []string
- func (f *Flag) GetFormations() []string
- func (f *Flag) GetIntegerOrDefault(def int) int
- func (f *Flag) GetStringOrDefault(def string) string
- func (f *Flag) GetValue() (string, bool)
- func (f *Flag) GetValues() []string
- func (f *Flag) IsAssigned() bool
- func (f *Flag) SetAssigned(istrue bool)
- func (f *Flag) SetValue(value string)
- func (f *Flag) SetValues(values []string)
- type FlagSet
- func (fs *FlagSet) Add(f *Flag)
- func (fs *FlagSet) AddByName(name string) (*Flag, error)
- func (fs *FlagSet) Flags() []*Flag
- func (fs *FlagSet) Get(name string) *Flag
- func (fs *FlagSet) GetByShorthand(c rune) *Flag
- func (fs *FlagSet) GetSuggestions(name string, distance int) []string
- func (fs *FlagSet) GetValue(name string) (string, bool)
- type InvalidCommandError
- type InvalidFlagError
- type MatchFunction
- type Options
- type Output
- type Parser
- type Suggester
- type SuggestibleError
Examples ¶
Constants ¶
const ( ColorOff = "\033[0m" // Reset Color // Regular Colors Black = "\033[0;30m" // Black Red = "\033[0;31m" // Red Green = "\033[0;32m" // Green Yellow = "\033[0;33m" // Yellow Blue = "\033[0;34m" // Blue Purple = "\033[0;35m" // Purple Cyan = "\033[0;36m" // Cyan White = "\033[0;37m" // White // Bold BBlack = "\033[1;30m" // Black BRed = "\033[1;31m" // Red BGreen = "\033[1;32m" // Green BYellow = "\033[1;33m" // Yellow BBlue = "\033[1;34m" // Blue BPurple = "\033[1;35m" // Purple BCyan = "\033[1;36m" // Cyan BWhite = "\033[1;37m" // White // Underline UBlack = "\033[4;30m" // Black URed = "\033[4;31m" // Red UGreen = "\033[4;32m" // Green UYellow = "\033[4;33m" // Yellow UBlue = "\033[4;34m" // Blue UPurple = "\033[4;35m" // Purple UCyan = "\033[4;36m" // Cyan UWhite = "\033[4;37m" // White // Background OnBlack = "\033[40m" // Black OnRed = "\033[41m" // Red OnGreen = "\033[42m" // Green OnYellow = "\033[43m" // Yellow OnBlue = "\033[44m" // Blue OnPurple = "\033[45m" // Purple OnCyan = "\033[46m" // Cyan OnWhite = "\033[47m" // White // High Intensty IBlack = "\033[0;90m" // Black IRed = "\033[0;91m" // Red IGreen = "\033[0;92m" // Green IYellow = "\033[0;93m" // Yellow IBlue = "\033[0;94m" // Blue IPurple = "\033[0;95m" // Purple ICyan = "\033[0;96m" // Cyan IWhite = "\033[0;97m" // White // Bold High Intensty BIBlack = "\033[1;90m" // Black BIRed = "\033[1;91m" // Red BIGreen = "\033[1;92m" // Green BIYellow = "\033[1;93m" // Yellow BIBlue = "\033[1;94m" // Blue BIPurple = "\033[1;95m" // Purple BICyan = "\033[1;96m" // Cyan BIWhite = "\033[1;97m" // White // High Intensty backgrounds OnIBlack = "\033[0;100m" // Black OnIRed = "\033[0;101m" // Red OnIGreen = "\033[0;102m" // Green OnIYellow = "\033[0;103m" // Yellow OnIBlue = "\033[0;104m" // Blue OnIPurple = "\033[10;95m" // Purple OnICyan = "\033[0;106m" // Cyan OnIWhite = "\033[0;107m" // White )
const ( DebugColor = White InfoColor = Cyan NoticeColor = BYellow WarningColor = BPurple ErrorColor = BRed )
const ( AssignedNone = AssignedMode(-1) AssignedDefault = AssignedMode(0) AssignedOnce = AssignedMode(1) AssignedRepeatable = AssignedMode(9) )
const ( Ins = iota Del Sub Match )
const DefaultSuggestDistance = 2
Variables ¶
var (
Version = "0.0.1"
)
This variable is replaced in compile time `-ldflags "-X 'github.com/aliyun/aliyun-cli/cli.Version=${VERSION}'"`
Functions ¶
func APIListColor ¶
func APIListColor() string
func CalculateStringDistance ¶
func DefaultWriter ¶
func DisableColor ¶
func DisableColor()
func DisableExitCode ¶
func DisableExitCode()
func DistanceForMatrix ¶
DistanceForMatrix reads the edit distance off the given Levenshtein matrix.
func DistanceForStrings ¶
DistanceForStrings returns the edit distance between source and target.
It has a runtime proportional to len(source) * len(target) and memory use proportional to len(target).
Example ¶
source := "a" target := "aa" distance := DistanceForStrings([]rune(source), []rune(target), DefaultOptions) fmt.Printf(`Distance between "%s" and "%s" computed as %d`, source, target, distance)
Output: Distance between "a" and "aa" computed as 1
func EnableColor ¶
func EnableColor()
func EnableExitCode ¶
func EnableExitCode()
func GetVersion ¶
func GetVersion() string
func LogMatrix ¶
LogMatrix writes a visual representation of the given matrix for the given strings to os.Stderr. This function is deprecated, use WriteMatrix(source, target, matrix, os.Stderr) instead.
func MatrixForStrings ¶
MatrixForStrings generates a 2-D array representing the dynamic programming table used by the Levenshtein algorithm, as described e.g. here: http://www.let.rug.nl/kleiweg/lev/ The reason for putting the creation of the table into a separate function is that it cannot only be used for reading of the edit distance between two strings, but also e.g. to backtrace an edit script that provides an alignment between the characters of both strings.
func NewErrorWithTip ¶
func NewInvalidCommandError ¶
func NewInvalidFlagError ¶
func PlatformCompatible ¶
func PlatformCompatible()
func PrintSuggestions ¶
func PrintWithColor ¶
func PrintfWithColor ¶
func ProductListColor ¶
func ProductListColor() string
func RatioForMatrix ¶
RatioForMatrix returns the Levenshtein ratio for the given matrix. The ratio is computed as follows:
(sourceLength + targetLength - distance) / (sourceLength + targetLength)
func RatioForStrings ¶
RatioForStrings returns the Levenshtein ratio for the given strings. The ratio is computed as follows:
(sourceLength + targetLength - distance) / (sourceLength + targetLength)
func SetAPIListColor ¶
func SetAPIListColor(color string)
func SetProductListColor ¶
func SetProductListColor(color string)
func SplitString ¶
func SplitStringWithPrefix ¶
SplitStringWithPrefix TODO can use function string.SplitN to replace
func UnquoteString ¶
func WriteMatrix ¶
WriteMatrix writes a visual representation of the given matrix for the given strings to the given writer.
Example ¶
source := []rune("neighbor") target := []rune("Neighbour") matrix := MatrixForStrings(source, target, DefaultOptions) WriteMatrix(source, target, matrix, os.Stdout)
Output: N e i g h b o u r 0 1 2 3 4 5 6 7 8 9 n 1 2 3 4 5 6 7 8 9 10 e 2 3 2 3 4 5 6 7 8 9 i 3 4 3 2 3 4 5 6 7 8 g 4 5 4 3 2 3 4 5 6 7 h 5 6 5 4 3 2 3 4 5 6 b 6 7 6 5 4 3 2 3 4 5 o 7 8 7 6 5 4 3 2 3 4 r 8 9 8 7 6 5 4 3 4 3
Types ¶
type AssignedMode ¶
type AssignedMode int
type Command ¶
type Command struct { // Command Name Name string // Short is the short description shown in the 'help' output. Short *i18n.Text // Long is the long message shown in the 'help <this-command>' output. Long *i18n.Text // Syntax for usage Usage string // Sample command Sample string // Enable unknown flags EnableUnknownFlag bool // enable suggest distance, // disable -1 // 0: default distance SuggestDistance int // Hidden command Hidden bool // Run, command error will be catch Run func(ctx *Context, args []string) error // Help Help func(ctx *Context, args []string) error // auto compete AutoComplete func(ctx *Context, args []string) []string // contains filtered or unexported fields }
func NewAutoCompleteCommand ¶
func NewAutoCompleteCommand() *Command
func NewVersionCommand ¶
func NewVersionCommand() *Command
func (*Command) AddSubCommand ¶
func (*Command) ExecuteComplete ¶
func (*Command) GetSubCommand ¶
func (*Command) GetSuggestDistance ¶
func (*Command) GetSuggestions ¶
func (*Command) GetUsageWithParent ¶
func (*Command) PrintFailed ¶
func (*Command) PrintFlags ¶
func (*Command) PrintSample ¶
func (*Command) PrintSubCommands ¶
func (*Command) PrintUsage ¶
type Completion ¶
func ParseCompletion ¶
func ParseCompletion(line, point string) *Completion
func ParseCompletionForShell ¶
func ParseCompletionForShell() *Completion
func (*Completion) GetArgs ¶
func (c *Completion) GetArgs() []string
func (*Completion) GetCurrent ¶
func (c *Completion) GetCurrent() string
type Context ¶
type Context struct {
// contains filtered or unexported fields
}
CLI Command Context
func NewCommandContext ¶
func (*Context) CheckFlags ¶
func (*Context) Completion ¶
func (ctx *Context) Completion() *Completion
func (*Context) EnterCommand ¶
Before go into the sub command, we need traverse flags and merge with parent
func (*Context) SetCompletion ¶
func (ctx *Context) SetCompletion(completion *Completion)
func (*Context) SetUnknownFlags ¶
func (*Context) UnknownFlags ¶
type EditOperation ¶
type EditOperation int
func (EditOperation) String ¶
func (operation EditOperation) String() string
type EditScript ¶
type EditScript []EditOperation
func EditScriptForMatrix ¶
func EditScriptForMatrix(matrix [][]int, op Options) EditScript
EditScriptForMatrix returns an optimal edit script based on the given Levenshtein matrix.
func EditScriptForStrings ¶
func EditScriptForStrings(source []rune, target []rune, op Options) EditScript
EditScriptForStrings returns an optimal edit script to turn source into target.
type ErrorWithTip ¶
If command.Execute return Noticeable error, print i18n Notice under error information
type Field ¶
type Field struct { // // appear in `--flag key1=value1, key2=value2` // if Key assigned with "", it can used with `--flag value1 value2` Key string // // if Required is true, this field must be assigned Required bool // // if Repeatable is true, this field can appear multiply times, eg: "--flag key1=value1 key2=value2" Repeatable bool // // if field not appear, use this value, not used with Required DefaultValue string // // Message show Short *i18n.Text // contains filtered or unexported fields }
func (*Field) SetAssigned ¶
type Flag ¶
type Flag struct { // Name of flag --{Name} Name string // Flag is the single characters Shorthand rune // Message print with --help command Short *i18n.Text // Message print with `help --flag` command Long *i18n.Text // If DefaultValue is not "" and Required is true, if flag is not assign // GetValue() will return DefaultValue, and IsAssigned() will be false DefaultValue string // If Required is true, the flag must be assigned with `--flag value` or DefaultValue is not empty Required bool // Enable flag has alias names Aliases []string // Ref to AssignedMode // `AssignedNone`: flag only appear with `--flag1` `--flag2` // `AssignedDefault`: flag can appear with `--flag1` or `--flag1 value1` // `AssignedOnce`: flag only appear with `--flag1 value1` // `AssignedRepeatable`: flag can appear multi times sample: `--flag1 [v1] [v2] [v3] [field1=value1]`, use with Fields AssignedMode AssignedMode // If Persistent is true, the flag can apply to child commands Persistent bool // If Hidden is true, it will not appear in --help mode Hidden bool // Using in FlagSet.GetByCategory()... Category string // Use to validate flag is in correct format Validate func(f *Flag) error // Flag can assigned with --flag field1=value1 field2=value2 value3 ... // must used with AssignedMode=AssignedRepeatable Fields []Field // Flag can't appear with other flags, use Flag.Name ExcludeWith []string // contains filtered or unexported fields }
func NewHelpFlag ¶
func NewHelpFlag() *Flag
func (*Flag) GetFieldValue ¶
--flag field1=value1
func (*Flag) GetFieldValues ¶
func (*Flag) GetFormations ¶
get all appears forms, maybe {"--Name", "--Alias1", "-Shorthand"}
func (*Flag) GetIntegerOrDefault ¶
TODO: flag support integer validate return def if Flag is not assign or assign failed
func (*Flag) GetStringOrDefault ¶
return def if Flag is not assigned
func (*Flag) GetValue ¶
return flag value, if not assigned return f.DefaultValue
for `AssignedMode == AssignedRepeatable`. Use GetValues() to get all values
func (*Flag) IsAssigned ¶
return true if flag appeared, either `--flag1` or `--flag1 value1`
func (*Flag) SetAssigned ¶
type FlagSet ¶
type FlagSet struct {
// contains filtered or unexported fields
}
func NewFlagSet ¶
func NewFlagSet() *FlagSet
func (*FlagSet) GetByShorthand ¶
get flag by shorthand, sample -a
func (*FlagSet) GetSuggestions ¶
get suggestions
type InvalidCommandError ¶
type InvalidCommandError struct { Name string // contains filtered or unexported fields }
OUTPUT: Error: "'%s' is not a valid command
{Hint}
func (*InvalidCommandError) Error ¶
func (e *InvalidCommandError) Error() string
func (*InvalidCommandError) GetSuggestions ¶
func (e *InvalidCommandError) GetSuggestions() []string
type InvalidFlagError ¶
type InvalidFlagError struct { Flag string // contains filtered or unexported fields }
func (*InvalidFlagError) Error ¶
func (e *InvalidFlagError) Error() string
func (*InvalidFlagError) GetSuggestions ¶
func (e *InvalidFlagError) GetSuggestions() []string
type MatchFunction ¶
type Options ¶
type Options struct { InsCost int DelCost int SubCost int Matches MatchFunction }
var DefaultOptions Options = Options{ InsCost: 1, DelCost: 1, SubCost: 2, Matches: func(sourceCharacter rune, targetCharacter rune) bool { return sourceCharacter == targetCharacter }, }
DefaultOptions is the default options: insertion cost is 1, deletion cost is 1, substitution cost is 2, and two runes match iff they are the same.
type Parser ¶
type Parser struct {
// contains filtered or unexported fields
}
func (*Parser) GetRemains ¶
type Suggester ¶
type Suggester struct {
// contains filtered or unexported fields
}
helper class for Suggester
func NewSuggester ¶
func (*Suggester) GetResults ¶
type SuggestibleError ¶
type SuggestibleError interface {
GetSuggestions() []string
}
error with suggestions