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 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, aliases ...string)
- func (app *App) AddCommand(c *Command) *Command
- func (app *App) AddCommander(cmder Commander) *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) Exit(code int)
- func (c App) GlobalFlags() *Flags
- func (app *App) HasCommand(name string) bool
- func (app *App) IsAlias(str string) bool
- 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) RemoveCommand(names ...string) int
- func (app *App) ResolveName(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) HelpName() string
- 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 Arguments
- func (ags *Arguments) Add(arg Argument) *Argument
- func (ags *Arguments) AddArg(name, desc string, requiredAndIsArray ...bool) *Argument
- func (ags *Arguments) AddArgument(arg *Argument) *Argument
- func (ags *Arguments) Arg(name string) *Argument
- func (ags *Arguments) ArgByIndex(i int) *Argument
- func (ags *Arguments) Args() []*Argument
- func (ags *Arguments) BindArg(arg Argument) *Argument
- func (ags *Arguments) ParseArgs(args []string) (err error)
- func (ags *Arguments) SetName(name string)
- func (ags *Arguments) SetValidateNum(validateNum bool)
- type Booleans
- type CmdFunc
- type Command
- func (c *Command) AliasesString(sep ...string) string
- func (c *Command) App() *App
- func (c *Command) AttachTo(app *App)
- 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) GlobalFlags() *Flags
- func (c *Command) ID() string
- 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) Run(inArgs []string) (err error)
- func (c *Command) Runnable() bool
- func (c *Command) SetFunc(fn CmdFunc) *Command
- func (c *Command) ShowHelp()
- type Commander
- type Executor
- type FlagMeta
- type Flags
- func (fs *Flags) BoolOpt(p *bool, name, shorts string, defValue bool, desc string)
- func (fs *Flags) BoolVar(p *bool, meta FlagMeta)
- func (fs *Flags) FSet() *flag.FlagSet
- func (fs *Flags) FlagMeta(name string) *FlagMeta
- func (fs *Flags) FlagNames() map[string]int
- func (fs *Flags) Float64Opt(p *float64, name, shorts string, defValue float64, desc string)
- func (fs *Flags) Float64Var(p *float64, meta FlagMeta)
- func (fs *Flags) FromStruct(ptr interface{}) error
- func (fs *Flags) HasFlagMeta(name string) bool
- func (fs *Flags) HasOption(name string) bool
- func (fs *Flags) InitFlagSet(name string)
- func (fs *Flags) Int64Opt(p *int64, name, shorts string, defValue int64, desc string)
- func (fs *Flags) Int64Var(p *int64, meta FlagMeta)
- func (fs *Flags) IntOpt(p *int, name, shorts string, defValue int, desc string)
- func (fs *Flags) IntVar(p *int, meta FlagMeta)
- func (fs *Flags) IsShortName(short string) bool
- func (fs *Flags) IsShortOpt(short string) bool
- func (fs *Flags) IterAll(fn func(f *flag.Flag, meta *FlagMeta))
- func (fs *Flags) LookupFlag(name string) *flag.Flag
- func (fs *Flags) Metas() map[string]*FlagMeta
- func (fs *Flags) Name() string
- func (fs *Flags) Parse(args []string) error
- func (fs *Flags) PrintHelpPanel()
- func (fs *Flags) RawArg(i int) string
- func (fs *Flags) RawArgs() []string
- func (fs *Flags) SetFlagSet(fSet *flag.FlagSet)
- func (fs *Flags) SetOutput(out io.Writer)
- func (fs *Flags) ShortNames(name string) (ss []string)
- func (fs *Flags) StrOpt(p *string, name, shorts, defValue, desc string)
- func (fs *Flags) StrVar(p *string, meta FlagMeta)
- func (fs *Flags) String() string
- func (fs *Flags) Uint64Opt(p *uint64, name, shorts string, defValue uint64, desc string)
- func (fs *Flags) Uint64Var(p *uint64, meta FlagMeta)
- func (fs *Flags) UintOpt(p *uint, name, shorts string, defValue uint, desc string)
- func (fs *Flags) UintVar(p *uint, meta FlagMeta)
- func (fs *Flags) Var(p flag.Value, meta FlagMeta)
- func (fs *Flags) VarOpt(p flag.Value, name, shorts, desc string)
- func (fs *Flags) WithOption(cfg FlagsOption) *Flags
- type FlagsOption
- type GlobalOpts
- type HelpVars
- type HookFunc
- type Hooks
- type Ints
- type Logo
- type Runner
- type RunningAble
- 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 ( EvtAppInit = "app.init" EvtAppBefore = "app.run.before" EvtAppAfter = "app.run.after" EvtAppError = "app.run.error" EvtCmdInit = "cmd.init" EvtCmdBefore = "cmd.run.before" EvtCmdAfter = "cmd.run.after" EvtCmdError = "cmd.run.error" EvtAppPrepareAfter = "app.prepare.after" )
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 ( AlignLeft = strutil.PosRight AlignRight = strutil.PosLeft )
The options alignment type - Align right, padding left - Align left, padding right
const HelpVarFormat = "{$%s}"
HelpVarFormat allow var replace on render help info. Default support:
"{$binName}" "{$cmd}" "{$fullCmd}" "{$workDir}"
Variables ¶
var ( // CLI create an default instance CLI = &cmdLine{ pid: os.Getpid(), osName: runtime.GOOS, binName: os.Args[0], argLine: strings.Join(os.Args[1:], " "), } )
var Version = "2.2.1"
Version the gCli version
Functions ¶
Types ¶
type App ¶
type App struct { // 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 // ExitFunc default is os.Exit ExitFunc func(int) // 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) AddCommander ¶ added in v2.3.0
AddCommander to the application
func (*App) CommandNames ¶
CommandNames get all command names
func (*App) DefaultCommand ¶
DefaultCommand set default command name
func (App) GlobalFlags ¶ added in v2.3.0
func (c App) GlobalFlags() *Flags
GlobalFlags get the app GlobalFlags
func (*App) HasCommand ¶ added in v2.3.0
HasCommand in the application
func (*App) NewCommand ¶
NewCommand create a new command
func (*App) RealCommandName ¶
RealCommandName get real command name by alias Deprecated
func (*App) RemoveCommand ¶ added in v2.3.0
RemoveCommand from the application
func (*App) ResolveName ¶ added in v2.3.0
ResolveName get real command name by alias
type Argument ¶
type Argument struct { // Name argument name. it's required Name string // Desc argument description message Desc string // Type name. eg: string, int, array // Type string // ShowName is a name for display help. default is equals to Name. ShowName string // Required arg is required Required bool // IsArray if is array, can allow accept multi values, and must in last. IsArray bool // valWrapper Value TODO ... // 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 Arguments ¶ added in v2.3.0
type Arguments struct {
// contains filtered or unexported fields
}
Arguments definition
func (*Arguments) AddArg ¶ added in v2.3.0
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 (*Arguments) AddArgument ¶ added in v2.3.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 (*Arguments) Arg ¶ added in v2.3.0
Arg get arg by defined name. Usage:
intVal := ags.Arg("name").Int() strVal := ags.Arg("name").String() arrVal := ags.Arg("names").Array()
func (*Arguments) ArgByIndex ¶ added in v2.3.0
ArgByIndex get named arg by index
func (*Arguments) SetValidateNum ¶ added in v2.3.0
SetValidateNum check
type Command ¶
type Command struct { // 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 // Config func, will call on `initialize`. // - you can config options and other init works Config func(c *Command) // Flags(command options) is a set of flags specific to this command. // Flags flag.FlagSet // Examples some usage example display Examples string // Func is the command handler func. Func Runner Func CmdFunc // Help is the long help message text Help string // CustomFlags indicates that the command will do its own flag parsing. CustomFlags bool // Flags options for the command. Flags // Arguments for the command Arguments // 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) AliasesString ¶
AliasesString returns aliases string
func (Command) GlobalFlags ¶ added in v2.3.0
func (c Command) GlobalFlags() *Flags
GlobalFlags get the app GlobalFlags
func (*Command) Runnable ¶
Runnable reports whether the command can be run; otherwise it is a documentation pseudo-command such as import path.
type Commander ¶ added in v2.3.0
type Commander interface { // Creator for create new command Creator() *Command // Prepare bind Flags or Arguments for the command Prepare(c *Command) // Execute(c *Command, args []string) error Run(c *Command, args []string) error }
Commander interface definition
type FlagMeta ¶ added in v2.3.0
type FlagMeta struct {
// varPtr interface{}
// name and description
Name, Desc string
// default value for the flag option
DefVal interface{}
// short names. eg: ["o", "a"]
Shorts []string
// special setting
Hidden, Required bool
// contains filtered or unexported fields
}
FlagMeta for an flag(option/argument)
func (*FlagMeta) Description ¶ added in v2.3.0
Description of the flag
type Flags ¶ added in v2.3.0
type Flags struct { // FlagsOption option for render help message FlagsOption // contains filtered or unexported fields }
Flags struct definition
func (*Flags) Float64Opt ¶ added in v2.3.0
Float64Opt binding an float64 option
func (*Flags) Float64Var ¶ added in v2.3.0
Float64Var binding an float64 option flag
func (*Flags) FromStruct ¶ added in v2.3.0
FromStruct from struct tag binding options
func (*Flags) HasFlagMeta ¶ added in v2.3.0
HasFlagMeta check it is has FlagMeta
func (*Flags) InitFlagSet ¶ added in v2.3.0
create and init flag.FlagSet
func (*Flags) IsShortName ¶ added in v2.3.0
IsShortcut check it is a shortcut name
func (*Flags) IsShortOpt ¶ added in v2.3.0
IsShortOpt alias of the IsShortcut()
func (*Flags) LookupFlag ¶ added in v2.3.0
LookupFlag get flag.Flag by name
func (*Flags) PrintHelpPanel ¶ added in v2.3.0
func (fs *Flags) PrintHelpPanel()
PrintHelpPanel for all options to the gf.out
func (*Flags) SetFlagSet ¶ added in v2.3.0
SetFlagSet set the raw *flag.FlagSet
func (*Flags) ShortNames ¶ added in v2.3.0
ShortNames get all short-names of the option
func (*Flags) VarOpt ¶ added in v2.3.0
VarOpt binding a custom var option Usage:
var names gcli.Strings cmd.VarOpt(&names, "tables", "t", "description ...")
func (*Flags) WithOption ¶ added in v2.3.0
func (fs *Flags) WithOption(cfg FlagsOption) *Flags
WithOption for render help panel message
type FlagsOption ¶ added in v2.3.0
type FlagsOption struct { // WithoutType dont display flag data type on print help WithoutType bool // NameDescOL flag and desc at one line on print help NameDescOL bool // Alignment flag align left or right. default is: right Alignment uint8 }
FlagsOption for render help information
type GlobalOpts ¶
type GlobalOpts struct {
// contains filtered or unexported fields
}
GlobalOpts global flags
type HelpVars ¶
type HelpVars struct { // varLeft, varRight string // varFormat string // 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 Hooks ¶ added in v2.3.0
type Hooks struct {
// contains filtered or unexported fields
}
Hooks struct
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
sflag
Package sflag is an simple cli flag parse tool
|
Package sflag is an simple cli flag parse tool |
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. |