Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor help #46

Merged
merged 1 commit into from Nov 29, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 10 additions & 12 deletions acmd.go
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