...
1
16
17 package templates
18
19 import (
20 "strings"
21 "unicode"
22 )
23
24 const (
25
26 SectionVars = `{{$isRootCmd := isRootCmd .}}` +
27 `{{$rootCmd := rootCmd .}}` +
28 `{{$visibleFlags := visibleFlags (flagsNotIntersected .LocalFlags .PersistentFlags)}}` +
29 `{{$explicitlyExposedFlags := exposed .}}` +
30 `{{$optionsCmdFor := optionsCmdFor .}}` +
31 `{{$usageLine := usageLine .}}` +
32 `{{$reverseParentsNames := reverseParentsNames .}}`
33
34
35 SectionAliases = `{{if gt .Aliases 0}}Aliases:
36 {{.NameAndAliases}}
37
38 {{end}}`
39
40
41 SectionExamples = `{{if .HasExample}}Examples:
42 {{trimRight .Example}}
43
44 {{end}}`
45
46
47 SectionSubcommands = `{{if .HasAvailableSubCommands}}{{cmdGroupsString .}}
48
49 {{end}}`
50
51
52 SectionFlags = `{{ if or $visibleFlags.HasFlags $explicitlyExposedFlags.HasFlags}}Options:
53 {{ if $visibleFlags.HasFlags}}{{trimRight (flagsUsages $visibleFlags)}}{{end}}{{ if $explicitlyExposedFlags.HasFlags}}{{ if $visibleFlags.HasFlags}}
54 {{end}}{{trimRight (flagsUsages $explicitlyExposedFlags)}}{{end}}
55
56 {{end}}`
57
58
59 SectionUsage = `{{if and .Runnable (ne .UseLine "") (ne .UseLine $rootCmd)}}Usage:
60 {{$usageLine}}
61
62 {{end}}`
63
64
65 SectionTipsHelp = `{{if .HasSubCommands}}Use "{{range $reverseParentsNames}}{{.}} {{end}}<command> --help" for more information about a given command.
66 {{end}}`
67
68
69 SectionTipsGlobalOptions = `{{if $optionsCmdFor}}Use "{{$optionsCmdFor}}" for a list of global command-line options (applies to all commands).
70 {{end}}`
71 )
72
73
74 func MainHelpTemplate() string {
75 return `{{with or .Long .Short }}{{. | trim}}{{end}}{{if or .Runnable .HasSubCommands}}{{.UsageString}}{{end}}`
76 }
77
78
79 func MainUsageTemplate() string {
80 sections := []string{
81 "\n\n",
82 SectionVars,
83 SectionAliases,
84 SectionExamples,
85 SectionSubcommands,
86 SectionFlags,
87 SectionUsage,
88 SectionTipsHelp,
89 SectionTipsGlobalOptions,
90 }
91 return strings.TrimRightFunc(strings.Join(sections, ""), unicode.IsSpace)
92 }
93
94
95 func OptionsHelpTemplate() string {
96 return ""
97 }
98
99
100 func OptionsUsageTemplate() string {
101 return `{{ if .HasInheritedFlags}}The following options can be passed to any command:
102
103 {{flagsUsages .InheritedFlags}}{{end}}`
104 }
105
View as plain text