Documentation ¶
Overview ¶
Package bin is sample command application lib, provides functional and classic style Apis.
Index ¶
- Constants
- func CleanoutString(ss string) string
- func Cmd2StringMap(c string) string
- func FormatKv(kv interface{}, params ...string) string
- func FormatKvSort(kv interface{}, params ...string) string
- func FormatQue(que interface{}, prefs ...string) string
- func FormatTable(table interface{}, args ...interface{}) string
- func GetHelpEmbed(content string, args ...string) string
- func ParseValueByStr(ss string) interface{}
- type App
- type AppCmd
- type AppOption
- type AppOptionGroup
- type CLI
- func (cli *CLI) CallCmd(cmd string)
- func (cli *CLI) CmdExist(cmds ...string) bool
- func (cli *CLI) Describe(desc string) bool
- func (cli *CLI) GetCmdList() []string
- func (cli *CLI) GetDescribe(cmd string) string
- func (cli *CLI) GetInjection(key string) interface{}
- func (cli *CLI) Inject(key string, value interface{}) *CLI
- func (cli *CLI) RegisterAny(action interface{}) *CLI
- func (cli *CLI) RegisterApp(ap interface{}, cmds ...string) *CLI
- func (cli *CLI) RegisterApps(aps map[string]interface{}) *CLI
- func (cli *CLI) RegisterCommand(c Cmd) *CLI
- func (cli *CLI) RegisterEmpty(action interface{}) *CLI
- func (cli *CLI) RegisterFunc(todo func(*CliCmd), cmds ...string) *CLI
- func (cli *CLI) RegisterUnmatched(callback func(string, *CliCmd)) *CLI
- func (cli *CLI) Run(args ...string)
- func (cli *CLI) RunDefault(args ...string)
- type CMD
- type CliApp
- type CliAppCompleteInterface
- type CliAppInterface
- type CliCmd
- func (app *CliCmd) AppendData(vMap map[string]interface{}) *CliCmd
- func (app *CliCmd) Arg(keys ...string) interface{}
- func (app *CliCmd) ArgDefault(key string, def interface{}) interface{}
- func (app *CliCmd) ArgInt(keys ...string) int
- func (app *CliCmd) ArgRaw(keys ...string) string
- func (app *CliCmd) ArgRawDefault(key, def string) string
- func (app *CliCmd) ArgRawLine() string
- func (app *CliCmd) ArgStringSlice(keys ...string) []string
- func (app *CliCmd) CallCmd(cmd string)
- func (app *CliCmd) CheckMustKey(keys ...string) bool
- func (app *CliCmd) CheckSetting(sets ...string) bool
- func (app *CliCmd) CmdType() int
- func (app *CliCmd) CommandAlias(key string, alias ...string) *CliCmd
- func (app *CliCmd) CommandAliasAll(alias map[string][]string) *CliCmd
- func (app *CliCmd) Context() CLI
- func (app *CliCmd) Cwd() string
- func (app *CliCmd) Next(keys ...string) string
- func (app *CliCmd) QueueNext(key string) string
- func (app *CliCmd) SubCommandAlias(key string, alias ...string) *CliCmd
- func (app *CliCmd) SubCommandAliasAll(alias map[string][]string) *CliCmd
- type Cmd
- type CmdOptions
- type Option
Examples ¶
Constants ¶
const ( AppMethodInit = "Init" AppMethodRun = "Run" AppMethodNoSubC = "SubCommandUnfind" AppMethodHelp = "Help" FuncRegisterEmpty = "_inner_empty_func" )
const ( CmdApp initIota = iota CmdFunc )
the Cmd of type
const ( AppTagName = "cmd" OptValidationRequire = "required" // 必须 OptValidationOptional = "optional" // 可选 )
const OptionTagName = "arg"
Variables ¶
This section is empty.
Functions ¶
func CleanoutString ¶ added in v1.1.0
CleanoutString clear out the raw input string like:
`"string"` => `string` `"'string'"` => `'string'` `'string'` => `string` `'"string"'` => `"string"`
func Cmd2StringMap ¶ added in v1.1.0
Cmd2StringMap command string turn to map string, for standard go method name. like:
`get-videos` -> `GetVideos` `get_videos` -> `GetVideos`
func FormatKv ¶ added in v0.5.2
FormatKv The `k-v` data format to beautiful str. FormatKv(kv map[string]interface{}, pref string) provide pref param form FormatKv. FormatKv(kv map[string]interface{}, pref string, md string) provide pref and middle param form FormatK. the `Kv` support map/struct, but not ptr(pointer)
func FormatKvSort ¶ added in v1.1.0
FormatKvSort The `k-v` data format to beautiful str. FormatKvSort(kv map[string]interface{}, pref string) provide pref param form FormatKv. FormatKvSort(kv map[string]interface{}, pref string, md string) provide pref and middle param form FormatK.
func FormatQue ¶ added in v0.5.0
FormatQue format the string array, using for cli output pretty. where prefs is empty default use the array index
func FormatTable ¶ added in v0.5.0
func FormatTable(table interface{}, args ...interface{}) string
FormatTable Table format output by slice:
(table, bool) if is use the idx, table is 2 dimensional array.
Bug(FormatQue): chinese text cannot alignment
func GetHelpEmbed ¶ added in v1.1.0
GetHelpEmbed GetHelpEmbed(content string, lang string)
func ParseValueByStr ¶ added in v1.1.0
func ParseValueByStr(ss string) interface{}
ParseValueByStr parse the command value to really type by format.
Types ¶
type App ¶
type AppOption ¶ added in v1.1.2
type AppOption struct { Validation string // validation item like: required, optional(default) Name string Alias []string IsSet bool // option is set // contains filtered or unexported fields }
AppOption parse struct tag syntax `cmd: "tagName;"` data valid: required, optional(default) name: "name,n,N" example help: `help,h; optional`
func ParseOptionTag ¶ added in v1.1.2
type AppOptionGroup ¶ added in v1.1.2
type AppOptionGroup struct {
// contains filtered or unexported fields
}
AppOptionGroup the group of many of AppOption.
func ParseOptionGroup ¶ added in v1.1.2
func ParseOptionGroup(v interface{}) *AppOptionGroup
func (*AppOptionGroup) Option ¶ added in v1.1.2
func (c *AppOptionGroup) Option(name string) *AppOption
Option get option by name
func (*AppOptionGroup) OptionSearchAlias ¶ added in v1.1.2
func (c *AppOptionGroup) OptionSearchAlias(name string) *AppOption
func (*AppOptionGroup) ParseEach ¶ added in v1.1.2
func (c *AppOptionGroup) ParseEach(v interface{}, each func(*AppOption)) error
func (*AppOptionGroup) Unmarshal ¶ added in v1.1.2
func (c *AppOptionGroup) Unmarshal(cmd *CliCmd, v interface{}) error
type CLI ¶ added in v1.1.0
type CLI struct { //external fields UnLoadDataSyntax bool //not support load data syntax, like json/url. UnLoadScriptSyntax bool // disable allow load script like shell syntax. ScriptOption string // default: --script,-s ScriptFileOption string // default: --file,-f // contains filtered or unexported fields }
CLI the cli application
func NewCLI ¶ added in v1.1.0
func NewCLI() *CLI
NewCLI the construct of `CLI`
Example (Func) ¶
cli := NewCLI() cli.RegisterAny(func() { fmt.Println("Hello world, cli.") }) cli.Run()
Output: Hello world, cli.
Example (Repl) ¶
var input = bufio.NewScanner(os.Stdin) prefShow := func() { fmt.Print("$ uymas> ") } cli := NewCLI() cli.RegisterAny(new(defaultAppHelloWorld)) prefShow() for input.Scan() { text := input.Text() text = strings.TrimSpace(text) switch text { case "exit": os.Exit(0) default: // to run struct command. var cmdsList = parser.NewParser(text) for _, cmdArgs := range cmdsList { cli.Run(cmdArgs...) } } fmt.Println() prefShow() }
Output:
Example (Struct) ¶
cli := NewCLI() // add the new struct instance. // type defaultAppHelloWorld struct { // bin.CliApp // } // // func (w *defaultAppHelloWorld) DefaultIndex() { // fmt.Println("Hello world, struct cli.") // } cli.RegisterAny(new(defaultAppHelloWorld)) cli.Run()
Output: Hello world, struct cli.
func (*CLI) GetCmdList ¶ added in v1.1.0
GetCmdList get the list cmd of application
func (*CLI) GetDescribe ¶ added in v1.1.0
GetDescribe support the `cmd, alias` param.
func (*CLI) GetInjection ¶ added in v1.1.0
GetInjection get Injection data
func (*CLI) RegisterAny ¶ added in v1.1.0
RegisterAny when command input not handler will callback the register, the format like:
- function `func(cmd string, cc *CliCmd)`/`func(cmd string)`/`func(cc *CliCmd)`/CliApp/Base Struct
func (*CLI) RegisterApp ¶ added in v1.1.0
RegisterApp register the struct app, the format same as RegisterFunc. cmds any be `cmd string` or `cmd, alias string`
func (*CLI) RegisterApps ¶ added in v1.1.0
RegisterApps Register many apps once.
func (*CLI) RegisterCommand ¶ added in v1.1.0
RegisterCommand register by command struct data
func (*CLI) RegisterEmpty ¶ added in v1.1.0
RegisterEmpty when the cmd is empty then callback the function, action only be
- function `func(cc *CliCmd)`/`func()` or struct.
func (*CLI) RegisterFunc ¶ added in v1.1.0
RegisterFunc register functional command, the format like
`RegisterFunc(todo func(cc *CliCmd), cmd string)` or `RegisterFunc(todo func(), cmd, alias string)`
func (*CLI) RegisterUnmatched ¶ added in v1.1.1
RegisterUnmatched old method `Unfind` of alias compatibly use `RegisterAny`
func (*CLI) RunDefault ¶ added in v1.1.1
RunDefault Run cli app using user defined `args` when os.args is empty, it'll be useful to debug or default define.
type CMD ¶ added in v1.1.0
type CMD struct { Title string //the title of `CMD` Command string Alias []string //the alias of `CMD`, support many alias command //help information Describe string HelpMessage string //help message HelpCall func(*CliCmd) //help message by `Func` //the action by the Func Todo func(*CliCmd) //cli callback TodoApp interface{} //bind from App struct }
CMD the struct to be a map for cli application @todo need to do
type CliAppCompleteInterface ¶ added in v1.1.0
type CliAppCompleteInterface interface { CliAppInterface DefaultHelp() DefaultIndex() DefaultUnmatched() }
CliAppCompleteInterface the complete CliApp show hand method should have a field name like `Cc *CliCmd` the method call order by `construct > command > help > index > unmatched`
type CliAppInterface ¶ added in v1.1.0
type CliAppInterface interface {
Construct()
}
CliAppInterface the interface of CliApp
type CliCmd ¶ added in v1.1.0
type CliCmd struct { Data map[string]interface{} // the data from the `DataRaw` by parse for type DataRaw map[string]string // the cli application apply the data Command string // the current command SubCommand string // the sub command Setting []string // the setting of command Raw []string // the raw args // contains filtered or unexported fields }
CliCmd the command of the cli application.
func NewCliCmd ¶ added in v1.1.0
NewCliCmd the construct of `CliCmd`, args default set os.Args if no function arguments
func NewCliCmdByString ¶ added in v1.1.0
NewCliCmdByString construction of `CliCmd` by string
@todo notice: `--test-string="Joshua 存在空格的字符串 Conero"` 解析失败
func (*CliCmd) AppendData ¶ added in v1.1.0
AppendData append the Data
func (*CliCmd) ArgDefault ¶ added in v1.1.0
ArgDefault can default value to get the arg
func (*CliCmd) ArgRawDefault ¶ added in v1.1.0
ArgRawDefault get raw arg has default
func (*CliCmd) ArgRawLine ¶ added in v1.1.0
ArgRawLine get the raw line input.
func (*CliCmd) ArgStringSlice ¶ added in v1.1.0
ArgStringSlice get string-slice param args
func (*CliCmd) CheckMustKey ¶ added in v1.1.0
CheckMustKey check the data key must in the sets and support multi
func (*CliCmd) CheckSetting ¶ added in v1.1.0
CheckSetting to checkout if the set exist in `CliCmd` sets and support multi.
func (*CliCmd) CommandAlias ¶ added in v1.1.0
CommandAlias Tip: in the future will merge method like CommandAlias And CommandAliasAll, chose one from twos.
func (*CliCmd) CommandAliasAll ¶ added in v1.1.0
func (*CliCmd) Context ¶ added in v1.1.0
Context get the context of `CLI`, in case `AppCmd` not `FunctionCmd`
func (*CliCmd) SubCommandAlias ¶ added in v1.1.0
type Cmd ¶ added in v1.1.0
type Cmd struct { Command string Alias interface{} //string, []string. the alias of the command Describe string //describe the command Handler func(cc *CliCmd) //when command call then handler the request Options map[string]CmdOptions // the command option }
Cmd define the struct command
type CmdOptions ¶ added in v1.1.0
Directories ¶
Path | Synopsis |
---|---|
Package butil bin util package will not run the init(), but bin will
|
Package butil bin util package will not run the init(), but bin will |
syntax
Package syntax is bin language syntax extend, like digital computing.
|
Package syntax is bin language syntax extend, like digital computing. |