1 Star 1 Fork 0

go-wena / cli

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
help_command.go 1.03 KB
一键复制 编辑 原始数据 按行查看 历史
k3x 提交于 2021-06-04 02:09 . init
package cli
import (
"fmt"
"strings"
)
func HelpCommand() Option {
validArgsFunction := ValidArgsFunction(func(c *Command, args []string, toComplete string) ([]string, ShellCompDirective) {
var completions []string
cmd, _, e := c.Root().Find(args)
if e != nil {
return nil, ShellCompDirectiveNoFileComp
}
if cmd == nil {
cmd = c.Root()
}
for _, subCmd := range cmd.Commands() {
if subCmd.IsAvailableCommand() {
if strings.HasPrefix(subCmd.Name(), toComplete) {
completions = append(completions, fmt.Sprintf("%s\t%s", subCmd.Name(), subCmd.Short))
}
}
}
return completions, ShellCompDirectiveNoFileComp
})
run := Run(func(c *Command, args []string) {
cmd, _, e := c.Root().Find(args)
if cmd == nil || e != nil {
c.Printf("Unknown help topic %#q\n", args)
CheckErr(c.Root().Usage())
} else {
cmd.InitDefaultHelpFlag()
CheckErr(cmd.Help())
}
})
help := New(Use("help [command]"), run, validArgsFunction)
return OptionApply(func(c *Command) { c.SetHelpCommand(&help.Command) })
}
Go
1
https://gitee.com/go-wena/cli.git
git@gitee.com:go-wena/cli.git
go-wena
cli
cli
v0.0.1

搜索帮助