1 Star 0 Fork 0

庞飞/multiapp

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
manager.go 1.59 KB
一键复制 编辑 原始数据 按行查看 历史
庞飞 提交于 2023-10-03 10:28 . 合并主框架
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(),
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
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/pangxianfei/multiapp.git
git@gitee.com:pangxianfei/multiapp.git
pangxianfei
multiapp
multiapp
v1.1.2

搜索帮助