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

Show subcommands help #44

Merged
merged 1 commit into from Nov 29, 2022
Merged
Show file tree
Hide file tree
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
47 changes: 33 additions & 14 deletions acmd.go
Expand Up @@ -374,25 +374,44 @@ 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 {
if cmd.IsHidden {

if len(cmd.Subcommands) == 0 {
printCommand(cfg, tw, "", cmd)
continue
}

desc := cmd.Description
if desc == "" {
desc = "<no description>"
}
fmt.Fprintf(tw, " %s\t%s\n", cmd.Name, desc)

if cfg.VerboseHelp && cmd.FlagSet != nil {
fset := cmd.FlagSet.Flags()
old := fset.Output()
fmt.Fprintf(tw, " ")
fset.SetOutput(tw)
fset.Usage()
fset.SetOutput(old)
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

if cmd.IsHidden {
return
}
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 {
fset := cmd.FlagSet.Flags()
old := fset.Output()
fmt.Fprintf(tw, " ")
fset.SetOutput(tw)
fset.Usage()
fset.SetOutput(old)
}
}
32 changes: 32 additions & 0 deletions acmd_test.go
Expand Up @@ -391,6 +391,38 @@ func TestExit(t *testing.T) {
mustEqual(t, buf.String(), wantOutput)
}

func TestCommand_Help_SubCommands(t *testing.T) {
buf := &bytes.Buffer{}
cmds := []Command{
{Name: "cmd", Subcommands: []Command{
{Name: "sub1", ExecFunc: nopFunc, Description: "show sub1"},
{Name: "sub2", ExecFunc: nopFunc, Description: "show sub2"},
}},
}
r := RunnerOf(cmds, Config{
Args: []string{"./myapp", "help"},
AppName: "myapp",
Output: buf,
})
failIfErr(t, r.Run())

if !strings.Contains(buf.String(), "cmd sub1") {
t.Fatal("should show subcommand help")
}

if !strings.Contains(buf.String(), "cmd sub2") {
t.Fatal("should show subcommand help")
}

if !strings.Contains(buf.String(), "show sub1") {
t.Fatal("should show subcommand help")
}

if !strings.Contains(buf.String(), "show sub2") {
t.Fatal("should show subcommand help")
}
}

func failIfOk(t testing.TB, err error) {
t.Helper()
if err == nil {
Expand Down