From a6e43479f0d53a34110f93f2a9ab6f45475456e5 Mon Sep 17 00:00:00 2001 From: Oleg Kovalov Date: Thu, 28 Oct 2021 21:19:32 +0200 Subject: [PATCH] Add help and version cmds to list of commands --- acmd.go | 27 +++++++++++++++++++-------- example_test.go | 8 +++++--- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/acmd.go b/acmd.go index 15d7771..6ee2fbd 100644 --- a/acmd.go +++ b/acmd.go @@ -115,6 +115,25 @@ func (r *Runner) init() error { names[cmd.Name] = struct{}{} } + r.cmds = append(r.cmds, + Command{ + Name: "help", + Description: "shows help message", + Do: func(ctx context.Context, args []string) error { + r.cfg.Usage(r.cfg, r.cmds) + return nil + }, + }, + Command{ + Name: "version", + Description: "shows version of the application", + Do: func(ctx context.Context, args []string) error { + fmt.Fprintf(r.cfg.Output, "%s version: %s\n\n", r.cfg.AppName, r.cfg.Version) + return nil + }, + }, + ) + sort.Slice(r.cmds, func(i, j int) bool { return r.cmds[i].Name < r.cmds[j].Name }) @@ -134,14 +153,6 @@ func (r *Runner) Run() error { func (r *Runner) run() error { cmd, params := r.args[0], r.args[1:] - switch { - case cmd == "help": - r.cfg.Usage(r.cfg, r.cmds) - return nil - case cmd == "version": - fmt.Fprintf(r.cfg.Output, "%s version: %s\n\n", r.cfg.AppName, r.cfg.Version) - return nil - } for _, c := range r.cmds { if c.Name == cmd { diff --git a/example_test.go b/example_test.go index a1320e5..f0eefc7 100644 --- a/example_test.go +++ b/example_test.go @@ -96,9 +96,11 @@ func ExampleHelp() { // // The commands are: // - // boom bombom - // now prints current time - // status prints status of the system + // boom bombom + // help shows help message + // now prints current time + // status prints status of the system + // version shows version of the application // // Version: the best v0.x.y }