Skip to content

Commit

Permalink
Add help and version cmds to list of commands
Browse files Browse the repository at this point in the history
  • Loading branch information
cristaloleg committed Oct 28, 2021
1 parent 84fb5e2 commit a6e4347
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
27 changes: 19 additions & 8 deletions acmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
Expand All @@ -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 {
Expand Down
8 changes: 5 additions & 3 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit a6e4347

Please sign in to comment.