1
1
package cmd
2
2
3
3
import (
4
- "fmt"
5
4
"strings"
6
5
7
6
"github.com/spf13/cobra"
@@ -12,19 +11,21 @@ import (
12
11
13
12
func configureHelpCommand (rootCmd * cobra.Command , modulesInfo * modules.ModulesInfo ) error {
14
13
// Add information about external modules into help template.
15
- rootCmd .SetUsageTemplate (fmt .Sprintf (usageTemplate , getExternalCommandsString (modulesInfo )))
14
+ cobra .AddTemplateFunc ("title" , util .Bold )
15
+ cobra .AddTemplateFunc ("split" , strings .Split )
16
+ rootCmd .SetUsageTemplate (usageTemplate )
16
17
17
18
internalHelpModule := func (cmdCtx * cmdcontext.CmdCtx , args []string ) error {
18
- fmt .Printf ("internalHelpModule: cmdCtx.CommandName=%s\n " , cmdCtx .CommandName )
19
- fmt .Printf ("internalHelpModule: args: %v\n " , args )
19
+ // fmt.Printf("internalHelpModule: cmdCtx.CommandName=%s\n", cmdCtx.CommandName)
20
+ // fmt.Printf("internalHelpModule: args: %v\n", args)
20
21
if len (args ) == 0 {
21
22
rootCmd .Help ()
22
23
return nil
23
24
}
24
25
25
26
cmd , _ , err := rootCmd .Find (args )
26
- fmt .Printf ("internalHelpModule: cmd.Name=%s\n " , cmd .Name ())
27
- fmt .Printf ("internalHelpModule: cmd.CommandPath=%s\n " , cmd .CommandPath ())
27
+ // fmt.Printf("internalHelpModule: cmd.Name=%s\n", cmd.Name())
28
+ // fmt.Printf("internalHelpModule: cmd.CommandPath=%s\n", cmd.CommandPath())
28
29
if err != nil {
29
30
return err
30
31
}
@@ -48,75 +49,66 @@ func configureHelpCommand(rootCmd *cobra.Command, modulesInfo *modules.ModulesIn
48
49
return nil
49
50
}
50
51
51
- // getExternalCommandString returns a pretty string
52
- // of descriptions for external modules.
53
- func getExternalCommandsString (modulesInfo * modules.ModulesInfo ) string {
54
- str := ""
55
- for path , manifest := range * modulesInfo {
56
- helpMsg , err := modules .GetExternalModuleDescription (manifest )
57
- if err != nil {
58
- helpMsg = "description is absent"
59
- }
60
-
61
- name := strings .Split (path , " " )[1 ]
62
- str = fmt .Sprintf ("%s %s\t %s\n " , str , name , helpMsg )
63
- }
64
-
65
- if str != "" {
66
- str = util .Bold ("\n EXTERNAL COMMANDS\n " ) + str
67
- return strings .Trim (str , "\n " )
68
- }
69
-
70
- return ""
71
- }
72
-
73
52
var (
74
- usageTemplate = util . Bold ( "USAGE" ) + `
53
+ usageTemplate = `{{title "USAGE"}}
75
54
{{- if (and .Runnable .HasAvailableInheritedFlags)}}
76
55
{{.UseLine}}
77
56
{{end -}}
78
57
79
58
{{- if .HasAvailableSubCommands}}
80
59
{{.CommandPath}} [flags] <command> [command flags]
81
- {{end -}}
82
-
83
- {{if not .HasAvailableSubCommands}}
84
- {{- if .Runnable}}
60
+ {{- else if .Runnable}}
85
61
{{.UseLine}}
86
- {{end - }}
87
- {{end }}
62
+ {{- end }}
63
+ {{- if gt (len .Aliases) 0 }}
88
64
89
- {{- if gt (len .Aliases) 0}}` + util . Bold ( " \n ALIASES" ) + `
65
+ {{title "ALIASES"}}
90
66
{{.NameAndAliases}}
91
- {{end -}}
67
+ {{- end}}
68
+ {{- if .HasAvailableSubCommands}}
92
69
93
- {{if .HasAvailableSubCommands}}` + util . Bold ( " \n COMMANDS" ) + `
70
+ {{title "COMMANDS"}}
94
71
{{- range .Commands}}
95
-
96
- {{- if (or .IsAvailableCommand ( eq .Name "help")) }}
72
+ {{- if or .IsAvailableCommand (eq .Name "help")}}
73
+ {{- if eq .GroupID "" }}
97
74
{{rpad .Name .NamePadding }} {{.Short}}
98
- {{- end -}}
99
-
100
- {{end}}
101
- {{end -}}
102
-
103
- {{- if not .HasAvailableInheritedFlags}} %s
104
- {{end -}}
105
-
106
- {{- if .HasAvailableLocalFlags}}` + util .Bold ("\n FLAGS" ) + `
75
+ {{- else if ne .GroupID "External"}}
76
+ {{rpad .Name .NamePadding }} {{index (split .Short "|") 0}}
77
+ {{- end}}
78
+ {{- end}}
79
+ {{- end}}
80
+ {{- end}}
81
+ {{- if gt (len .Groups) 0}}
82
+
83
+ {{title "EXTERNAL COMMANDS"}}
84
+ {{- range .Commands}}
85
+ {{- if or .IsAvailableCommand (eq .Name "help")}}
86
+ {{- if eq .GroupID "External"}}
87
+ {{rpad .Name .NamePadding }} {{.Short}}
88
+ {{- else if ne .GroupID ""}}
89
+ {{rpad .Name .NamePadding }} {{index (split .Short "|") 1}}
90
+ {{- end}}
91
+ {{- end}}
92
+ {{- end}}
93
+ {{- end}}
94
+ {{- if .HasAvailableLocalFlags}}
95
+
96
+ {{title "FLAGS"}}
107
97
{{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}
108
- {{end -}}
98
+ {{- end}}
99
+ {{- if .HasAvailableInheritedFlags}}
109
100
110
- {{- if .HasAvailableInheritedFlags}}` + util . Bold ( " \n GLOBAL FLAGS") + `
101
+ {{title "GLOBAL FLAGS"}}
111
102
{{.InheritedFlags.FlagUsages | trimTrailingWhitespaces}}
112
- {{end -}}
103
+ {{- end}}
104
+ {{- if .HasExample}}
113
105
114
- {{- if .HasExample}}` + util . Bold ( " \n EXAMPLES" ) + `
106
+ {{title "EXAMPLES"}}
115
107
{{.Example}}
116
- {{end -}}
117
-
108
+ {{- end}}
118
109
{{- if .HasAvailableSubCommands}}
110
+
119
111
Use "{{.CommandPath}} <command> --help" for more information about a command.
120
- {{end - }}
112
+ {{- end }}
121
113
`
122
114
)
0 commit comments