Skip to content

Commit

Permalink
Refactor help (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
cristaloleg authored Nov 29, 2022
1 parent a1e96d9 commit ed4f464
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions acmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ func defaultUsage(r *Runner) func(cfg Config, cmds []Command) {
}

fmt.Fprintf(w, "Usage:\n\n %s <command> [arguments...]\n\nThe commands are:\n\n", cfg.AppName)
printCommands(r.cfg, cmds)
printCommands(&r.cfg, cmds)

if cfg.PostDescription != "" {
fmt.Fprintf(w, "%s\n\n", cfg.PostDescription)
Expand All @@ -370,40 +370,38 @@ func defaultUsage(r *Runner) func(cfg Config, cmds []Command) {
}

// printCommands in a table form (Name and Description)
func printCommands(cfg Config, cmds []Command) {
func printCommands(cfg *Config, cmds []Command) {
minwidth, tabwidth, padding, padchar, flags := 0, 0, 11, byte(' '), uint(0)
tw := tabwriter.NewWriter(cfg.Output, minwidth, tabwidth, padding, padchar, flags)
for _, cmd := range cmds {

for _, cmd := range cmds {
if len(cmd.Subcommands) == 0 {
printCommand(cfg, tw, "", cmd)
continue
}

for _, subcmd := range cmd.Subcommands {
printCommand(cfg, tw, cmd.Name, subcmd)
}

}
fmt.Fprint(tw, "\n")
tw.Flush()
}

func printCommand(cfg Config, tw *tabwriter.Writer, pre string, cmd Command) {
var name string = cmd.Name

func printCommand(cfg *Config, tw *tabwriter.Writer, prefix string, cmd Command) {
if cmd.IsHidden {
return
}

name := cmd.Name
if prefix != "" {
name = fmt.Sprintf("%s %s", prefix, cmd.Name)
}

desc := cmd.Description
if desc == "" {
desc = "<no description>"
}

if pre != "" {
name = fmt.Sprintf("%s %s", pre, cmd.Name)
}

fmt.Fprintf(tw, " %s\t%s\n", name, desc)

if cfg.VerboseHelp && cmd.FlagSet != nil {
Expand Down

0 comments on commit ed4f464

Please sign in to comment.