Documentation ¶
Overview ¶
Package bin is sample command application lib, provides functional and classic style Apis.
Index ¶
- Constants
- Variables
- func CleanoutString(ss string) string
- func Cmd2StringMap(c string) string
- func FormatKv(kv any, params ...string) string
- func FormatKvSort(kv any, params ...string) string
- func FormatQue(que any, prefs ...string) string
- func FormatTable(table any, args ...any) string
- func GetHelpEmbed(content string, args ...string) string
- func ParseValueByStr(ss string) any
- func PlgCmdList() []string
- func ReplCommand(name string, cli *CLI)
- type App
- type AppCmd
- type AppOption
- type AppOptionGroup
- type Arg
- func (app *Arg) AppendData(vMap map[string]any) *Arg
- func (app *Arg) Arg(keys ...string) any
- func (app *Arg) ArgBool(keys ...string) bool
- func (app *Arg) ArgDefault(key string, def any) any
- func (app *Arg) ArgFloat64(keys ...string) float64
- func (app *Arg) ArgInt(keys ...string) int
- func (app *Arg) ArgIntSlice(keys ...string) []int
- func (app *Arg) ArgRaw(keys ...string) string
- func (app *Arg) ArgRawDefault(key, def string) string
- func (app *Arg) ArgRawLine() string
- func (app *Arg) ArgStringSlice(keys ...string) []string
- func (app *Arg) CallCmd(cmd string)
- func (app *Arg) CheckMustKey(keys ...string) bool
- func (app *Arg) CheckSetting(sets ...string) bool
- func (app *Arg) CmdType() int
- func (app *Arg) CommandAlias(key string, alias ...string) *Arg
- func (app *Arg) CommandAliasAll(alias map[string][]string) *Arg
- func (app *Arg) Config() ArgConfig
- func (app *Arg) Context() CLI
- func (app *Arg) Cwd() string
- func (app *Arg) DefF64(def float64, args ...string) float64
- func (app *Arg) DefInt(def int, args ...string) int
- func (app *Arg) DefString(def string, args ...string) string
- func (app *Arg) IsPlgCmd() bool
- func (app *Arg) Next(keys ...string) string
- func (app *Arg) NextList(keys ...string) []string
- func (app *Arg) ParseOption(v any) *Option
- func (app *Arg) QueueNext(key string) string
- func (app *Arg) SubCommandAlias(key string, alias ...string) *Arg
- func (app *Arg) SubCommandAliasAll(alias map[string][]string) *Arg
- type ArgConfig
- 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) any
- func (cli *CLI) Inject(key string, value any) *CLI
- func (cli *CLI) NoVerify(args ...bool) *CLI
- func (cli *CLI) RegisterAny(action any) *CLI
- func (cli *CLI) RegisterApp(ap any, cmds ...string) *CLI
- func (cli *CLI) RegisterApps(aps map[string]any) *CLI
- func (cli *CLI) RegisterCommand(c Cmd) *CLI
- func (cli *CLI) RegisterEmpty(action any) *CLI
- func (cli *CLI) RegisterEnd(action any) *CLI
- func (cli *CLI) RegisterFunc(todo func(*Arg), cmds ...string) *CLI
- func (cli *CLI) RegisterUnmatched(callback func(string, *Arg)) *CLI
- func (cli *CLI) Run(args ...string)
- func (cli *CLI) RunDefault(args ...string)
- func (cli *CLI) RunWith(cfg ArgConfig, args ...string)
- type CMD
- type CliApp
- type CliAppCompleteInterface
- type CliAppInterface
- type Cmd
- type CmdOptions
- type Option
- type PlgCProfile
- type PluginCommand
- type Repl
Examples ¶
Constants ¶
const ( CmdApp int = iota CmdFunc )
the Cmd of type
const ( AppTagName = "cmd" OptValidationRequire = "required" // 必须 OptValidationOptional = "optional" // 可选 )
const ( OptionTagName = "arg" OptionTagDef = "argDef" )
const ( PlgCmdGetTitle = "plg-cmd-title" PlgCmdGetProfile = "plg-cmd--profile" )
const ( ReplContinue = iota ReplBreak ReplExit )
Variables ¶
var DefaultArgConfig = ArgConfig{ LongOption: true, EqualColon: false, }
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
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
ParseValueByStr parse the command value to really type by format.
func PlgCmdList ¶ added in v1.3.0
func PlgCmdList() []string
PlgCmdList Get Plugin Sub-Command list, support windows/linux/darwin.
func ReplCommand ¶ added in v1.2.0
ReplCommand create a repl cli sub command
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 any) *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
type Arg ¶ added in v1.2.0
type Arg struct { Data map[string]any // 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 }
Arg the command of the cli application.
func NewArgWith ¶ added in v1.4.2
NewArgWith Args is instantiated in tape configuration mode
func NewCliCmd ¶ added in v1.1.0
NewCliCmd the construct of `Arg`, args default set os.Args if no function arguments
func NewCliCmdByString ¶ added in v1.1.0
NewCliCmdByString construction of `Arg` by string
func (*Arg) AppendData ¶ added in v1.2.0
AppendData append the Data
func (*Arg) ArgDefault ¶ added in v1.2.0
ArgDefault can default value to get the arg
func (*Arg) ArgFloat64 ¶ added in v1.2.0
ArgFloat64 get args data identified as float64
func (*Arg) ArgIntSlice ¶ added in v1.4.0
func (*Arg) ArgRawDefault ¶ added in v1.2.0
ArgRawDefault get raw arg has default
func (*Arg) ArgRawLine ¶ added in v1.2.0
ArgRawLine get the raw line input.
func (*Arg) ArgStringSlice ¶ added in v1.2.0
ArgStringSlice get string-slice param args
func (*Arg) CheckMustKey ¶ added in v1.2.0
CheckMustKey check the data key must in the sets and support multi
func (*Arg) CheckSetting ¶ added in v1.2.0
CheckSetting checkout if the set exist in `Arg` sets and support multi.
func (*Arg) CommandAlias ¶ added in v1.2.0
CommandAlias Tip: in the future will merge method like CommandAlias And CommandAliasAll, chose one from twos.
func (*Arg) CommandAliasAll ¶ added in v1.2.0
func (*Arg) Context ¶ added in v1.2.0
Context get the context of `CLI`, in case `AppCmd` not `FunctionCmd`
func (*Arg) DefString ¶ added in v1.4.0
DefString @todo In the near future, generics will be used instead of template functions
func (*Arg) ParseOption ¶ added in v1.4.0
ParseOption parse the option object using parameters
func (*Arg) SubCommandAlias ¶ added in v1.2.0
type ArgConfig ¶ added in v1.4.2
type ArgConfig struct { // If it's support `--long` that is true, otherwise as `-long`. // // And `-long` same as `-l -o -n -g`, otherwise `--long`. LongOption bool // support `:` as equity EqualColon bool }
ArgConfig the config of command
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 // DisPlgCmdDetect Turn off Plugin Command detection DisPlgCmdDetect bool // 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 *Arg)`/`func(cmd string)`/`func(cc *Arg)`/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 *Arg)`/`func()` or struct.
func (*CLI) RegisterEnd ¶ added in v1.3.0
RegisterEnd when the cmd is empty then callback the function, action only be
- function `func(cc *Arg)`/`func()` or struct.
func (*CLI) RegisterFunc ¶ added in v1.1.0
RegisterFunc register functional command, the format like
`RegisterFunc(todo func(cc *Arg), 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(*Arg) //help message by `Func` //the action by the Func Todo func(*Arg) //cli callback TodoApp any //bind from App struct }
CMD the struct to be a map for cli application @todo need to do
type CliApp ¶ added in v1.1.0
type CliApp struct {
Cc *Arg
}
CliApp the cli app.
func (*CliApp) Construct ¶ added in v1.2.0
func (c *CliApp) Construct()
Construct implement method for `CliAppCompleteInterface` interface.
func (*CliApp) DefaultEnd ¶ added in v1.3.0
func (c *CliApp) DefaultEnd()
func (*CliApp) DefaultHelp ¶ added in v1.2.0
func (c *CliApp) DefaultHelp()
func (*CliApp) DefaultIndex ¶ added in v1.2.0
func (c *CliApp) DefaultIndex()
func (*CliApp) DefaultUnmatched ¶ added in v1.2.0
func (c *CliApp) DefaultUnmatched()
type CliAppCompleteInterface ¶ added in v1.1.0
type CliAppCompleteInterface interface { CliAppInterface DefaultHelp() DefaultIndex() DefaultUnmatched() DefaultEnd() }
CliAppCompleteInterface the complete CliApp show hand method should have a field name like `Cc` *Arg 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 Cmd ¶ added in v1.1.0
type Cmd struct { Command string Alias any //string, []string. the alias of the command Describe string //describe the command Handler func(cc *Arg) //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
type Option ¶ added in v1.1.0
type Option struct {
// contains filtered or unexported fields
}
Option the command of options parse.
func (*Option) CheckAllow ¶ added in v1.4.0
func (*Option) ExcludeReg ¶ added in v1.4.0
ExcludeReg use regular expressions to exclude specified options during validation
type PlgCProfile ¶ added in v1.3.0
type PlgCProfile struct { Name string `json:"name"` Description string `json:"description"` ExecName string `json:"execName,omitempty"` }
type PluginCommand ¶ added in v1.3.0
PluginCommand Hot swap command (sub command)
func NewPluginCommand ¶ added in v1.3.0
func NewPluginCommand() *PluginCommand
Source Files ¶
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 |
Package color Experimental addition of command line text color characters.
|
Package color Experimental addition of command line text color characters. |
Package doc command line help information, [Experimental]
|
Package doc command line help information, [Experimental] |
syntax
Package syntax is bin language syntax extend, like digital computing.
|
Package syntax is bin language syntax extend, like digital computing. |
Package tag reflect struct tag for bin
|
Package tag reflect struct tag for bin |