From 9720ac029c97c01ec70333b4a7f2aa6dc04536b9 Mon Sep 17 00:00:00 2001 From: Michael Schuett Date: Sun, 27 Jan 2019 15:48:00 -0500 Subject: [PATCH] Basic working flag category support --- app.go | 17 ++++++++++------- command.go | 7 +++++++ help.go | 10 +++++++--- 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/app.go b/app.go index 677ba02f1d..94834820cb 100644 --- a/app.go +++ b/app.go @@ -145,6 +145,14 @@ func (a *App) Setup() { if c.HelpName == "" { c.HelpName = fmt.Sprintf("%s %s", a.HelpName, c.Name) } + + fc := FlagCategories{} + for _, flag := range c.Flags { + fc = fc.AddFlag(flag.GetCategory(), flag) + } + + sort.Sort(fc) + c.FlagCategories = fc newCmds = append(newCmds, c) } a.Commands = newCmds @@ -166,7 +174,7 @@ func (a *App) Setup() { } sort.Sort(a.categories) -if a.Metadata == nil { + if a.Metadata == nil { a.Metadata = make(map[string]interface{}) } @@ -194,11 +202,6 @@ func (a *App) Run(arguments []string) (err error) { return err } - a.flagCategories = FlagCategories{} - for _, flag := range a.Flags { - a.flagCategories = a.flagCategories.AddFlag(flag.GetCategory(), flag) - } - set.SetOutput(ioutil.Discard) err = set.Parse(arguments[1:]) nerr := normalizeFlags(a.Flags, set) @@ -445,7 +448,7 @@ func (a *App) VisibleCommands() []Command { } // Categories returns a slice containing all the categories with the commands they contain -func (a *App) FlagCategories() FlagCategories { +func (a *App) VisibleFlagCategories() FlagCategories { return a.flagCategories } diff --git a/command.go b/command.go index 5bb2adaa04..1e6ee9f3e7 100644 --- a/command.go +++ b/command.go @@ -45,6 +45,8 @@ type Command struct { Subcommands Commands // List of flags to parse Flags []Flag + // List of all flag categories + FlagCategories FlagCategories // Treat all flags as normal arguments if true SkipFlagParsing bool // Skip argument reordering which attempts to move flags before arguments, @@ -377,6 +379,11 @@ func (c Command) startApp(ctx *Context) error { return app.RunAsSubcommand(ctx) } +// Categories returns a slice containing all the categories with the commands they contain +func (c Command) VisibleFlagCategories() FlagCategories { + return c.FlagCategories +} + // VisibleFlags returns a slice of the Flags with Hidden=false func (c Command) VisibleFlags() []Flag { return visibleFlags(c.Flags) diff --git a/help.go b/help.go index 65874fa2fc..18e185ef19 100644 --- a/help.go +++ b/help.go @@ -54,9 +54,10 @@ CATEGORY: {{.Category}}{{end}}{{if .Description}} DESCRIPTION: - {{.Description}}{{end}}{{if .VisibleFlags}} + {{.Description}}{{end}} -OPTIONS: +OPTIONS:{{range .VisibleFlagCategories}} + {{.Name}} {{range .VisibleFlags}}{{.}} {{end}}{{end}} ` @@ -250,7 +251,10 @@ func printHelpCustom(out io.Writer, templ string, data interface{}, customFunc m // If the writer is closed, t.Execute will fail, and there's nothing // we can do to recover. if os.Getenv("CLI_TEMPLATE_ERROR_DEBUG") != "" { - fmt.Fprintf(ErrWriter, "CLI TEMPLATE ERROR: %#v\n", err) + // Generic error message + fmt.Fprintf(ErrWriter, "CLI TEMPLATE ERROR DEBUG: %#v\n", err) + // Helpful error message + fmt.Fprintf(ErrWriter, "CLI TEMPLATE ERROR DEBUG: %#v\n", err.Error()) } return }