diff --git a/commands/root.go b/commands/root.go index ebc7615..2f9b491 100644 --- a/commands/root.go +++ b/commands/root.go @@ -76,7 +76,7 @@ func newRootCommand(cnf *config.Config, assets *vendorization.VendorAssets) *cob }() } }, - Run: func(cmd *cobra.Command, _ []string) { + Run: func(cmd *cobra.Command, args []string) { c := &legacy.CLIWrapper{ Config: cnf, Version: config.Version, @@ -93,7 +93,7 @@ func newRootCommand(cnf *config.Config, assets *vendorization.VendorAssets) *cob return } - if err := c.Exec(cmd.Context(), os.Args[1:]...); err != nil { + if err := c.Exec(cmd.Context(), args...); err != nil { debugLog("%s\n", color.RedString(err.Error())) exitCode := 1 var execErr *exec.ExitError @@ -113,16 +113,22 @@ func newRootCommand(cnf *config.Config, assets *vendorization.VendorAssets) *cob }, } - cmd.SetHelpFunc(func(cmd *cobra.Command, args []string) { - if cmd.Context() == nil { - cmd.SetContext(context.Background()) + cmd.SetHelpFunc(func(innerCmd *cobra.Command, args []string) { + if innerCmd.Use != cmd.Use { + // For real (Cobra) commands, print the usage string. + innerCmd.Println(innerCmd.UsageString()) + return } + // Others will be passed to the legacy CLI's help command. if !slices.Contains(args, "--help") && !slices.Contains(args, "-h") { args = append([]string{"help"}, args...) } + if len(args) == 1 && (args[0] == "-h" || args[0] == "--help") { + args = []string{"help"} + } - cmd.Run(cmd, args) + cmd.Run(innerCmd, args) }) cmd.PersistentFlags().BoolP("version", "V", false, fmt.Sprintf("Displays the %s version", cnf.Application.Name))