@@ -34,7 +34,6 @@ var Version = "DEV"
3434var BuildDate = "" // YYYY-MM-DD
3535
3636var versionOutput = ""
37- var cobraDefaultHelpFunc func (* cobra.Command , []string )
3837
3938func init () {
4039 if Version == "DEV" {
@@ -58,9 +57,11 @@ func init() {
5857 // TODO:
5958 // RootCmd.PersistentFlags().BoolP("verbose", "V", false, "enable verbose output")
6059
61- cobraDefaultHelpFunc = RootCmd .HelpFunc ()
6260 RootCmd .SetHelpFunc (rootHelpFunc )
6361
62+ // This will silence the usage func on error
63+ RootCmd .SetUsageFunc (func (_ * cobra.Command ) error { return nil })
64+
6465 RootCmd .SetFlagErrorFunc (func (cmd * cobra.Command , err error ) error {
6566 if err == pflag .ErrHelp {
6667 return err
@@ -95,6 +96,13 @@ var RootCmd = &cobra.Command{
9596
9697 SilenceErrors : true ,
9798 SilenceUsage : true ,
99+ Example : `$ gh issue create
100+ $ gh repo clone
101+ $ gh pr checkout 321` ,
102+ Annotations : map [string ]string {
103+ "help:feedback" : `
104+ Fill out our feedback form https://forms.gle/umxd3h31c7aMQFKG7
105+ Open an issue using “gh issue create -R cli/cli”` },
98106}
99107
100108var versionCmd = & cobra.Command {
@@ -320,100 +328,6 @@ func determineBaseRepo(apiClient *api.Client, cmd *cobra.Command, ctx context.Co
320328 return baseRepo , nil
321329}
322330
323- func rootHelpFunc (command * cobra.Command , args []string ) {
324- if command != RootCmd {
325- // Display helpful error message in case subcommand name was mistyped.
326- // This matches Cobra's behavior for root command, which Cobra
327- // confusingly doesn't apply to nested commands.
328- if command .Parent () == RootCmd && len (args ) >= 2 {
329- if command .SuggestionsMinimumDistance <= 0 {
330- command .SuggestionsMinimumDistance = 2
331- }
332- candidates := command .SuggestionsFor (args [1 ])
333-
334- errOut := command .OutOrStderr ()
335- fmt .Fprintf (errOut , "unknown command %q for %q\n " , args [1 ], "gh " + args [0 ])
336-
337- if len (candidates ) > 0 {
338- fmt .Fprint (errOut , "\n Did you mean this?\n " )
339- for _ , c := range candidates {
340- fmt .Fprintf (errOut , "\t %s\n " , c )
341- }
342- fmt .Fprint (errOut , "\n " )
343- }
344-
345- oldOut := command .OutOrStdout ()
346- command .SetOut (errOut )
347- defer command .SetOut (oldOut )
348- }
349- cobraDefaultHelpFunc (command , args )
350- return
351- }
352-
353- type helpEntry struct {
354- Title string
355- Body string
356- }
357-
358- coreCommandNames := []string {"issue" , "pr" , "repo" }
359- var coreCommands []string
360- var additionalCommands []string
361- for _ , c := range command .Commands () {
362- if c .Short == "" {
363- continue
364- }
365- s := " " + rpad (c .Name ()+ ":" , c .NamePadding ()) + c .Short
366- if includes (coreCommandNames , c .Name ()) {
367- coreCommands = append (coreCommands , s )
368- } else if ! c .Hidden {
369- additionalCommands = append (additionalCommands , s )
370- }
371- }
372-
373- helpEntries := []helpEntry {
374- {
375- "" ,
376- command .Long },
377- {"USAGE" , command .Use },
378- {"CORE COMMANDS" , strings .Join (coreCommands , "\n " )},
379- {"ADDITIONAL COMMANDS" , strings .Join (additionalCommands , "\n " )},
380- {"FLAGS" , strings .TrimRight (command .LocalFlags ().FlagUsages (), "\n " )},
381- {"EXAMPLES" , `
382- $ gh issue create
383- $ gh repo clone
384- $ gh pr checkout 321` },
385- {"LEARN MORE" , `
386- Use "gh <command> <subcommand> --help" for more information about a command.
387- Read the manual at http://cli.github.com/manual` },
388- {"FEEDBACK" , `
389- Fill out our feedback form https://forms.gle/umxd3h31c7aMQFKG7
390- Open an issue using “gh issue create -R cli/cli”` },
391- }
392-
393- out := colorableOut (command )
394- for _ , e := range helpEntries {
395- if e .Title != "" {
396- fmt .Fprintln (out , utils .Bold (e .Title ))
397- }
398- fmt .Fprintln (out , strings .TrimLeft (e .Body , "\n " )+ "\n " )
399- }
400- }
401-
402- // rpad adds padding to the right of a string.
403- func rpad (s string , padding int ) string {
404- template := fmt .Sprintf ("%%-%ds " , padding )
405- return fmt .Sprintf (template , s )
406- }
407-
408- func includes (a []string , s string ) bool {
409- for _ , x := range a {
410- if x == s {
411- return true
412- }
413- }
414- return false
415- }
416-
417331func formatRemoteURL (cmd * cobra.Command , fullRepoName string ) string {
418332 ctx := contextForCommand (cmd )
419333
0 commit comments