Documentation ¶
Overview ¶
Package gcli is a simple to use command line application, written using golang
Source code and other details for the project are available at GitHub:
https://github.com/gookit/gcli
usage please ref examples and README
Index ¶
- Constants
- Variables
- func AllCommands() map[string]*Command
- 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 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) AddVar(name, value string)
- func (app *App) AddVars(vars map[string]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) DebugMode()
- func (app *App) DefaultCommand(name string)
- func (app *App) Exec(name string, args []string) int
- func (app *App) GetVar(name string) string
- func (app *App) GetVars(name string, value string) map[string]string
- func (app *App) Initialize()
- func (app *App) IsCommand(name string) bool
- func (app *App) NewCommand(name, useFor string, config func(c *Command)) *Command
- func (app *App) On(name string, handler func(a *App, data interface{}))
- func (app *App) QuietMode()
- func (app *App) RealCommandName(alias string) string
- func (app *App) Run()
- func (app *App) SetLogo(logo string, style ...string)
- func (app *App) SetVerbose(verbose uint)
- type Argument
- type Booleans
- type CmdFunc
- type CmdLine
- type Command
- func (c *Command) AddArg(name, description string, requiredAndIsArray ...bool) *Argument
- func (c *Command) AddVars(vars map[string]string)
- 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 string, short string, defValue bool, description string) *Command
- func (c *Command) Copy() *Command
- func (c *Command) Disable()
- func (c *Command) Error() error
- func (c *Command) Errorf(format string, v ...interface{}) int
- func (c *Command) Execute(args []string) int
- func (c *Command) GetVar(name string) string
- func (c *Command) IntOpt(p *int, name string, 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) NotAlone() bool
- func (c *Command) On(name string, handler func(c *Command, data interface{}))
- 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) int
- 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 string, short string, defValue string, description string) *Command
- func (c *Command) UintOpt(p *uint, name string, short string, defValue uint, description string) *Command
- func (c *Command) VarOpt(p flag.Value, name string, short string, description string) *Command
- func (c *Command) WithError(err error) int
- type GlobalOpts
- type HookFunc
- type Ints
- type Logo
- type Runner
- 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 )
const HelpVar = "{$%s}"
HelpVar allow var replace in help info. default support:
"{$binName}" "{$cmd}" "{$fullCmd}" "{$workDir}"
Variables ¶
Functions ¶
Types ¶
type App ¶
type App struct { // internal use *CmdLine // 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 // Hooks can setting some hooks func on running. // allow hooks: "init", "before", "after", "error" Hooks map[string]appHookFunc // Strict use strict mode. short opt must be begin '-', long opt must be begin '--' Strict bool // contains filtered or unexported fields }
App the cli app definition
func Instance ¶ added in v1.2.1
func Instance() *App
Instance returns the current application instance
func New ¶
New create new app instance. eg:
gcli.NewApp() gcli.NewApp(func(a *App) { // do something before init .... a.Hooks[gcli.EvtInit] = func () {} })
func NewApp ¶
NewApp create new app instance. alias of the New() eg:
gcli.New() gcli.New(func(a *App) { // do something before init .... a.Hooks[gcli.EvtInit] = func () {} })
func (*App) AddAliases ¶ added in v1.2.1
AddAliases add alias names for a command
func (*App) AddCommand ¶ added in v1.2.1
AddCommand add a new command
func (*App) CommandName ¶ added in v1.2.1
CommandName get current command name
func (*App) CommandNames ¶ added in v1.2.1
CommandNames get all command names
func (*App) Config ¶ added in v1.2.1
Config the application. Notice: must be called before adding a command
func (*App) DefaultCommand ¶ added in v1.2.1
DefaultCommand set default command name
func (*App) NewCommand ¶ added in v1.2.1
NewCommand create a new command
func (*App) RealCommandName ¶ added in v1.2.1
RealCommandName get real command name by alias
type Argument ¶
type Argument struct { // Name argument name Name string // ShowName is a name for display help. default is equals to Name. ShowName string // Description argument description message Description string // IsArray if is array, can allow accept multi values, and must in last. IsArray bool // Required arg is required Required bool // value store parsed argument data. (type: string, []string) Value interface{} // contains filtered or unexported fields }
Argument a command argument definition
type CmdLine ¶
type CmdLine struct {
// contains filtered or unexported fields
}
CmdLine store common data for CLI
type Command ¶
type Command struct { // is internal use *CmdLine // Name is the command name. Name string // UseFor is the command description message. UseFor 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) // Hooks can setting some hooks func on running. // allow hooks: "init", "before", "after", "error" Hooks map[string]HookFunc // Aliases is the command name's alias names Aliases []string // 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 // Vars you can add some vars map for render help info Vars map[string]string // Help is the long help message text Help string // Examples some usage example display Examples string // contains filtered or unexported fields }
Command a CLI command structure
func NewCommand ¶ added in v1.2.1
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 a 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) 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) BoolOpt ¶
func (c *Command) BoolOpt(p *bool, name string, short string, defValue bool, description string) *Command
BoolOpt binding a bool option
func (*Command) IntOpt ¶
func (c *Command) IntOpt(p *int, name string, short string, defValue int, description string) *Command
IntOpt binding a int option
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.
func (*Command) StrOpt ¶
func (c *Command) StrOpt(p *string, name string, short string, defValue string, description string) *Command
StrOpt binding a string option
func (*Command) UintOpt ¶
func (c *Command) UintOpt(p *uint, name string, short string, defValue uint, description string) *Command
UintOpt binding a uint option
type GlobalOpts ¶
type GlobalOpts struct {
// contains filtered or unexported fields
}
GlobalOpts global flags
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
Package interact collect some interactive methods for CLI
|
Package interact collect some interactive methods for CLI |
Package show provides some formatter tools for display data.
|
Package show provides some formatter tools for display data. |