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 README
Index ¶
- Constants
- Variables
- func Exit(code int)
- func Logf(level uint, format string, v ...interface{})
- func Print(args ...interface{})
- func Printf(format string, args ...interface{})
- func Println(args ...interface{})
- func SetDebugMode()
- func SetQuietMode()
- func SetStrictMode(strict bool)
- func SetVerbose(verbose uint)
- func StrictMode() bool
- func Verbose() uint
- type App
- func (app *App) Add(c *Command, more ...*Command)
- func (app *App) AddAliases(command string, names []string)
- func (app *App) AddCommand(c *Command) *Command
- func (app *App) AddError(err error)
- func (app *App) CleanArgs() []string
- func (app *App) CommandName() string
- func (app *App) CommandNames() []string
- func (app *App) Commands() map[string]*Command
- func (app *App) Config(fn func(a *App))
- func (app *App) DefaultCommand(name string)
- func (app *App) Exec(name string, args []string) (err error)
- func (app *App) IsCommand(name string) bool
- func (app *App) Names() map[string]int
- func (app *App) NewCommand(name, useFor string, config func(c *Command)) *Command
- func (app *App) On(name string, handler HookFunc)
- func (app *App) RealCommandName(alias string) string
- func (app *App) Run() (code int)
- func (app *App) SetDebugMode()
- func (app *App) SetLogo(logo string, style ...string)
- func (app *App) SetQuietMode()
- func (app *App) SetVerbose(verbose uint)
- type Argument
- func (a *Argument) Array() (ss []string)
- func (a *Argument) Config(fn func(arg *Argument))
- func (a *Argument) GetValue() interface{}
- func (a *Argument) HasValue() bool
- func (a *Argument) Index() int
- func (a *Argument) Int(defVal ...int) int
- func (a *Argument) IsEmpty() bool
- func (a *Argument) String(defVal ...string) string
- func (a *Argument) StringSplit(sep ...string) (ss []string)
- func (a *Argument) Strings() (ss []string)
- func (a *Argument) WithValidator(fn func(interface{}) (interface{}, error)) *Argument
- func (a *Argument) WithValue(val interface{}) *Argument
- type Booleans
- type CmdFunc
- type CmdLine
- type Command
- func (c *Command) AddArg(name, description string, requiredAndIsArray ...bool) *Argument
- func (c *Command) AddArgument(arg *Argument) *Argument
- func (c *Command) AliasesString(sep ...string) string
- func (c *Command) App() *App
- func (c *Command) Arg(name string) *Argument
- func (c *Command) ArgByIndex(i int) *Argument
- func (c *Command) Args() []*Argument
- func (c *Command) AttachTo(app *App)
- func (c *Command) BoolOpt(p *bool, name, short string, defValue bool, description string) *Command
- func (c *Command) Copy() *Command
- func (c *Command) Disable()
- func (c *Command) Errorf(format string, v ...interface{}) error
- func (c *Command) Fire(event string, data interface{})
- func (c *Command) ID() string
- func (c *Command) IntOpt(p *int, name, short string, defValue int, description string) *Command
- func (c *Command) IsAlone() bool
- func (c *Command) IsDisabled() bool
- func (c *Command) Logf(level uint, format string, v ...interface{})
- func (c *Command) Module() string
- func (c *Command) MustRun(inArgs []string)
- func (c *Command) NotAlone() bool
- func (c *Command) On(name string, handler HookFunc)
- func (c *Command) OptDes(name string) string
- func (c *Command) OptFlag(name string) *flag.Flag
- func (c *Command) OptNames() map[string]string
- func (c *Command) ParseDefaults() string
- func (c *Command) RawArg(i int) string
- func (c *Command) RawArgs() []string
- func (c *Command) Run(inArgs []string) (err error)
- func (c *Command) Runnable() bool
- func (c *Command) SetFunc(fn CmdFunc) *Command
- func (c *Command) ShortName(name string) string
- func (c *Command) ShowHelp(quit ...bool)
- func (c *Command) StrOpt(p *string, name, short string, defValue, description string) *Command
- func (c *Command) UintOpt(p *uint, name, short string, defValue uint, description string) *Command
- func (c *Command) VarOpt(p flag.Value, name string, short string, description string) *Command
- type GlobalOpts
- type HelpVars
- type HookFunc
- type Ints
- type Logo
- type Runner
- type SimpleHooks
- type Strings
Constants ¶
const ( VerbQuiet uint = iota // don't report anything VerbError // reporting on error VerbWarn VerbInfo VerbDebug VerbCrazy )
constants for error level 0 - 4
const ( EvtInit = "init" EvtBefore = "before" EvtAfter = "after" EvtError = "error" )
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 // HelpCommand name HelpCommand = "help" )
const HelpVarFormat = "{$%s}"
HelpVarFormat allow var replace on render help info. Default support:
"{$binName}" "{$cmd}" "{$fullCmd}" "{$workDir}"
Variables ¶
Functions ¶
Types ¶
type App ¶
type App struct {
// internal use
*CmdLine
HelpVars
SimpleHooks // allow hooks: "init", "before", "after", "error"
// Name app name
Name string
// Version app version. like "1.0.1"
Version string
// Description app description
Description string
// Logo ASCII logo setting
Logo Logo
// Args default is equals to os.args
Args []string
// ExitOnEnd call os.Exit on running end
ExitOnEnd bool
// contains filtered or unexported fields
}
App the cli app definition
func InitStdApp ¶ added in v2.0.9
InitStdApp create the default cli app.
func NewApp ¶
NewApp create new app instance. Usage:
NewApp()
// Or with a config func
NewApp(func(a *App) {
// do something before init ....
a.Hooks[gcli.EvtInit] = func () {}
})
func (*App) AddAliases ¶
AddAliases add alias names for a command
func (*App) AddCommand ¶
AddCommand add a new command
func (*App) CommandNames ¶
CommandNames get all command names
func (*App) DefaultCommand ¶
DefaultCommand set default command name
func (*App) NewCommand ¶
NewCommand create a new command
func (*App) RealCommandName ¶
RealCommandName get real command name by alias
type Argument ¶
type Argument struct {
// Name argument name. it's required
Name string
// ShowName is a name for display help. default is equals to Name.
ShowName string
// Description argument description message
Description string
// Required arg is required
Required bool
// IsArray if is array, can allow accept multi values, and must in last.
IsArray bool
// value store parsed argument data. (type: string, []string)
Value interface{}
// Handler custom argument value parse handler
Handler func(val interface{}) interface{}
// Validator you can add an validator, will call it on binding argument value
Validator func(val interface{}) (interface{}, error)
// contains filtered or unexported fields
}
Argument a command argument definition
func NewArgument ¶ added in v2.1.0
NewArgument quick create an new command argument
func (*Argument) GetValue ¶ added in v2.1.0
func (a *Argument) GetValue() interface{}
GetValue get value by custom handler func
func (*Argument) StringSplit ¶ added in v2.1.0
StringSplit quick split a string argument to string slice
func (*Argument) WithValidator ¶ added in v2.1.1
WithValidator set an value validator of the argument
type CmdLine ¶
type CmdLine struct {
// contains filtered or unexported fields
}
CmdLine store common data for CLI
type Command ¶
type Command struct {
// CmdLine is internal use
*CmdLine
HelpVars
// SimpleHooks can allow setting some hooks func on running.
SimpleHooks // allowed hooks: "init", "before", "after", "error"
// Name is the command name.
Name string
// UseFor is the command description message.
UseFor string
// Aliases is the command name's alias names
Aliases []string
// Func is the command handler func. Func Runner
Func CmdFunc
// Config func, will call on `initialize`. you can config options and other works
Config func(c *Command)
// Flags(command options) is a set of flags specific to this command.
Flags flag.FlagSet
// CustomFlags indicates that the command will do its own flag parsing.
CustomFlags bool
// Examples some usage example display
Examples string
// Help is the long help message text
Help string
// contains filtered or unexported fields
}
Command a CLI command structure
func NewCommand ¶
NewCommand create a new command instance. Usage:
cmd := NewCommand("my-cmd", "description", func(c *Command) { ... })
app.Add(cmd) // OR cmd.AttachTo(app)
func (*Command) AddArg ¶
AddArg binding an named argument for the command. Notice:
- Required argument cannot be defined after optional argument
- Only one array parameter is allowed
- The (array) argument of multiple values can only be defined at the end
Usage:
cmd.AddArg("name", "description")
cmd.AddArg("name", "description", true) // required
cmd.AddArg("names", "description", true, true) // required and is array
func (*Command) AddArgument ¶ added in v2.1.0
AddArgument binding an named argument for the command.
Notice:
- Required argument cannot be defined after optional argument
- Only one array parameter is allowed
- The (array) argument of multiple values can only be defined at the end
func (*Command) AliasesString ¶
AliasesString returns aliases string
func (*Command) Arg ¶
Arg get arg by defined name. Usage:
intVal := c.Arg("name").Int()
strVal := c.Arg("name").String()
arrVal := c.Arg("names").Array()
func (*Command) ArgByIndex ¶
ArgByIndex get named arg by index
func (*Command) ParseDefaults ¶
ParseDefaults prints, to standard error unless configured otherwise, the default values of all defined command-line flags in the set. See the documentation for the global function PrintDefaults for more information.
NOTICE: the func is copied from package 'flag', func 'PrintDefaults'
func (*Command) Runnable ¶
Runnable reports whether the command can be run; otherwise it is a documentation pseudo-command such as import path.
type GlobalOpts ¶
type GlobalOpts struct {
// contains filtered or unexported fields
}
GlobalOpts global flags
type HelpVars ¶
type HelpVars struct {
// Vars you can add some vars map for render help info
Vars map[string]string
}
HelpVars struct. provide string var function for render help template.
func (*HelpVars) ReplaceVars ¶
ReplaceVars replace vars in the input string.
type HookFunc ¶
type HookFunc func(obj ...interface{})
HookFunc definition. func arguments:
in app, like: func(app *App, data interface{})
in cmd, like: func(cmd *Command, data interface{})
type HookFunc func(obj interface{}, data interface{})
type SimpleHooks ¶
type SimpleHooks struct {
// contains filtered or unexported fields
}
SimpleHooks struct
func (*SimpleHooks) AddOn ¶ added in v2.1.0
func (sh *SimpleHooks) AddOn(name string, handler HookFunc)
AddOn register on not exists hook.
func (*SimpleHooks) Fire ¶
func (sh *SimpleHooks) Fire(event string, data ...interface{})
Fire event by name, allow with event data
func (*SimpleHooks) On ¶
func (sh *SimpleHooks) On(name string, handler HookFunc)
On register event hook by name
Directories
¶
| Path | Synopsis |
|---|---|
|
alone
command
|
|
|
srvmanager
command
|
|
|
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 sflag is an simple cli flag parse tool
|
Package sflag is an simple cli flag parse tool |
|
Package show provides some formatter tools for display data.
|
Package show provides some formatter tools for display data. |















