1/*
2Copyright 2016 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package templates
18
19import (
20	"strings"
21	"unicode"
22)
23
24const (
25	// SectionVars is the help template section that declares variables to be used in the template.
26	SectionVars = `{{$isRootCmd := isRootCmd .}}` +
27		`{{$rootCmd := rootCmd .}}` +
28		`{{$visibleFlags := visibleFlags (flagsNotIntersected .LocalFlags .PersistentFlags)}}` +
29		`{{$explicitlyExposedFlags := exposed .}}` +
30		`{{$optionsCmdFor := optionsCmdFor .}}` +
31		`{{$usageLine := usageLine .}}`
32
33	// SectionAliases is the help template section that displays command aliases.
34	SectionAliases = `{{if gt .Aliases 0}}Aliases:
35{{.NameAndAliases}}
36
37{{end}}`
38
39	// SectionExamples is the help template section that displays command examples.
40	SectionExamples = `{{if .HasExample}}Examples:
41{{trimRight .Example}}
42
43{{end}}`
44
45	// SectionSubcommands is the help template section that displays the command's subcommands.
46	SectionSubcommands = `{{if .HasAvailableSubCommands}}{{cmdGroupsString .}}
47
48{{end}}`
49
50	// SectionFlags is the help template section that displays the command's flags.
51	SectionFlags = `{{ if or $visibleFlags.HasFlags $explicitlyExposedFlags.HasFlags}}Options:
52{{ if $visibleFlags.HasFlags}}{{trimRight (flagsUsages $visibleFlags)}}{{end}}{{ if $explicitlyExposedFlags.HasFlags}}{{ if $visibleFlags.HasFlags}}
53{{end}}{{trimRight (flagsUsages $explicitlyExposedFlags)}}{{end}}
54
55{{end}}`
56
57	// SectionUsage is the help template section that displays the command's usage.
58	SectionUsage = `{{if and .Runnable (ne .UseLine "") (ne .UseLine $rootCmd)}}Usage:
59  {{$usageLine}}
60
61{{end}}`
62
63	// SectionTipsHelp is the help template section that displays the '--help' hint.
64	SectionTipsHelp = `{{if .HasSubCommands}}Use "{{$rootCmd}} <command> --help" for more information about a given command.
65{{end}}`
66
67	// SectionTipsGlobalOptions is the help template section that displays the 'options' hint for displaying global flags.
68	SectionTipsGlobalOptions = `{{if $optionsCmdFor}}Use "{{$optionsCmdFor}}" for a list of global command-line options (applies to all commands).
69{{end}}`
70)
71
72// MainHelpTemplate if the template for 'help' used by most commands.
73func MainHelpTemplate() string {
74	return `{{with or .Long .Short }}{{. | trim}}{{end}}{{if or .Runnable .HasSubCommands}}{{.UsageString}}{{end}}`
75}
76
77// MainUsageTemplate if the template for 'usage' used by most commands.
78func MainUsageTemplate() string {
79	sections := []string{
80		"\n\n",
81		SectionVars,
82		SectionAliases,
83		SectionExamples,
84		SectionSubcommands,
85		SectionFlags,
86		SectionUsage,
87		SectionTipsHelp,
88		SectionTipsGlobalOptions,
89	}
90	return strings.TrimRightFunc(strings.Join(sections, ""), unicode.IsSpace)
91}
92
93// OptionsHelpTemplate if the template for 'help' used by the 'options' command.
94func OptionsHelpTemplate() string {
95	return ""
96}
97
98// OptionsUsageTemplate if the template for 'usage' used by the 'options' command.
99func OptionsUsageTemplate() string {
100	return `{{ if .HasInheritedFlags}}The following options can be passed to any command:
101
102{{flagsUsages .InheritedFlags}}{{end}}`
103}
104