同步操作将从 gookit/gcli 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
一个Golang下的简单易用的命令行应用,工具库。包含运行命令,颜色风格,数据展示,进度显示,交互方法等
--long
,支持添加短选项 -s
required
,可选,数组isArray
三种设定
info,error,success,danger
等多种风格,可直接使用ReadLine
, Confirm
, Select
, MultiSelect
等Txt
, Bar
, Loading
, RoundTrip
, DynamicText
等zsh
,bash
下的命令补全脚本文件如下,引入当前包就可以快速的编写cli应用了
import "gopkg.in/gookit/gcli.v2" // 推荐
// or
import "github.com/gookit/gcli"
package main
import (
"runtime"
"github.com/gookit/gcli"
"github.com/gookit/gcli/_examples/cmd"
)
// 测试运行: go run ./_examples/cliapp.go && ./cliapp
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
app := gcli.NewApp()
app.Version = "1.0.3"
app.Description = "this is my cli application"
// app.SetVerbose(gcli.VerbDebug)
app.Add(cmd.ExampleCommand())
app.Add(&gcli.Command{
Name: "demo",
// allow color tag and {$cmd} will be replace to 'demo'
UseFor: "this is a description <info>message</> for command",
Aliases: []string{"dm"},
Func: func (cmd *gcli.Command, args []string) error {
gcli.Println("hello, in the demo command")
return nil
},
})
// .... add more ...
app.Run()
}
先使用本项目下的 demo 示例代码构建一个小的cli demo应用
% go build ./_examples/cliapp.go
打印我们在创建cli应用时设置的版本信息
% ./cliapp --version
This is my cli application
Version: 1.0.3
使用 ./cliapp
或者 ./cliapp -h
来显示应用的帮助信息,包含所有的可用命令和一些全局选项
示例:
./cliapp
./cliapp -h # can also
./cliapp --help # can also
% ./cliapp example -c some.txt -d ./dir --id 34 -n tom -n john val0 val1 val2 arrVal0 arrVal1 arrVal2
you can see:
by
./cliapp {command} -h
or./cliapp {command} --help
or./cliapp help {command}
import "github.com/gookit/gcli/builtin"
// ...
// 添加内置提供的生成命令
app.Add(builtin.GenAutoCompleteScript())
构建并运行生成命令(生成成功后可以去掉此命令):
% go build ./_examples/cliapp.go && ./cliapp genac -h // 使用帮助
% go build ./_examples/cliapp.go && ./cliapp genac // 开始生成, 你将会看到类似的信息
INFO:
{shell:zsh binName:cliapp output:auto-completion.zsh}
Now, will write content to file auto-completion.zsh
Continue? [yes|no](default yes): y
OK, auto-complete file generate successful
运行后就会在当前目录下生成一个
auto-completion.{zsh|bash}
文件, shell 环境名是自动获取的。当然你可以在运行时手动指定
生成的shell script 文件请参看:
预览效果:
app.Add(&gcli.Command{
Name: "demo",
// allow color tag and {$cmd} will be replace to 'demo'
UseFor: "this is a description <info>message</> for command",
Aliases: []string{"dm"},
Func: func (cmd *gcli.Command, args []string) error {
gcli.Print("hello, in the demo command\n")
return nil
},
})
the source file at: example.go
package cmd
import (
"fmt"
"github.com/gookit/color"
"github.com/gookit/gcli"
)
// options for the command
var exampleOpts = struct {
id int
c string
dir string
opt string
names gcli.Strings
}{}
// ExampleCommand command definition
func ExampleCommand() *gcli.Command {
cmd := &gcli.Command{
Name: "example",
UseFor: "this is a description message",
Aliases: []string{"exp", "ex"}, // 命令别名
Func: exampleExecute,
// {$binName} {$cmd} is help vars. '{$cmd}' will replace to 'example'
Examples: `{$binName} {$cmd} --id 12 -c val ag0 ag1
<cyan>{$fullCmd} --names tom --names john -n c</> test use special option`,
}
// 绑定命令选项信息
cmd.IntOpt(&exampleOpts.id, "id", "", 2, "the id option")
cmd.StrOpt(&exampleOpts.c, "config", "c", "value", "the config option")
// notice `DIRECTORY` will replace to option value type
cmd.StrOpt(&exampleOpts.dir, "dir", "d", "", "the `DIRECTORY` option")
// 支持设置选项短名称
cmd.StrOpt(&exampleOpts.opt, "opt", "o", "", "the option message")
// 支持绑定自定义变量, 但必须实现 flag.Value 接口
cmd.VarOpt(&exampleOpts.names, "names", "n", "the option message")
// 绑定命令参数信息,按参数位置绑定
cmd.AddArg("arg0", "the first argument, is required", true)
cmd.AddArg("arg1", "the second argument, is required", true)
cmd.AddArg("arg2", "the optional argument, is optional")
cmd.AddArg("arrArg", "the array argument, is array", false, true)
return cmd
}
// 命令执行主逻辑代码
// example run:
// go run ./_examples/cliapp.go ex -c some.txt -d ./dir --id 34 -n tom -n john val0 val1 val2 arrVal0 arrVal1 arrVal2
func exampleExecute(c *gcli.Command, args []string) error {
fmt.Print("hello, in example command\n")
magentaln := color.Magenta.Println
magentaln("All options:")
fmt.Printf("%+v\n", exampleOpts)
magentaln("Raw args:")
fmt.Printf("%v\n", args)
magentaln("Get arg by name:")
arr := c.Arg("arrArg")
fmt.Printf("named array arg '%s', value: %v\n", arr.Name, arr.Value)
magentaln("All named args:")
for _, arg := range c.Args() {
fmt.Printf("named arg '%s': %+v\n", arg.Name, *arg)
}
return nil
}
go build ./_examples/cliapp.go && ./cliapp example -h
漂亮的帮助信息就已经自动生成并展示出来了
progress.Bar
通用的进度条25/50 [==============>-------------] 50%
progress.Txt
文本进度条Data handling ... ... 50% (25/50)
progress.LoadBar
加载中progress.Counter
计数progress.RoundTrip
来回滚动的进度条[=== ] -> [ === ] -> [ === ]
progress.DynamicText
动态消息,执行进度到不同的百分比显示不同的消息示例:
package main
import "time"
import "github.com/gookit/gcli/progress"
func main() {
speed := 100
maxSteps := 110
p := progress.Bar(maxSteps)
p.Start()
for i := 0; i < maxSteps; i++ {
time.Sleep(time.Duration(speed) * time.Millisecond)
p.Advance()
}
p.Finish()
}
更多示例和使用请看 progress_demo.go
run demos:
go run ./_examples/cliapp.go prog txt
go run ./_examples/cliapp.go prog bar
go run ./_examples/cliapp.go prog roundTrip
console interactive methods
interact.ReadInput
interact.ReadLine
interact.ReadFirst
interact.Confirm
interact.Select/Choice
interact.MultiSelect/Checkbox
interact.Question/Ask
interact.ReadPassword
示例:
package main
import "fmt"
import "github.com/gookit/gcli/interact"
func main() {
username, _ := interact.ReadLine("Your name?")
password := interact.ReadPassword("Your password?")
ok := interact.Confirm("ensure continue?")
if !ok {
// do something...
}
fmt.Printf("username: %s, password: %s\n", username, password)
}
更多示例和使用请看 interact_demo.go
package main
import (
"github.com/gookit/color"
)
func main() {
// simple usage
color.Cyan.Printf("Simple to use %s\n", "color")
// internal theme/style:
color.Info.Tips("message")
color.Info.Prompt("message")
color.Info.Println("message")
color.Warn.Println("message")
color.Error.Println("message")
// custom color
color.New(color.FgWhite, color.BgBlack).Println("custom color style")
// can also:
color.Style{color.FgCyan, color.OpBold}.Println("custom color style")
// use defined color tag
color.Print("use color tag: <suc>he</><comment>llo</>, <cyan>wel</><red>come</>\n")
// use custom color tag
color.Print("custom color tag: <fg=yellow;bg=black;op=underscore;>hello, welcome</>\n")
// set a style tag
color.Tag("info").Println("info style text")
// prompt message
color.Info.Prompt("prompt style message")
color.Warn.Prompt("prompt style message")
// tips message
color.Info.Tips("tips style message")
color.Warn.Tips("tips style message")
}
// 仅设置前景色
color.FgCyan.Printf("Simple to use %s\n", "color")
// 仅设置背景色
color.BgRed.Printf("Simple to use %s\n", "color")
// 完全自定义 前景色 背景色 选项
style := color.New(color.FgWhite, color.BgBlack, color.OpBold)
style.Println("custom color style")
// can also:
color.Style{color.FgCyan, color.OpBold}.Println("custom color style")
支持在windows
cmd.exe
使用
color.Bold
color.Black
color.White
color.Gray
color.Red
color.Green
color.Yellow
color.Blue
color.Magenta
color.Cyan
color.Bold.Println("bold message")
color.Yellow.Println("yellow message")
支持在windows
cmd.exe
使用
color.Info
color.Note
color.Light
color.Error
color.Danger
color.Notice
color.Success
color.Comment
color.Primary
color.Warning
color.Question
color.Secondary
color.Info.Println("Info message")
color.Success.Println("Success message")
不 支持在windows
cmd.exe
使用,但不影响使用,会自动去除颜色标签
使用颜色标签可以非常方便简单的构建自己需要的任何格式
// 使用内置的 color tag
color.Print("<suc>he</><comment>llo</>, <cyan>wel</><red>come</>")
color.Println("<suc>hello</>")
color.Println("<error>hello</>")
color.Println("<warning>hello</>")
// 自定义颜色属性
color.Print("<fg=yellow;bg=black;op=underscore;>hello, welcome</>\n")
更多关于颜色库的使用请访问 gookit/color
issue9/term
https://github.com/issue9/term
beego/bee
https://github.com/beego/bee
inhere/console
https://github/inhere/php-console
golang.org/x/crypto/ssh/terminal
MIT
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。