Gcli
🖥 Go CLI application, tool library, running CLI commands, support console color, user interaction, progress display, data formatting display, generate bash/zsh completion add more features. Go的命令行应用,工具库,运行CLI命令,支持命令行色彩,用户交互,进度显示,数据格式化显示,生成bash/zsh命令补全脚本
Install / Use
/learn @gookit/GcliREADME
GCli
A simple and easy-to-use command-line application and tool library written in Golang. Including running commands, color styles, data display, progress display, interactive methods, etc.
中文说明
中文说明请看 README.zh-CN
Screenshots

Features
- Rich in functions and easy to use
- Support for adding multiple commands and supporting command aliases
- Support binding command options from structure
- example
flag:"name=int0;shorts=i;required=true;desc=int option message"
- example
- Support for adding multi-level commands, each level of command supports binding its own options
option/flag- support option binding--long, support for adding short options(-s)- POSIX-style short flag combining (
-a -b=-ab) - Support setting
Required, indicating a required option parameter - Support setting
Validator, which can customize the validation input parameters
- POSIX-style short flag combining (
argument- support binding argument to specify name- Support
required, optional,arraysettings - It will be automatically detected and collected when the command is run.
- Support
colorable- supports rich color output. provide by gookit/color- Supports html tab-style color rendering, compatible with Windows
- Built-in
info, error, success, dangerand other styles, can be used directly
interactBuilt-in user interaction methods:ReadLine,Confirm,Select,MultiSelect...progressBuilt-in progress display methods:Txt,Bar,Loading,RoundTrip,DynamicText...- Automatically generate command help information and support color display
- When the command entered is incorrect, a similar command will be prompted(including an alias prompt)
- Supports generation of
zshandbashcommand completion script files - Supports a single command as a stand-alone application
Flag Options:
- Options start with
-or--, and the first character must be a letter - Support long option. eg:
--long--long value - Support short option. eg:
-s -a value - Support define array option
- eg:
--tag php --tag gowill gettag: [php, go]
- eg:
Flag Arguments:
- Support binding named argument
- Support define array argument
GoDoc
Install
go get github.com/gookit/gcli/v3
Quick start
an example for quick start:
package main
import (
"github.com/gookit/gcli/v3"
"github.com/gookit/gcli/v3/_examples/cmd"
)
// for test run: go build ./_examples/cliapp.go && ./cliapp
func main() {
app := gcli.NewApp()
app.Version = "1.0.3"
app.Desc = "this is my cli application"
// app.SetVerbose(gcli.VerbDebug)
app.Add(cmd.Example)
app.Add(&gcli.Command{
Name: "demo",
// allow color tag and {$cmd} will be replace to 'demo'
Desc: "this is a description <info>message</> for {$cmd}",
Subs: []*gcli.Command {
// ... allow add subcommands
},
Aliases: []string{"dm"},
Func: func (cmd *gcli.Command, args []string) error {
gcli.Print("hello, in the demo command\n")
return nil
},
})
// .... add more ...
app.Run(nil)
}
Binding flags
flags binding and manage by builtin gflag.go, allow binding flag options and arguments.
Bind options
gcli support multi method to binding flag options.
Use flag methods
Available methods:
BoolOpt(p *bool, name, shorts string, defValue bool, desc string)
BoolVar(p *bool, meta FlagMeta)
Float64Opt(p *float64, name, shorts string, defValue float64, desc string)
Float64Var(p *float64, meta FlagMeta)
Int64Opt(p *int64, name, shorts string, defValue int64, desc string)
Int64Var(p *int64, meta FlagMeta)
IntOpt(p *int, name, shorts string, defValue int, desc string)
IntVar(p *int, meta FlagMeta)
StrOpt(p *string, name, shorts, defValue, desc string)
StrVar(p *string, meta FlagMeta)
Uint64Opt(p *uint64, name, shorts string, defValue uint64, desc string)
Uint64Var(p *uint64, meta FlagMeta)
UintOpt(p *uint, name, shorts string, defValue uint, desc string)
UintVar(p *uint, meta FlagMeta)
Var(p flag.Value, meta FlagMeta)
VarOpt(p flag.Value, name, shorts, desc string)
Usage examples:
var id int
var b bool
var opt, dir string
var f1 float64
var names gcli.Strings
// bind options
cmd.IntOpt(&id, "id", "", 2, "the id option")
cmd.BoolOpt(&b, "bl", "b", false, "the bool option")
// notice `DIRECTORY` will replace to option value type
cmd.StrOpt(&dir, "dir", "d", "", "the `DIRECTORY` option")
// setting option name and short-option name
cmd.StrOpt(&opt, "opt", "o", "", "the option message")
// setting a special option var, it must implement the flag.Value interface
cmd.VarOpt(&names, "names", "n", "the option message")
Use struct tags
package main
import (
"github.com/gookit/gcli/v3"
)
type userOpts struct {
Int int `flag:"name=int0;shorts=i;required=true;desc=int option message"`
Bol bool `flag:"name=bol;shorts=b;desc=bool option message"`
Str1 string `flag:"name=str1;shorts=o;required=true;desc=str1 message"`
// use ptr
Str2 *string `flag:"name=str2;required=true;desc=str2 message"`
// custom type and implement flag.Value
Verb0 gcli.VerbLevel `flag:"name=verb0;shorts=v0;desc=verb0 message"`
// use ptr
Verb1 *gcli.VerbLevel `flag:"name=verb1;desc=verb1 message"`
}
// run: go run ./_examples/issues/iss157.go
func main() {
astr := "xyz"
verb := gcli.VerbWarn
cmd := gcli.NewCommand("test", "desc")
cmd.Config = func(c *gcli.Command) {
c.MustFromStruct(&userOpts{
Str2: &astr,
Verb1: &verb,
})
}
// disable auto bind global options: verbose,version, progress...
gcli.GOpts().SetDisable()
// direct run
if err := cmd.Run(nil); err != nil {
colorp.Errorln( err)
}
}
Bind arguments
About arguments:
- Required argument cannot be defined after optional argument
- Support binding array argument
- The (array)argument of multiple values can only be defined at the end
Available methods:
Add(arg Argument) *Argument
AddArg(name, desc string, requiredAndArrayed ...bool) *Argument
AddArgByRule(name, rule string) *Argument
AddArgument(arg *Argument) *Argument
BindArg(arg Argument) *Argument
Usage examples:
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)
can also use Arg()/BindArg() add a gcli.Argument object:
cmd.Arg("arg0", gcli.Argument{
Name: "ag0",
Desc: "the first argument, is required",
Require: true,
})
cmd.BindArg("arg2", gcli.Argument{
Name: "ag0",
Desc: "the third argument, is is optional",
})
cmd.BindArg("arrArg", gcli.Argument{
Name: "arrArg",
Desc: "the third argument, is is array",
Arrayed: true,
})
use AddArgByRule:
cmd.AddArgByRule("arg2", "add an arg by string rule;required;23")
New application
app := gcli.NewApp()
app.Version = "1.0.3"
app.Desc = "this is my cli application"
// app.SetVerbose(gcli.VerbDebug)
Add commands
app.Add(cmd.Example)
app.Add(&gcli.Command{
Name: "demo",
// allow color tag and {$cmd} will be replace to 'demo'
Desc: "this is a description <info>message</> for {$cmd}",
Subs: []*gcli.Command {
// level1: sub commands...
{
Name: "remote",
Desc: "remote command for git",
Aliases: []string{"rmt"},
Func: func(c *gcli.Command, args []string) error {
dump.Println(c.Path())
return nil
},
Subs: []*gcli.Command{
// level2: sub commands...
// {}
}
},
// ... allow add subcommands
},
Aliases: []string{"dm"},
Func: func (cmd *gcli.Command, args []string) error {
gcli.Print("hello, in the demo command\n")
return nil
},
})
Run application
Build the example application as demo
$ go build ./_examples/cliapp
Display version
$ ./cliapp --version
# or use -V
$ ./cliapp -V

Display app help
by
./cliappor./cliapp -hor./cliapp --help
Examples:
./cliapp
./cliapp -h # can also
./cliapp --help # can also

Run command
Format:
./cliapp COMMAND [--OPTION VALUE -S VALUE ...] [ARGUMENT0 ARGUMENT1 ...]
./cliapp COMMAND [--OPTION VALUE -S VALUE ...] SUBCOMMAND [--OPTION ...] [ARGUMENT0 ARGUMENT1 ...]
Run example:
$ ./cliapp example -c som
