代码拉取完成,页面将自动刷新
同步操作将从 gookit/gcli 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
package gcli
import (
"flag"
"fmt"
"log"
"strings"
"text/template"
"github.com/gookit/color"
"github.com/gookit/gcli/helper"
"github.com/gookit/goutil/strutil"
)
// parseGlobalOpts parse global options
func (app *App) parseGlobalOpts() []string {
// don't display date on print log
log.SetFlags(0)
// bind help func
flag.Usage = app.showCommandsHelp
// binding global options
flag.UintVar(&gOpts.verbose, "verbose", gOpts.verbose, "")
flag.BoolVar(&gOpts.showHelp, "h", false, "")
flag.BoolVar(&gOpts.showHelp, "help", false, "")
flag.BoolVar(&gOpts.showVer, "V", false, "")
flag.BoolVar(&gOpts.showVer, "version", false, "")
flag.BoolVar(&gOpts.noColor, "no-color", gOpts.noColor, "")
// this is a internal command
flag.BoolVar(&inCompletion, "cmd-completion", false, "")
// parse global options
flag.Parse()
// check global options
if gOpts.showHelp {
app.showCommandsHelp()
}
if gOpts.showVer {
app.showVersionInfo()
}
if gOpts.noColor {
color.Enable = false
}
return flag.Args()
}
// prepare to running, parse args, get command name and command args
func (app *App) prepareRun() (string, []string) {
args := app.parseGlobalOpts()
if inCompletion {
app.showCompletion(args)
}
// if no input command
if len(args) == 0 {
// will try run defaultCommand
defCmd := app.defaultCommand
if len(defCmd) == 0 {
app.showCommandsHelp()
}
if !app.IsCommand(defCmd) {
Logf(VerbError, "The default command '%s' is not exists", defCmd)
app.showCommandsHelp()
}
args = []string{defCmd}
} else if args[0] == "help" { // is help command
if len(args) == 1 { // like 'help'
app.showCommandsHelp()
}
// like 'help COMMAND'
app.showCommandHelp(args[1:], true)
}
return args[0], args[1:]
}
// Run running application
func (app *App) Run() {
rawName, args := app.prepareRun()
name := app.RealCommandName(rawName)
Logf(VerbCrazy, "[App.Run] begin run console application, process ID: %d", app.pid)
Logf(VerbDebug, "[App.Run] input command is: %s, real command: %s, args: %v", rawName, name, args)
if !app.IsCommand(name) {
color.Error.Prompt("unknown input command '%s'", name)
ns := app.findSimilarCmd(name)
if len(ns) > 0 {
fmt.Println("\nMaybe you mean:\n ", color.Green.Render(strings.Join(ns, ", ")))
}
fmt.Printf("\nUse \"%s\" to see available commands\n", color.Cyan.Render(app.binName+" -h"))
Exit(ERR)
}
cmd := app.commands[name]
app.commandName = name
if app.Strict {
args = strictFormatArgs(args)
}
// fmt.Println(cmd.argsStr, len(cmd.argsStr), strings.LastIndex(cmd.argsStr, " -h"))
app.fireEvent(EvtBefore, cmd.Copy())
// parse args, don't contains command name.
if !cmd.CustomFlags {
if CLI.hasHelpKeywords() { // contains keywords "-h" OR "--help"
cmd.ShowHelp(true)
}
if err := cmd.Flags.Parse(args); err != nil {
exitWithErr("Flags parse error: %s", err.Error())
}
args = cmd.Flags.Args()
}
Logf(VerbDebug, "[App.Run] args for the command '%s': %v", name, args)
// do execute command
err := cmd.Execute(args)
exitCode := 0
if err != nil {
exitCode = ERR
app.fireEvent(EvtError, err)
} else {
app.fireEvent(EvtAfter, nil)
}
Logf(VerbDebug, "[App.Run] command %s run complete, exit with code: %d", name, exitCode)
Exit(exitCode)
}
// Exec running other command in current command
func (app *App) Exec(name string, args []string) (err error) {
if !app.IsCommand(name) {
color.Error.Prompt("unknown command name '%s'", name)
return
}
cmd := app.commands[name]
if !cmd.CustomFlags {
// parse args, don't contains command name.
if err = cmd.Flags.Parse(args); err != nil {
color.Error.Prompt("Flags parse error: %s", err.Error())
return
}
args = cmd.Flags.Args()
}
// do execute command
return cmd.Execute(args)
}
// IsCommand name check
func (app *App) IsCommand(name string) bool {
_, has := app.names[name]
return has
}
// CommandName get current command name
func (app *App) CommandName() string {
return app.commandName
}
// CommandNames get all command names
func (app *App) CommandNames() []string {
var ss []string
for n := range app.names {
ss = append(ss, n)
}
return ss
}
// AddAliases add alias names for a command
func (app *App) AddAliases(command string, names []string) {
if app.aliases == nil {
app.aliases = make(map[string]string)
}
// add alias
for _, alias := range names {
if cmd, has := app.aliases[alias]; has {
exitWithErr("The alias '%s' has been used by command '%s'", alias, cmd)
}
app.aliases[alias] = command
}
}
// RealCommandName get real command name by alias
func (app *App) RealCommandName(alias string) string {
if name, has := app.aliases[alias]; has {
return name
}
return alias
}
/*************************************************************
* display app help
*************************************************************/
// help template for all commands
var commandsHelp = `{{.Description}} (Version: <info>{{.Version}}</>)
<comment>Usage:</>
{$binName} [Global Options...] <info>{command}</> [--option ...] [argument ...]
<comment>Global Options:</>
<info>--verbose</> Set error reporting level(quiet 0 - 4 debug)
<info>--no-color</> Disable color when outputting message
<info>-h, --help</> Display the help information
<info>-V, --version</> Display app version information
<comment>Available Commands:</>{{range $module, $cs := .Cs}}
<comment>{{ $module }}</>{{ range $cs }}
<info>{{.Name | paddingName }}</> {{.UseFor}}{{if .Aliases}} (alias: <cyan>{{ join .Aliases ","}}</>){{end}}{{end}}{{end}}
<info>{{ paddingName "help" }}</> Display help information
Use "<cyan>{$binName} {command} -h</>" for more information about a command
`
// display app version info
func (app *App) showVersionInfo() {
fmt.Printf(
"%s\n\nVersion: %s\n",
strutil.UpperFirst(app.Description),
color.ApplyTag("cyan", app.Version),
)
if app.Logo.Text != "" {
fmt.Printf("%s\n", color.ApplyTag(app.Logo.Style, app.Logo.Text))
}
Exit(OK)
}
// display app commands help
func (app *App) showCommandsHelp() {
commandsHelp = color.ReplaceTag(commandsHelp)
// render help text template
s := helper.RenderText(commandsHelp, map[string]interface{}{
"Cs": app.moduleCommands,
// app version
"Version": app.Version,
// always upper first char
"Description": strutil.UpperFirst(app.Description),
}, template.FuncMap{
"paddingName": func(n string) string {
return strutil.PadRight(n, " ", app.nameMaxLength)
},
})
// parse help vars and render color tags
fmt.Print(color.String(app.ReplaceVars(s)))
Exit(OK)
}
// showCommandHelp display help for an command
func (app *App) showCommandHelp(list []string, quit bool) {
if len(list) != 1 {
exitWithErr("Too many arguments given.\n\nUsage: %s help COMMAND", app.binName)
}
// get real name
name := app.RealCommandName(list[0])
if name == HelpCommand || name == "-h" {
fmt.Printf("Display help message for application or command.\n\n")
fmt.Printf("Usage: %s help COMMAND\n", app.binName)
Exit(0)
}
cmd, exist := app.commands[name]
if !exist {
color.Error.Prompt("Unknown command name %#q. Run '%s -h'", name, app.binName)
Exit(ERR)
}
// show help for the command.
cmd.ShowHelp(quit)
}
// show bash/zsh completion
func (app *App) showCompletion(args []string) {
}
// findSimilarCmd find similar cmd by input string
func (app *App) findSimilarCmd(input string) []string {
var ss []string
// ins := strings.Split(input, "")
// fmt.Print(input, ins)
ln := len(input)
names := app.Names()
names["help"] = 4 // add 'help' command
// find from command names
for name := range names {
cln := len(name)
if cln > ln && strings.Contains(name, input) {
ss = append(ss, name)
} else if ln > cln && strings.Contains(input, name) {
// sns := strings.Split(str, "")
ss = append(ss, name)
}
// max find 5 items
if len(ss) == 5 {
break
}
}
// find from aliases
for alias := range app.aliases {
// max find 5 items
if len(ss) >= 5 {
break
}
cln := len(alias)
if cln > ln && strings.Contains(alias, input) {
ss = append(ss, alias)
} else if ln > cln && strings.Contains(input, alias) {
ss = append(ss, alias)
}
}
return ss
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。