代码拉取完成,页面将自动刷新
package cmd
import (
"errors"
"regexp"
"strings"
"github.com/urfave/cli"
)
type commandName = string
var commandMap map[commandName]parsedCommand
func init() {
commandMap = make(map[commandName]parsedCommand)
}
func Add(c Commander) {
cmdName, argList := parseCommand(c.Command())
commandMap[cmdName] = parsedCommand{
c,
cmdName,
argList,
}
}
func getParsedCommand(name commandName) (*parsedCommand, error) {
pcmd, ok := commandMap[name]
if !ok {
return nil, errors.New("command not found")
}
return &pcmd, nil
}
func parseCommand(commandWithArgs string) (name commandName, argList []string) {
tmp := strings.SplitN(commandWithArgs, " ", 2)
name = tmp[0]
const reg = `(?m).*?(\s\{(.*?)\})`
re := regexp.MustCompile(reg)
for _, match := range re.FindAllStringSubmatch(commandWithArgs, -1) {
if len(match) > 2 {
argList = append(argList, match[2])
}
}
return
}
func parseArgData(commandWithArgData string) (name commandName, argData []string) {
tmp := strings.Split(commandWithArgData, " ")
if len(tmp) > 0 {
name = tmp[0]
}
argData = tmp[1:]
return
}
func List() (cmdList []cli.Command) {
for _, v := range commandMap {
cmdList = append(cmdList, cli.Command{
Category: v.commandCategory(),
Name: v.Name(),
//Aliases: c.Aliases(),
Usage: v.Description(),
Action: func(_c *cli.Context) error {
c, err := getParsedCommand(_c.Command.Name)
if err != nil {
return err
}
if !_c.Args().Present() {
return c.Handler(newArg(nil))
}
argMap := c.mapArg(_c.Args())
return c.Handler(newArg(&argMap))
},
ArgsUsage: v.argUsage(),
})
}
return
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。