Documentation ¶
Overview ¶
Package gcli is a simple-to-use command line application and tool library.
Contains: cli app, flags parse, interact, progress, data show tools.
Source code and other details for the project are available at GitHub:
https://github.com/gookit/gcli
Usage please refer examples and see README
Index ¶
- Constants
- Variables
- func CommitID() string
- func Config(fn func(opts *GlobalOpts))
- func Debugf(format string, v ...any)
- func IsDebugMode() bool
- func IsGteVerbose(verb VerbLevel) bool
- func Logf(level VerbLevel, format string, v ...any)
- func NewFlags(nameWithDesc ...string) *gflag.Flags
- func NotExitOnEnd() func(*App)
- func Print(args ...any)
- func Printf(format string, args ...any)
- func Println(args ...any)
- func ResetGOpts()
- func ResetVerbose()
- func SetDebugMode()
- func SetQuietMode()
- func SetStrictMode(strict bool)
- func SetVerbose(verbose VerbLevel)
- func StrictMode() bool
- func Version() string
- type App
- func (app *App) Add(c *Command, more ...*Command)
- func (app *App) AddAliases(name string, aliases ...string)
- func (app *App) AddCommand(c *Command)
- func (b *App) AddError(err error)
- func (app *App) AddHandler(h Handler)
- func (b *App) AddHelpVar(key string, val any)
- func (b *App) AliasesMapping() map[string]string
- func (b *App) BinDir() string
- func (b *App) BinName() string
- func (b *App) ChWorkDir(dir string)
- func (b *App) CmdAliases() *structs.Aliases
- func (b *App) CmdNameMap() map[string]int
- func (b *App) CmdNames() []string
- func (b *App) Command(name string) (c *Command, exist bool)
- func (app *App) CommandName() string
- func (b *App) CommandNames() []string
- func (b *App) Commands() map[string]*Command
- func (app *App) Config(fns ...func(a *App))
- func (app *App) Exec(path string, args []string) error
- func (app *App) Exit(code int)
- func (b *App) FindByPath(path string) *Command
- func (b *App) FindCommand(path string) *Command
- func (app *App) Fire(event string, data map[string]any) bool
- func (app *App) Flags() *Flags
- func (b *App) GetCommand(name string) *Command
- func (b *App) HasCommand(name string) bool
- func (b *App) HasCommands() bool
- func (b *App) HasSubcommands() bool
- func (b *App) IsAlias(alias string) bool
- func (b *App) IsCommand(name string) bool
- func (b *App) Match(names []string) *Command
- func (b *App) MatchByPath(path string) *Command
- func (app *App) On(name string, handler HookFunc)
- func (app *App) Opts() *GlobalOpts
- func (app *App) QuickRun() int
- func (b *App) ResetData()
- func (b *App) ResolveAlias(alias string) string
- func (app *App) Run(args []string) (code int)
- func (app *App) RunArgs(args ...string) int
- func (app *App) RunCmd(name string, args []string) error
- func (app *App) RunLine(argsLine string) int
- func (app *App) SetDefaultCommand(name string)
- func (b *App) SetLogo(logo string, style ...string)
- func (b *App) WorkDir() string
- type AppConfig
- type Argument
- type Arguments
- type Booleans
- type CliArg
- type CliArgs
- type CliOpt
- type Command
- func (c *Command) Add(sub *Command, more ...*Command)
- func (c *Command) AddCommand(sub *Command)
- func (b *Command) AddError(err error)
- func (b *Command) AddHelpVar(key string, val any)
- func (c *Command) AddSubs(sub *Command, more ...*Command)
- func (b *Command) AliasesMapping() map[string]string
- func (c *Command) App() *App
- func (c *Command) AttachTo(app *App)
- func (b *Command) BinDir() string
- func (b *Command) BinName() string
- func (b *Command) ChWorkDir(dir string)
- func (b *Command) CmdAliases() *structs.Aliases
- func (b *Command) CmdNameMap() map[string]int
- func (b *Command) CmdNames() []string
- func (b *Command) Command(name string) (c *Command, exist bool)
- func (b *Command) CommandNames() []string
- func (b *Command) Commands() map[string]*Command
- func (c *Command) Copy() *Command
- func (c *Command) Disable()
- func (b *Command) FindByPath(path string) *Command
- func (b *Command) FindCommand(path string) *Command
- func (c *Command) Fire(event string, data map[string]any) (stop bool)
- func (b *Command) GetCommand(name string) *Command
- func (b *Command) HasCommand(name string) bool
- func (b *Command) HasCommands() bool
- func (b *Command) HasSubcommands() bool
- func (c *Command) HelpDesc() (desc string)
- func (c *Command) ID() string
- func (c *Command) Init()
- func (b *Command) IsAlias(alias string) bool
- func (b *Command) IsCommand(name string) bool
- func (c *Command) IsDisabled() bool
- func (c *Command) IsRoot() bool
- func (c *Command) IsRunnable() bool
- func (c *Command) IsStandalone() bool
- func (c *Command) IsSubCommand(name string) bool
- func (c *Command) Match(names []string) *Command
- func (c *Command) MatchByPath(path string) *Command
- func (c *Command) MustRun(args []string)
- func (c *Command) NewErr(msg string) error
- func (c *Command) NewErrf(format string, v ...any) error
- func (c *Command) Next()
- func (c *Command) NotStandalone() bool
- func (c *Command) On(name string, handler HookFunc)
- func (c *Command) Parent() *Command
- func (c *Command) ParentName() string
- func (c *Command) Path() string
- func (c *Command) PathNames() []string
- func (b *Command) ResetData()
- func (b *Command) ResolveAlias(alias string) string
- func (c *Command) Root() *Command
- func (c *Command) Run(args []string) (err error)
- func (c *Command) SetFunc(fn RunnerFunc)
- func (b *Command) SetLogo(logo string, style ...string)
- func (c *Command) SetParent(parent *Command)
- func (c *Command) ShowHelp() (err error)
- func (c *Command) Sub(name string) *Command
- func (c *Command) SubCommand(name string) *Command
- func (c *Command) Visible() bool
- func (c *Command) WithFunc(fn RunnerFunc) *Command
- func (c *Command) WithHidden() *Command
- func (b *Command) WorkDir() string
- type Context
- func (ctx *Context) ArgLine() string
- func (ctx *Context) BinDir() string
- func (ctx *Context) BinFile() string
- func (ctx *Context) BinName() string
- func (ctx *Context) ChWorkDir(dir string)
- func (ctx *Context) InitCtx() *Context
- func (ctx *Context) OsArgs() []string
- func (ctx *Context) OsName() string
- func (ctx *Context) PID() int
- func (ctx *Context) PIDString() string
- func (ctx *Context) ResetData()
- func (ctx *Context) Value(key any) any
- func (ctx *Context) WorkDir() string
- type EnumString
- type FlagMeta
- type Flags
- type FlagsConfig
- type GlobalOpts
- type Handler
- type HandlersChain
- type HelpReplacer
- type HookCtx
- type HookFunc
- type Hooks
- type Ints
- type Logo
- type Runner
- type RunnerFunc
- type String
- type Strings
- type VerbLevel
Constants ¶
const ( EvtAppInit = events.OnAppInitAfter EvtAppPrepareAfter = events.OnAppPrepared EvtAppRunBefore = events.OnAppRunBefore EvtAppRunAfter = events.OnAppRunAfter EvtAppRunError = events.OnAppRunError EvtCmdInit = events.OnCmdInitAfter // EvtCmdNotFound app or sub command not found EvtCmdNotFound = events.OnCmdNotFound // EvtAppCmdNotFound app command not found EvtAppCmdNotFound = events.OnAppCmdNotFound // EvtCmdSubNotFound sub command not found EvtCmdSubNotFound = events.OnCmdSubNotFound EvtCmdOptParsed = events.OnCmdOptParsed // EvtCmdRunBefore cmd run EvtCmdRunBefore = events.OnCmdRunBefore EvtCmdRunAfter = events.OnCmdRunAfter EvtCmdRunError = events.OnCmdRunError // EvtCmdExecBefore cmd exec EvtCmdExecBefore = events.OnCmdExecBefore EvtCmdExecAfter = events.OnCmdExecAfter EvtCmdExecError = events.OnCmdExecError EvtGOptionsParsed = events.OnGlobalOptsParsed )
constants for hooks event, there are default allowed event names
const ( // OK success exit code OK = 0 // ERR error exit code ERR = 2 // GOON prepare run successful, goon run command GOON = -1 // CommandSep char CommandSep = ":" // HelpCommand name HelpCommand = "help" // VerbEnvName for set gcli debug level VerbEnvName = "GCLI_VERBOSE" )
const HelpVarFormat = "{$%s}"
HelpVarFormat allow string replace on render help info.
Default support:
"{$binName}" "{$cmd}" "{$fullCmd}" "{$workDir}"
Variables ¶
var AppHelpTemplate = `` /* 675-byte string literal not displayed */
AppHelpTemplate help template for app(all commands)
var CmdHelpTemplate = `` /* 934-byte string literal not displayed */
CmdHelpTemplate help template for a command
Functions ¶
Types ¶
type App ¶
type App struct { // Name app name Name string // Desc app description Desc string // Func on run app, if is empty will display help. Func func(app *App, args []string) error // contains filtered or unexported fields }
App the cli app definition
func NewApp ¶
NewApp create new app instance.
Usage:
NewApp() // Or with a config func NewApp(func(a *App) { // do something before init .... a.Hooks[events.OnAppInitAfter] = func () {} })
func (*App) AddAliases ¶
AddAliases add alias names for a command
func (*App) AddCommand ¶
AddCommand add a new command to the app
func (*App) AddHelpVar ¶ added in v3.1.1
AddHelpVar to instance.
func (*App) AliasesMapping ¶
AliasesMapping get cmd aliases mapping
func (*App) Exec ¶
Exec direct exec other command in current command
Name can be:
- top command name in the app. 'top'
- command path in the app. 'top sub'
Usage:
app.Exec("top") app.Exec("top:sub") app.Exec("top sub") app.Exec("top sub", []string{"-a", "val0", "arg0"})
func (*App) FindByPath ¶
FindByPath command by path. eg: "top:sub" or "top sub"
func (*App) FindCommand ¶
FindCommand command by path. eg: "top:sub" or "top sub"
func (*App) GetCommand ¶
GetCommand get a command by top name
func (*App) HasCommand ¶
HasCommand top command name check
func (*App) MatchByPath ¶
MatchByPath command by path. eg: "top:sub" or "top sub"
func (*App) ResolveAlias ¶
ResolveAlias get real command name by alias
func (*App) Run ¶
Run the application with input args
Usage:
// run with os.Args app.Run(nil) app.Run(os.Args[1:]) // custom args app.Run([]string{"cmd", "--name", "inhere"})
func (*App) RunCmd ¶ added in v3.0.1
RunCmd running a top command with custom args
Usage:
app.Exec("top") app.Exec("top", []string{"-a", "val0", "arg0"}) // can add sub command on args app.Exec("top", []string{"sub", "-o", "abc"})
func (*App) RunLine ¶
RunLine manual run a command by command line string.
eg: app.RunLine("top --top-opt val0 sub --sub-opt val1 arg0")
func (*App) SetDefaultCommand ¶
SetDefaultCommand set default command name
type AppConfig ¶ added in v3.1.0
type AppConfig struct { BeforeRun func() bool AfterRun func() bool BeforeAddOpts func(opts *Flags) AfterAddOpts func(app *App) bool }
AppConfig struct
type Argument ¶
Argument alias of the gflag.CliArg
func NewArgument ¶
NewArgument quick create a new command argument
type Command ¶
type Command struct { // Flags cli (options+arguments) parse and manage for the command gflag.Flags // Name is the command name. Name string // Desc is the command description message. // Can use string-var in contents, eg: {$cmd} Desc string // Aliases is the command name's alias names Aliases arrutil.Strings // Category for the command Category string // Config func, will call on `initialize`. // // - you can config options and other init works Config func(c *Command) // Hidden the command on render help Hidden bool // Subs sub commands of the Command // NOTICE: if command has been initialized, adding through this field is invalid Subs []*Command // Examples some usage example display Examples string // Func is the command handler func. Func Runner // // TIP: // func `args` is the remain arguments after parse flags(options and arguments). Func RunnerFunc // Help is the long help message text // Can use string-var in contents, eg: {$cmd} Help string // HelpRender custom render cmd help message HelpRender func(c *Command) // contains filtered or unexported fields }
Command a CLI command structure
func NewCommand ¶
NewCommand create a new command instance.
Usage:
cmd := NewCommand("my-cmd", "description") // OR with an config func cmd := NewCommand("my-cmd", "description", func(c *Command) { ... }) app.Add(cmd) // OR cmd.AttachTo(app)
func (*Command) AddCommand ¶
AddCommand add a sub command
func (*Command) AddHelpVar ¶ added in v3.1.1
AddHelpVar to instance.
func (*Command) AliasesMapping ¶
AliasesMapping get cmd aliases mapping
func (*Command) BinDir ¶ added in v3.1.1
func (b *Command) BinDir() string
BinDir get bin script dirname
func (*Command) BinName ¶ added in v3.1.1
func (b *Command) BinName() string
BinName get bin script name
func (*Command) ChWorkDir ¶ added in v3.2.2
func (b *Command) ChWorkDir(dir string)
ChWorkDir work dir path
func (*Command) CmdAliases ¶
CmdAliases get cmd aliases
func (*Command) CmdNameMap ¶
CmdNameMap get all command names
func (*Command) CommandNames ¶
func (b *Command) CommandNames() []string
CommandNames get all command names
func (*Command) FindByPath ¶
FindByPath command by path. eg: "top:sub" or "top sub"
func (*Command) FindCommand ¶
FindCommand command by path. eg: "top:sub" or "top sub"
func (*Command) GetCommand ¶
GetCommand get a command by top name
func (*Command) HasCommand ¶
HasCommand top command name check
func (*Command) HasSubcommands ¶ added in v3.1.0
func (b *Command) HasSubcommands() bool
HasSubcommands on the app
func (*Command) IsRunnable ¶ added in v3.1.1
IsRunnable reports whether the command can be run; otherwise it is a documentation pseudo-command such as import path.
func (*Command) IsSubCommand ¶
IsSubCommand name check. alias of the HasCommand()
func (*Command) MatchByPath ¶
MatchByPath command by path. eg: "top:sub"
func (*Command) MustRun ¶
MustRun Alone the current command, will panic on error
Usage:
// run with os.Args cmd.MustRun(nil) cmd.MustRun(os.Args[1:]) // custom args cmd.MustRun([]string{"-a", ...})
func (*Command) ParentName ¶
ParentName name of the parent command
func (*Command) ResolveAlias ¶
ResolveAlias get real command name by alias
func (*Command) Run ¶
Run standalone running the command
Usage:
// run with os.Args cmd.Run(nil) cmd.Run(os.Args[1:]) // custom args cmd.Run([]string{"-a", ...})
func (*Command) SetFunc ¶
func (c *Command) SetFunc(fn RunnerFunc)
SetFunc Settings command handler func
func (*Command) SubCommand ¶
SubCommand get sub command by name. eg "sub"
func (*Command) WithFunc ¶
func (c *Command) WithFunc(fn RunnerFunc) *Command
WithFunc Settings command handler func
func (*Command) WithHidden ¶ added in v3.0.1
WithHidden Settings command is hidden
type Context ¶ added in v3.1.0
Context struct
type EnumString ¶
type EnumString = cflag.EnumString
EnumString The string flag list, implemented flag.Value interface
type FlagsConfig ¶ added in v3.0.3
FlagsConfig alias of the gflag.Config
type GlobalOpts ¶ added in v3.1.0
type GlobalOpts struct { Disable bool NoColor bool Verbose VerbLevel // message report level ShowHelp bool // ShowVersion show version information ShowVersion bool // NoProgress dont display progress NoProgress bool // NoInteractive close interactive confirm NoInteractive bool // contains filtered or unexported fields }
GlobalOpts global flag options
func (*GlobalOpts) SetDisable ¶ added in v3.1.0
func (g *GlobalOpts) SetDisable()
SetDisable global options
func (*GlobalOpts) SetStrictMode ¶ added in v3.1.0
func (g *GlobalOpts) SetStrictMode(strictMode bool)
SetStrictMode option
func (*GlobalOpts) SetVerbose ¶ added in v3.1.0
func (g *GlobalOpts) SetVerbose(verbose VerbLevel)
SetVerbose value
type Handler ¶
type Handler interface { // Creator for create new command Creator() *Command // Config bind Flags or Arguments for the command Config(c *Command) // Execute the command Execute(c *Command, args []string) error }
Handler interface definition
type HandlersChain ¶
type HandlersChain []RunnerFunc
HandlersChain middleware handlers chain definition
func (HandlersChain) Last ¶
func (c HandlersChain) Last() RunnerFunc
Last returns the last handler in the chain. tip: the last handler is the main own.
type HelpReplacer ¶ added in v3.1.1
type HelpReplacer struct {
VarOpen, VarClose string
// contains filtered or unexported fields
}
HelpReplacer provide string var replace for render help template.
func (*HelpReplacer) AddReplace ¶ added in v3.1.1
func (hv *HelpReplacer) AddReplace(name, value string)
AddReplace get command name. AddReplace
func (*HelpReplacer) AddReplaces ¶ added in v3.1.1
func (hv *HelpReplacer) AddReplaces(vars map[string]string)
AddReplaces add multi tpl vars.
func (*HelpReplacer) GetReplace ¶ added in v3.1.1
func (hv *HelpReplacer) GetReplace(name string) string
GetReplace get a help var by name
func (*HelpReplacer) ReplacePairs ¶ added in v3.1.1
func (hv *HelpReplacer) ReplacePairs(input string) string
ReplacePairs replace string vars in the input text.
func (*HelpReplacer) Replaces ¶ added in v3.1.1
func (hv *HelpReplacer) Replaces() map[string]string
Replaces get all tpl vars.
type HookCtx ¶
type HookCtx struct { context.Context maputil.Data App *App Cmd *Command // contains filtered or unexported fields }
HookCtx struct
type HookFunc ¶
HookFunc definition.
Returns:
- True for stop continue run.
- False continue handle next logic.
type Hooks ¶
type Hooks struct {
// contains filtered or unexported fields
}
Hooks struct. hookManager
type Runner ¶
type Runner interface { // Run the command // // TIP: // args is the remain arguments after parse flags(options and arguments). Run(c *Command, args []string) error }
Runner /Executor interface
type RunnerFunc ¶
RunnerFunc definition
TIP:
args is the remain arguments after parse flags(options and arguments).
Directories ¶
Path | Synopsis |
---|---|
sflag
Package sflag is an simple cli flag parse tool
|
Package sflag is an simple cli flag parse tool |
Package gflag provide command line options and arguments binding, parse, management.
|
Package gflag provide command line options and arguments binding, parse, management. |
Package interact collect some interactive methods for CLI
|
Package interact collect some interactive methods for CLI |
Package progress provide terminal progress bar display.
|
Package progress provide terminal progress bar display. |
Package show provides some formatter tools for display data.
|
Package show provides some formatter tools for display data. |