Skip to content

Commit

Permalink
feat: xcmd alias support
Browse files Browse the repository at this point in the history
  • Loading branch information
hui.wang committed Feb 21, 2022
1 parent 78905db commit bd2430d
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 2 deletions.
5 changes: 3 additions & 2 deletions xcmd/commander.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ func (c *Command) Execute(ctx context.Context, args ...string) error {
// 以index=0的元素作为命令名尝试寻找subcommand
argFirst = args[0]
for _, cmd := range c.commands {
if cmd.Name() != argFirst {
if cmd.Name() != argFirst && !xutil.ContainString(cmd.cc.Alias, argFirst) {
continue
}
return cmd.Execute(ctx, args[1:]...)
Expand Down Expand Up @@ -271,7 +271,8 @@ func (c *Command) Execute(ctx context.Context, args ...string) error {
executerMiddleware = append(executerMiddleware, c.executerMiddleware...)
} else {
execUsing = func(ctx context.Context, cmd *Command) error {
return usageExecuter(context.Background(), cmd)
// 依赖flagset的usage打印help信息,不再主动打印
return nil
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions xcmd/explain.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ func explainGroup(w io.Writer, c *Command) {
fmt.Fprintf(w, "USAGE: \n%s%s <subcommand> <flags> <args>\n\n", PaddingContent, strings.Join(c.usageNamePath, " "))
}

if len(c.cc.Alias) != 0 {
fmt.Fprintf(w, "ALIAS: \n%s%s\n\n", PaddingContent, strings.Join(c.cc.Alias, ","))
}

paragraph(w, "DEPRECATED", c.cc.Deprecated)
paragraph(w, "DESCRIPTION", c.cc.Description)
paragraph(w, "EXAMPLES", c.cc.Examples)
Expand Down
14 changes: 14 additions & 0 deletions xcmd/gen_config_optiongen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions xcmd/main/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func main() {
return next(ctx, c)
})
cmdExport := xcmd.SubCommand("export", // 添加一个子命令
xcmd.WithAlias("gen", "cc"),
xcmd.WithShort("export proto to golang/cs/python/lua"),
xcmd.WithDescription(`详细描述一下,在执行: export --help时会显示该消息,并可以换行,内部会自动格式化`),
xcmd.WithExamples(`- 只设定http: export --http_address=10.0.0.1
Expand Down
2 changes: 2 additions & 0 deletions xcmd/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package xcmd

import (
"context"
"fmt"

"github.com/sandwich-go/xconf"
)
Expand All @@ -10,6 +11,7 @@ import (
type Executer = func(ctx context.Context, cmd *Command) error

var usageExecuter = func(ctx context.Context, cmd *Command) error {
fmt.Println("usageExecuterusageExecuterusageExecuterusageExecuterusageExecuterusageExecuterusageExecuterusageExecuterusageExecuter")
cmd.Usage()
return xconf.ErrHelp
}
Expand Down
2 changes: 2 additions & 0 deletions xcmd/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ func configOptionDeclareWithDefault() interface{} {
"Deprecated": "",
// annotation@Author(comment="命令作者联系信息,只用于显示")
"Author": []string{},
// annotation@Alias(comment="alias command")
"Alias": []string{},
}
}

Expand Down

0 comments on commit bd2430d

Please sign in to comment.