Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var CliColor = &gcli.Command{ Name: "color", Desc: "this is a example for cli color usage", Aliases: []string{"clr", "colors"}, Func: colorUsage, Examples: "{$binName} {$cmd} --id 12 -c val ag0 ag1", Config: func(c *gcli.Command) { c.IntOpt(&colorOpts.id, "id", "", 2, "the id option") c.StrOpt(&colorOpts.c, "c", "", "value", "the config option") c.StrOpt(&colorOpts.dir, "dir", "", "", "the dir option") }, }
CliColor command definition
View Source
var DaemonRun = &gcli.Command{ Name: "bgrun", Desc: "an example for background run program", Func: handleDaemonRun, Aliases: []string{"bgr"}, Config: func(c *gcli.Command) { c.BoolOpt(&bgrOpts.deamon, "daemon", "d", false, "want background run") }, }
View Source
var EmojiDemo = &gcli.Command{ Name: "emoji", Desc: "this is a emoji usage example command", Aliases: []string{"emoj"}, Examples: ` An render example {$fullCmd} render ":car: a message text, contains emoji :smile:" An search example {$fullCmd} search smi`, Subs: []*gcli.Command{ { Name: "render", Desc: "render given string, will replace special char to emoji", Aliases: []string{"r"}, Config: func(c *gcli.Command) { c.AddArg("msg", "The message string for render", true) }, Func: func(c *gcli.Command, args []string) error { fmt.Println(emoji.Render(c.Arg("msg").String())) return nil }, }, { Name: "search", Desc: "search emojis by given keywords", Aliases: []string{"s"}, Config: func(c *gcli.Command) { c.AddArg("keyword", "The keyword string for search", true) }, Func: func(c *gcli.Command, args []string) error { kw := c.Arg("keyword").String() return searchEmoji(kw) }, }, }, }
View Source
var EnvInfo = &gcli.Command{ Name: "env", Desc: "collect project info by git info", Aliases: []string{"env-info", "ei"}, Config: func(c *gcli.Command) { c.IntOpt(&eiOpts.id, "id", "", 0, "the id option") c.StrOpt(&eiOpts.c, "c", "", "", "the config option") c.StrOpt(&eiOpts.dir, "dir", "d", "", "the dir option") }, Func: func(c *gcli.Command, _ []string) error { eAble, _ := os.Executable() data := map[string]any{ "os": runtime.GOOS, "binName": c.BinName(), "workDir": c.WorkDir(), "rawArgs": os.Args, "execAble": eAble, "env": os.Environ(), } show.JSON(&data) return nil }, }
EnvInfo command
View Source
var Example = &gcli.Command{ Func: exampleExecute, Name: "example", Aliases: []string{"module-exp", "exp", "ex"}, Desc: "this is command description message", Examples: ` {$binName} {$cmd} --id 12 -c val ag0 ag1 <cyan>{$fullCmd} --names tom --names john -n c</> test use special option `, Config: func(c *gcli.Command) { c.IntOpt(&exampleOpts.id, "id", "", 2, "the id option") c.BoolOpt(&exampleOpts.showErr, "err", "e", false, "display error example") c.StrOpt(&exampleOpts.c, "config", "c", "value", "the config option") c.StrOpt(&exampleOpts.dir, "dir", "d", "", "the `DIRECTORY` option") c.StrOpt(&exampleOpts.opt, "opt", "o", "", "the option message") c.VarOpt(&exampleOpts.names, "names", "n", "the option message") c.AddArg("arg0", "the first argument, is required", true) c.AddArg("arg1", "the second argument, is required", true) c.AddArg("arg2", "the optional argument, is optional") c.AddArg("arrArg", "the array argument, is array", false, true) }, }
Example command definition
View Source
var GitCmd = &gcli.Command{ Name: "git", Desc: "git usage example", Subs: []*gcli.Command{ GitInfo, GitPullMulti, GitRemote, }, }
View Source
var GitInfo = &gcli.Command{ Name: "info", Desc: "collect project latest commit info by git log command", Config: func(c *gcli.Command) { c.IntOpt(&gitOpts.id, "id", "", 0, "the id option") c.StrOpt(&gitOpts.c, "c", "", "", "the config option") c.StrOpt(&gitOpts.dir, "dir", "d", "", "the dir option") }, Func: gitExecute, }
GitInfo git info command
View Source
var GitPullMulti = &gcli.Command{ Name: "pull", Desc: "use git pull for update multi project", Aliases: []string{"pul"}, Config: func(c *gcli.Command) { c.AddArg( "basePath", "the base operate dir path. default is current dir", true, ). WithValue("./"). WithValidator(func(v any) (i any, e error) { if !fsutil.IsDir(v.(string)) { return nil, fmt.Errorf("the base path must be an exist dir") } return v, nil }) c.AddArg( "dirNames", "the operate dir names in the base path, allow multi by spaces", false, true, ) }, Examples: ` {$fullCmd} /my/workspace project1 project2 `, Func: func(c *gcli.Command, _ []string) (err error) { var ret string basePath := c.Arg("basePath").String() dirNames := c.Arg("dirNames").Strings() if len(dirNames) == 0 { dirNames = getSubDirs(basePath) if len(dirNames) == 0 { return fmt.Errorf("no valid subdirs in the base path: %s", basePath) } } color.Green.Println("The operate bash path:", basePath) fmt.Println("- want updated project dir names:", dirNames) for _, name := range dirNames { ret, err = execCmd("git pull", path.Join(basePath, name)) if err != nil { return } color.Info.Println("RESULT:") fmt.Println(ret) } color.Cyan.Println("Update Complete :)") return }, }
GitPullMulti use git pull for update multi project
View Source
var GitRemote = &gcli.Command{ Name: "remote", Desc: "remote command of the git", Aliases: []string{"rmt"}, Config: func(c *gcli.Command) { c.BoolOpt(&gitRmtOPts.v, "v", "", false, "option for git remote") }, Func: func(c *gcli.Command, args []string) error { dump.P(c.Path()) return nil }, Subs: []*gcli.Command{ { Name: "set-url", Desc: "set-url command of git remote", Aliases: []string{"su"}, Config: func(c *gcli.Command) { c.AddArg("name", "the remote name", true) c.AddArg("address", "the remote address", true) }, Func: func(c *gcli.Command, args []string) error { dump.P(c.Path()) return nil }, }, }, }
GitRemote remote command of the git.
View Source
var InteractCollectCmd = &gcli.Command{ Name: "collect", Desc: "collect multi input params at once", Func: func(c *gcli.Command, args []string) error { vc := interact.NewCollector() err := vc.AddParams( cparam.NewStringParam("title", "title name").Config(func(p *cparam.StringParam) { p.ValidFn = func(val string) error { return goutil.OrError(strutil.IsBlank(val), errorx.Raw("title is required")) } }), cparam.NewChoiceParam("projects", "select projects"). WithChoices([]string{"user", "order", "goods"}), ) if err != nil { return err } if err = vc.Run(); err != nil { return err } c.Println("Result:") dump.P(vc.Results()) return nil }, }
InteractCollectCmd instance.
View Source
var InteractDemo = &gcli.Command{ Name: "interact", Func: interactDemo, Desc: "the command will show some interactive methods", Subs: []*gcli.Command{InteractCollectCmd}, Aliases: []string{"itt"}, Config: func(c *gcli.Command) { c.AddArg("name", "want running interact method name", true) }, Examples: `{$fullCmd} confirm {$fullCmd} select `, Help: ` Supported interactive methods: read read user input text answerIsYes check user answer is Yes confirm confirm message select select one from given options password read user hidden input multiSelect select multi from given options `, }
InteractDemo command
View Source
var ProgressDemo = &gcli.Command{ Name: "prog", Desc: "there are some progress bar run demos", Aliases: []string{"prg-demo", "progress"}, Config: func(c *gcli.Command) { c.IntOpt(&pdOpts.maxSteps, "max-step", "", 100, "setting the max step value") c.BoolOpt(&pdOpts.overwrite, "overwrite", "o", true, "setting overwrite progress bar line") c.BoolVar(&pdOpts.random, &gcli.CliOpt{Name: "random", Desc: "use random style for progress bar"}) c.BindArg(&gcli.CliArg{ Name: "name", Desc: "progress bar type name. allow: bar,txt,dtxt,loading,roundTrip", Required: true, }) }, Examples: `Text progress bar: {$fullCmd} txt Image progress bar: {$fullCmd} bar`, Func: func(c *gcli.Command, args []string) error { name := c.Arg("name").String() max := pdOpts.maxSteps color.Infoln("Progress Demo:") switch name { case "bar": showProgressBar(max) case "bars", "all-bar": showAllProgressBar(max) case "dt", "dtxt", "dynamicText": dynamicTextBar(max) case "txt", "text": txtProgressBar(max) case "spr", "load", "loading", "spinner": runLoadingBar(max) case "rt", "roundTrip": runRoundTripBar(max) default: return c.NewErrf("the progress bar type name only allow: bar,txt,dtxt,loading,roundTrip. input is: %s", name) } return nil }, }
View Source
var QuestionDemo = &gcli.Command{ Name: "ask-demo", Aliases: []string{"qd"}, Desc: "demo B7: declarative interactive collect by CliOpt.Question", Examples: ` <cyan>{$fullCmd}</> # 不带 --token 运行 -> 自动提问收集输入 <cyan>{$fullCmd} --token abc123</> # 已提供值则不提问`, Config: func(c *gcli.Command) { c.StrOpt2(&questionOpts.token, "token", "the access token (will ask if empty)", gflag.WithQuestion("Please input your access token: ")) c.StrOpt(&questionOpts.name, "name", "n", "guest", "the user name") }, Func: func(c *gcli.Command, args []string) error { color.Infoln("hello, in ask-demo command (B7 demo)") color.Cyanln("Collected options:") dump.V(questionOpts) return nil }, }
QuestionDemo 演示 B7:CliOpt.Question 声明式交互收集
View Source
var ReorderDemo = &gcli.Command{ Name: "reorder-args", Aliases: []string{"rda"}, Desc: "demo: auto-reorder mixed input to canonical --options... arguments", Examples: ` <cyan>{$fullCmd} src dst --name tom -f</> # options 写在 arguments 之后, 仍被解析 <cyan>{$fullCmd} src --name tom dst --age 18</> # options/arguments 交错混写 <cyan>{$fullCmd} --name tom -f src dst</> # 标准顺序, 结果与上面一致`, Config: func(c *gcli.Command) { c.StrOpt(&reorderOpts.name, "name", "n", "", "the name option (take value)") c.IntOpt(&reorderOpts.age, "age", "a", 0, "the age option (take value)") c.BoolOpt(&reorderOpts.force, "force", "f", false, "the force option (bool)") c.AddArg("src", "the source argument", true) c.AddArg("dst", "the destination argument") }, Func: func(c *gcli.Command, _ []string) error { color.Infoln("hello, in reorder-args command (auto-reorder demo)") color.Cyanln("Parsed options:") dump.V(reorderOpts) color.Cyanln("Parsed arguments:") dump.V(map[string]string{ "src": c.Arg("src").String(), "dst": c.Arg("dst").String(), }) return nil }, }
ReorderDemo 演示 args 自动重排:写在 arguments 之后的 options 仍能被正确解析。
该特性由 gflag Config.DisableReorderArgs 控制(默认开启), 可用 c.ParserCfg().DisableReorderArgs = true 关闭以恢复严格顺序。
View Source
var ShortMergeDemo = &gcli.Command{ Name: "short-merge", Aliases: []string{"smd"}, Desc: "demo B4+B5: POSIX short option merge by EnhanceShort", Examples: ` <cyan>{$fullCmd} -aux</> # = -a -u -x (三个 bool 短选项合并,仅全 bool 才拆) <cyan>{$fullCmd} -Ostdout</> # = -O stdout (取值短选项紧贴写法,需 level2) <cyan>{$fullCmd} -au -O file.txt</> # 混合写法`, Config: func(c *gcli.Command) { c.ParserCfg().EnhanceShort = gcli.EnhanceShortAttach c.BoolOpt(&shortMergeOpts.all, "all", "a", false, "the all option (bool)") c.BoolOpt(&shortMergeOpts.upload, "upload", "u", false, "the upload option (bool)") c.BoolOpt(&shortMergeOpts.extract, "extract", "x", false, "the extract option (bool)") c.StrOpt(&shortMergeOpts.output, "output", "O", "", "the output option (take value)") }, Func: func(c *gcli.Command, args []string) error { color.Infoln("hello, in short-merge command (B4+B5 demo)") color.Cyanln("Parsed options:") dump.V(shortMergeOpts) return nil }, }
ShortMergeDemo 演示 B4+B5:EnhanceShort POSIX 短选项合并
View Source
var ShowDemo = &gcli.Command{
Name: "show",
Func: runShow,
Desc: "the command will show some data format methods",
}
View Source
var SpinnerDemo = &gcli.Command{ Name: "spinner", Desc: "there are some CLI spinner bar run demos", Aliases: []string{"spr", "spr-demo"}, Config: func(c *gcli.Command) { c.IntOpt(&spOpts.speed, "speed", "s", 100, "setting the spinner running speed") c.IntOpt(&spOpts.themeNum, "theme-num", "t", 0, "setting the theme numbering. allow: 0 - 16") c.AddArg("name", "spinner type name. allow: loading,roundTrip", false, ) }, Examples: `Loading spinner: {$fullCmd} loading roundTrip spinner: {$fullCmd} roundTrip`, Func: func(c *gcli.Command, _ []string) error { name := c.Arg("name").String() switch name { case "", "spinner", "load", "loading": spOpts.runLoadingSpinner() case "rt", "roundTrip": spOpts.runRoundTripSpinner() default: return c.NewErrf("the spinner type name only allow: loading,roundTrip. input is: %s", name) } return nil }, }
View Source
var StructFlagDemo = &gcli.Command{ Name: "struct-flag", Aliases: []string{"sfd"}, Desc: "demo B6: bind options from struct by TagRuleField + anonymous field expand", Examples: ` <cyan>{$fullCmd} --user-name tom --age 22 -e tom@example.com -v</> <cyan>{$fullCmd} -u tom</> # age 用默认值 18,user-name 为必填项`, Config: func(c *gcli.Command) { c.MustFromStruct(structFlagData, gcli.TagRuleField) }, Func: func(c *gcli.Command, args []string) error { color.Infoln("hello, in struct-flag command (B6 demo)") color.Cyanln("Parsed struct options:") dump.V(structFlagData) return nil }, }
StructFlagDemo 演示 B6:TagRuleField 标签规则 + 匿名字段展开
View Source
var StructTypesDemo = &gcli.Command{ Name: "struct-types", Aliases: []string{"stp"}, Desc: "demo D1: bind richer field types from struct (slice/duration/map/enum)", Examples: ` <cyan>{$fullCmd} -n a -n b -p 80 -p 443 --ttl 1h30m -m k1=v1 -m k2=v2 -l go</> <cyan>{$fullCmd} -l ruby</> # enum 校验失败示例(lang 仅允许 go/php/java)`, Config: func(c *gcli.Command) { c.MustFromStruct(structTypesData) }, Func: func(c *gcli.Command, args []string) error { color.Infoln("hello, in struct-types command (D1 demo)") color.Cyanln("Parsed struct options:") dump.V(structTypesData) color.Cyanf("TTL(readable): %s\n", structTypesData.TTL.String()) return nil }, }
StructTypesDemo 演示 D1:结构体标签支持 slice / Duration / map / enum
Functions ¶
This section is empty.
Types ¶
type GitInfoData ¶
Click to show internal directories.
Click to hide internal directories.