diff --git a/xcmd/commander.go b/xcmd/commander.go index 153b453..e9a80f8 100644 --- a/xcmd/commander.go +++ b/xcmd/commander.go @@ -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:]...) @@ -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 } } } diff --git a/xcmd/explain.go b/xcmd/explain.go index 27abe03..899e424 100644 --- a/xcmd/explain.go +++ b/xcmd/explain.go @@ -86,6 +86,10 @@ func explainGroup(w io.Writer, c *Command) { fmt.Fprintf(w, "USAGE: \n%s%s \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) diff --git a/xcmd/gen_config_optiongen.go b/xcmd/gen_config_optiongen.go index 64efccf..9611731 100644 --- a/xcmd/gen_config_optiongen.go +++ b/xcmd/gen_config_optiongen.go @@ -30,6 +30,8 @@ type config struct { Deprecated string // annotation@Author(comment="命令作者联系信息,只用于显示") Author []string + // annotation@Alias(comment="alias command") + Alias []string } // NewConfig new config @@ -140,6 +142,15 @@ func WithAuthor(v ...string) ConfigOption { } } +// WithAlias alias command +func WithAlias(v ...string) ConfigOption { + return func(cc *config) ConfigOption { + previous := cc.Alias + cc.Alias = v + return WithAlias(previous...) + } +} + // InstallConfigWatchDog the installed func will called when NewConfig called func InstallConfigWatchDog(dog func(cc *config)) { watchDogConfig = dog } @@ -160,6 +171,7 @@ func newDefaultConfig() *config { WithOutput(os.Stdout), WithDeprecated(""), WithAuthor(make([]string, 0)...), + WithAlias(make([]string, 0)...), } { opt(cc) } @@ -177,6 +189,7 @@ func (cc *config) GetSuggestionsMinDistance() int { return cc.SuggestionsMinDist func (cc *config) GetOutput() io.Writer { return cc.Output } func (cc *config) GetDeprecated() string { return cc.Deprecated } func (cc *config) GetAuthor() []string { return cc.Author } +func (cc *config) GetAlias() []string { return cc.Alias } // ConfigVisitor visitor interface for config type ConfigVisitor interface { @@ -189,6 +202,7 @@ type ConfigVisitor interface { GetOutput() io.Writer GetDeprecated() string GetAuthor() []string + GetAlias() []string } // ConfigInterface visitor + ApplyOption interface for config diff --git a/xcmd/main/main.go b/xcmd/main/main.go index ff2a823..4a9228a 100644 --- a/xcmd/main/main.go +++ b/xcmd/main/main.go @@ -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 diff --git a/xcmd/middleware.go b/xcmd/middleware.go index 29ba617..cbd375a 100644 --- a/xcmd/middleware.go +++ b/xcmd/middleware.go @@ -2,6 +2,7 @@ package xcmd import ( "context" + "fmt" "github.com/sandwich-go/xconf" ) @@ -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 } diff --git a/xcmd/option.go b/xcmd/option.go index 769549a..7e9a11a 100644 --- a/xcmd/option.go +++ b/xcmd/option.go @@ -36,6 +36,8 @@ func configOptionDeclareWithDefault() interface{} { "Deprecated": "", // annotation@Author(comment="命令作者联系信息,只用于显示") "Author": []string{}, + // annotation@Alias(comment="alias command") + "Alias": []string{}, } }