From fef0a55288eaa48a61559b42a3bfb8f71f2881b4 Mon Sep 17 00:00:00 2001 From: Roberto Hidalgo Date: Fri, 24 Mar 2023 00:44:47 -0600 Subject: [PATCH] remove version from global options --- internal/commands/help.go | 10 ++++------ internal/registry/registry.go | 32 +++++++++++++++++++++----------- pkg/command/command.go | 1 - pkg/command/root.go | 24 ++++++++++++------------ 4 files changed, 37 insertions(+), 30 deletions(-) diff --git a/internal/commands/help.go b/internal/commands/help.go index 390b6b6..46d6c3d 100644 --- a/internal/commands/help.go +++ b/internal/commands/help.go @@ -9,7 +9,6 @@ import ( _c "git.rob.mx/nidito/chinampa/internal/constants" "git.rob.mx/nidito/chinampa/pkg/env" - "git.rob.mx/nidito/chinampa/pkg/runtime" "git.rob.mx/nidito/chinampa/pkg/statuscode" "github.com/sirupsen/logrus" "github.com/spf13/cobra" @@ -24,13 +23,13 @@ func cobraCommandFullName(c *cobra.Command) []string { } var Help = &cobra.Command{ - Use: _c.HelpCommandName + " [command]", + Use: _c.HelpCommandName, Short: "Display usage information for any command", - Long: `Help provides the valid arguments and options for any command known to ﹅@chinampa@﹅. + Long: `Provides documentation for any command known to ﹅@chinampa@﹅, including its known arguments and options. -## Colorized output +### Colorized output -By default, and unless ﹅` + env.NoColor + `﹅ is set, ﹅@chinampa@ help﹅ will query the environment variable ﹅COLORFGBG﹅ to decide which style to use when rendering help, unless if ﹅` + env.HelpStyle + `﹅ is set to any of the following values: **light**, **dark**, **markdown**, and **auto**. 24-bit color is available when ﹅COLORTERM﹅ is set to ﹅truecolor﹅.`, +By default, and unless ﹅` + env.NoColor + `﹅ is set, ﹅@chinampa@ help﹅ will query the environment variable ﹅COLORFGBG﹅ to decide which style to use when rendering help, unless if ﹅` + env.HelpStyle + `﹅ is set to any of the following values: **light**, **dark**, **markdown**, or **auto**. 24-bit color is available when ﹅COLORTERM﹅ is set to ﹅truecolor﹅.`, ValidArgsFunction: func(c *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { var completions []string cmd, _, e := c.Root().Find(args) @@ -51,7 +50,6 @@ By default, and unless ﹅` + env.NoColor + `﹅ is set, ﹅@chinampa@ help﹅ w return completions, cobra.ShellCompDirectiveNoFileComp }, Run: func(c *cobra.Command, args []string) { - c.Long = strings.ReplaceAll(c.Long, "@chinampa@", runtime.Executable) if len(args) > 0 && c != nil && c.Name() != args[len(args)-1] { c, topicArgs, err := c.Root().Find(args) if err == nil && c != nil && len(topicArgs) == 0 { diff --git a/internal/registry/registry.go b/internal/registry/registry.go index abcbe45..352ac53 100644 --- a/internal/registry/registry.go +++ b/internal/registry/registry.go @@ -12,8 +12,10 @@ import ( "git.rob.mx/nidito/chinampa/pkg/command" "git.rob.mx/nidito/chinampa/pkg/errors" "git.rob.mx/nidito/chinampa/pkg/logger" + "git.rob.mx/nidito/chinampa/pkg/runtime" "github.com/fatih/color" "github.com/spf13/cobra" + "github.com/spf13/pflag" ) // ContextKeyRuntimeIndex is the string key used to store context in a cobra Command. @@ -65,7 +67,21 @@ func Execute(version string) error { cmdRoot := command.Root ccRoot := newCobraRoot(command.Root) ccRoot.CompletionOptions.HiddenDefaultCmd = true - ccRoot.PersistentFlags().AddFlagSet(cmdRoot.FlagSet()) + globalOptions := command.Options{} + cmdRoot.FlagSet().VisitAll(func(f *pflag.Flag) { + opt := command.Root.Options[f.Name] + if f.Name == "version" { + ccRoot.Flags().AddFlag(f) + } else { + ccRoot.PersistentFlags().AddFlag(f) + globalOptions[f.Name] = opt + } + + if err := ccRoot.RegisterFlagCompletionFunc(f.Name, opt.CompletionFunction); err != nil { + log.Errorf("Failed setting up autocompletion for option <%s> of command <%s>", f.Name, cmdRoot.FullName()) + } + }) + if version != "" { name := commands.VersionCommandName ccRoot.Annotations["version"] = version @@ -75,20 +91,13 @@ func Execute(version string) error { } ccRoot.AddCommand(commands.GenerateCompletions) - for name, opt := range cmdRoot.Options { - if err := ccRoot.RegisterFlagCompletionFunc(name, opt.CompletionFunction); err != nil { - log.Errorf("Failed setting up autocompletion for option <%s> of command <%s>", name, cmdRoot.FullName()) - } - } - - ccRoot.SetHelpFunc(cmdRoot.HelpRenderer(cmdRoot.Options)) - + ccRoot.SetHelpFunc(cmdRoot.HelpRenderer(globalOptions)) for _, cmd := range CommandList() { cmd := cmd container := ccRoot for idx, cp := range cmd.Path { if idx == len(cmd.Path)-1 { - leaf := ToCobra(cmd, cmdRoot.Options) + leaf := ToCobra(cmd, globalOptions) container.AddCommand(leaf) log.Tracef("cobra: %s => %s", leaf.Name(), container.CommandPath()) break @@ -160,7 +169,7 @@ func Execute(version string) error { Options: command.Options{}, } Register(groupParent) - cc.SetHelpFunc(groupParent.HelpRenderer(command.Options{})) + cc.SetHelpFunc(groupParent.HelpRenderer(globalOptions)) cc.SetHelpCommand(commands.Help) container.AddCommand(cc) container = cc @@ -170,6 +179,7 @@ func Execute(version string) error { cmd.Path = append(cmdRoot.Path, cmd.Path...) } cmdRoot.SetCobra(ccRoot) + commands.Help.Long = strings.ReplaceAll(commands.Help.Long, "@chinampa@", runtime.Executable) ccRoot.SetHelpCommand(commands.Help) current, remaining, err := ccRoot.Find(os.Args[1:]) diff --git a/pkg/command/command.go b/pkg/command/command.go index 19c7d45..f00e913 100644 --- a/pkg/command/command.go +++ b/pkg/command/command.go @@ -112,7 +112,6 @@ func (cmd *Command) FlagSet() *pflag.FlagSet { continue } } - cmd.runtimeFlags = fs } return cmd.runtimeFlags diff --git a/pkg/command/root.go b/pkg/command/root.go index 6c9cb5f..6d64c46 100644 --- a/pkg/command/root.go +++ b/pkg/command/root.go @@ -17,26 +17,21 @@ var Root = &Command{ Type: "bool", Description: "Display help for any command", }, - "verbose": &Option{ - ShortName: "v", + "color": &Option{ Type: "bool", - Default: runtime.VerboseEnabled(), - Description: "Log verbose output to stderr", - }, - "version": &Option{ - Type: "bool", - Default: false, - Description: "Display program version and exit", + Description: "Always print colors to stderr", + Default: runtime.ColorEnabled(), }, "no-color": &Option{ Type: "bool", Description: "Disable printing of colors to stderr", Default: !runtime.ColorEnabled(), }, - "color": &Option{ + "verbose": &Option{ + ShortName: "v", Type: "bool", - Description: "Always print colors to stderr", - Default: runtime.ColorEnabled(), + Default: runtime.VerboseEnabled(), + Description: "Log verbose output to stderr", }, "silent": &Option{ Type: "bool", @@ -46,5 +41,10 @@ var Root = &Command{ Type: "bool", Description: "Do not validate any arguments or options", }, + "version": &Option{ + Type: "bool", + Default: false, + Description: "Display program version and exit", + }, }, }