Skip to content

Commit

Permalink
new: add app.RunCmd for direct run an top command
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jun 1, 2021
1 parent 132d38c commit 7160b62
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 45 deletions.
14 changes: 13 additions & 1 deletion app.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,17 @@ func (app *App) Run(args []string) (code int) {
return app.exitOnEnd(code)
}

// RunCmd running an 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 *App) RunCmd(name string, args []string) int {
return app.doRunCmd(name, args)
}

func (app *App) doRunCmd(name string, args []string) (code int) {
cmd := app.GetCommand(name)
app.Fire(EvtAppRunBefore, cmd)
Expand Down Expand Up @@ -518,7 +529,8 @@ func (app *App) exitOnEnd(code int) int {
return code
}

// Exec running other command in current command
// 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'
Expand Down
37 changes: 2 additions & 35 deletions base.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ import (

// core definition TODO rename to context ??
type core struct {
color.SimplePrinter
*cmdLine
// Hooks manage. allowed hooks: "init", "before", "after", "error"
*Hooks
// HelpVars help template vars.
HelpVars
// global options flag set
gFlags *Flags
SimplePrinter
// GOptsBinder you can custom binding global options
GOptsBinder func(gf *Flags)
}
Expand Down Expand Up @@ -77,39 +77,6 @@ func (c core) innerHelpVars() map[string]string {
}
}

// SimplePrinter struct. for inject struct
type SimplePrinter struct{}

// Print message
func (s SimplePrinter) Print(v ...interface{}) {
color.Print(v...)
}

// Printf message
func (s SimplePrinter) Printf(format string, v ...interface{}) {
color.Printf(format, v...)
}

// Println message
func (s SimplePrinter) Println(v ...interface{}) {
color.Println(v...)
}

// Infoln message
func (s SimplePrinter) Infoln(a ...interface{}) {
color.Info.Println(a...)
}

// Warnln message
func (s SimplePrinter) Warnln(a ...interface{}) {
color.Warn.Println(a...)
}

// Errorln message
func (s SimplePrinter) Errorln(a ...interface{}) {
color.Error.Println(a...)
}

// simple map[string]interface{} struct
type mapData struct {
data map[string]interface{}
Expand Down Expand Up @@ -182,7 +149,7 @@ func (hc *HookCtx) Name() string {
return hc.name
}

// Hooks struct
// Hooks struct. hookManager
type Hooks struct {
// Hooks can setting some hooks func on running.
hooks map[string]HookFunc
Expand Down
2 changes: 1 addition & 1 deletion cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ func (c *Command) Match(names []string) *Command {
return c.commandBase.Match(names)
}

// Match command by path. eg. "top:sub"
// MatchByPath command by path. eg. "top:sub"
func (c *Command) MatchByPath(path string) *Command {
return c.Match(splitPath2names(path))
}
Expand Down
15 changes: 7 additions & 8 deletions gcli.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,21 @@ const (

EvtCmdInit = "cmd.init"

// app or sub command not found
// EvtCmdNotFound app or sub command not found
EvtCmdNotFound = "cmd.not.found"
// app command not found
// EvtAppCmdNotFound app command not found
EvtAppCmdNotFound = "app.cmd.not.found"
// sub command not found
// EvtCmdSubNotFound sub command not found
EvtCmdSubNotFound = "cmd.sub.not.found"

EvtCmdOptParsed = "cmd.opts.parsed"

// cmd run
// EvtCmdRunBefore cmd run
EvtCmdRunBefore = "cmd.run.before"
EvtCmdRunAfter = "cmd.run.after"
EvtCmdRunError = "cmd.run.error"

// cmd exec
// EvtCmdExecBefore cmd exec
EvtCmdExecBefore = "cmd.exec.before"
EvtCmdExecAfter = "cmd.exec.after"
EvtCmdExecError = "cmd.exec.error"
Expand Down Expand Up @@ -155,7 +155,7 @@ func SetStrictMode(strict bool) {
gOpts.SetStrictMode(strict)
}

// IsGtVerbose get is strict mode
// IsGteVerbose get is strict mode
func IsGteVerbose(verb VerbLevel) bool {
return gOpts.verbose >= verb
}
Expand All @@ -167,7 +167,6 @@ func IsDebugMode() bool {

// Commander interface
type Commander interface {
// Run([]string) int
Value(string) interface{}
SetValue(string, interface{})
}
Expand Down Expand Up @@ -266,7 +265,7 @@ func (vl VerbLevel) Upper() string {
return strings.ToUpper(vl.Name())
}

// String verbose level to string.
// Name verbose level to string.
func (vl VerbLevel) Name() string {
switch vl {
case VerbQuiet:
Expand Down

0 comments on commit 7160b62

Please sign in to comment.