kong icon indicating copy to clipboard operation
kong copied to clipboard

Add --help-all (or equivalent) for advanced help

Open virtuald opened this issue 3 years ago • 2 comments

I'm not a huge fan of expanding subcommands by default for a large CLI tree (so I set NoExpandSubcommands to true)-- however, it seems like a good "advanced" option to provide if a user really wants all of their options.

So maybe you have normal help (NoExpandSubcommands is true):

$ go run main.go --help
Usage: app <command>

complicated app

Flags:
  -h, --help    Show context-sensitive help.

Commands:
  a    Command A
  b    Command B

Run "app <command> --help" for more information on a command.

And then (NoExpandSubcommands is still true) advanced help:

$ go run main.go --help-all
Usage: app <command>

complicated app

Flags:
  -h, --help    Show context-sensitive help.

Commands:
  a sub-cmd-a    Subcommand A
  a sub-cmd-b    Subcommand B
  b sub-cmd-a    Subcommand A
  b sub-cmd-b    Subcommand B

Run "app <command> --help" for more information on a command.

virtuald avatar Jul 20 '21 02:07 virtuald

You should be able to do this yourself. Create a type HelpAllFlag bool that implements BeforeApply() similar to kong.VersionFlag, then you should be able to call DefaultHelpPrinter with the appropriate options.

alecthomas avatar Jul 20 '21 03:07 alecthomas

Neat, thanks for the tip.

package main

import (
	"github.com/alecthomas/kong"
)

type subcmd struct{}

type A struct {
	SubCmdA subcmd `kong:"cmd,help='Subcommand A'"`
	SubCmdB subcmd `kong:"cmd,help='Subcommand B'"`
}

type B struct {
	SubCmdA subcmd `kong:"cmd,help='Subcommand A'"`
	SubCmdB subcmd `kong:"cmd,help='Subcommand B'"`
}

type CLI struct {
	A A `kong:"cmd,help='Command A'"`
	B B `kong:"cmd,help='Command B'"`

	HelpAll HelpAllFlag `kong:"help='Advanced help'"`
}

type HelpAllFlag bool

func (HelpAllFlag) BeforeApply(ctx *kong.Context) error {
	err := kong.DefaultHelpPrinter(kong.HelpOptions{
		Compact:             true,
		NoExpandSubcommands: false,
	}, ctx)
	if err != nil {
		return err
	}
	ctx.Kong.Exit(0)
	return nil
}

func main() {
	ctx := kong.Parse(&CLI{},
		kong.Name("app"),
		kong.Description("complicated app"),
		kong.UsageOnError(),
		kong.ConfigureHelp(kong.HelpOptions{
			Compact:             true,
			NoExpandSubcommands: true,
		}),
	)
	err := ctx.Run()
	ctx.FatalIfErrorf(err)
}

Would be nice if Kong had it too, but obviously not necessary. Feel free to close this if you don't think kong needs it.

virtuald avatar Jul 20 '21 03:07 virtuald