Merge pull request #916 from cli/cobra1.0

Upgrade to Cobra 1.0
This commit is contained in:
Mislav Marohnić 2020-06-02 10:34:04 +02:00 committed by GitHub
commit bfc3e4e43d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 30 additions and 178 deletions

View file

@ -5,7 +5,6 @@ import (
"fmt"
"os"
"github.com/cli/cli/internal/cobrafish"
"github.com/cli/cli/utils"
"github.com/spf13/cobra"
)
@ -58,7 +57,7 @@ Homebrew, see <https://docs.brew.sh/Shell-Completion>
case "powershell":
return RootCmd.GenPowerShellCompletion(cmd.OutOrStdout())
case "fish":
return cobrafish.GenCompletion(RootCmd, cmd.OutOrStdout())
return RootCmd.GenFishCompletion(cmd.OutOrStdout(), true)
default:
return fmt.Errorf("unsupported shell type %q", shellType)
}

View file

@ -336,9 +336,33 @@ func determineBaseRepo(apiClient *api.Client, cmd *cobra.Command, ctx context.Co
return baseRepo, nil
}
func rootHelpFunc(command *cobra.Command, s []string) {
func rootHelpFunc(command *cobra.Command, args []string) {
if command != RootCmd {
cobraDefaultHelpFunc(command, s)
// Display helpful error message in case subcommand name was mistyped.
// This matches Cobra's behavior for root command, which Cobra
// confusingly doesn't apply to nested commands.
if command.Parent() == RootCmd && len(args) >= 2 {
if command.SuggestionsMinimumDistance <= 0 {
command.SuggestionsMinimumDistance = 2
}
candidates := command.SuggestionsFor(args[1])
errOut := command.OutOrStderr()
fmt.Fprintf(errOut, "unknown command %q for %q\n", args[1], "gh "+args[0])
if len(candidates) > 0 {
fmt.Fprint(errOut, "\nDid you mean this?\n")
for _, c := range candidates {
fmt.Fprintf(errOut, "\t%s\n", c)
}
fmt.Fprint(errOut, "\n")
}
oldOut := command.OutOrStdout()
command.SetOut(errOut)
defer command.SetOut(oldOut)
}
cobraDefaultHelpFunc(command, args)
return
}