...

Source file src/k8s.io/kubectl/pkg/util/templates/templates.go

Documentation: k8s.io/kubectl/pkg/util/templates

     1  /*
     2  Copyright 2016 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package templates
    18  
    19  import (
    20  	"strings"
    21  	"unicode"
    22  )
    23  
    24  const (
    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  		`{{$reverseParentsNames := reverseParentsNames .}}`
    33  
    34  	// SectionAliases is the help template section that displays command aliases.
    35  	SectionAliases = `{{if gt .Aliases 0}}Aliases:
    36  {{.NameAndAliases}}
    37  
    38  {{end}}`
    39  
    40  	// SectionExamples is the help template section that displays command examples.
    41  	SectionExamples = `{{if .HasExample}}Examples:
    42  {{trimRight .Example}}
    43  
    44  {{end}}`
    45  
    46  	// SectionSubcommands is the help template section that displays the command's subcommands.
    47  	SectionSubcommands = `{{if .HasAvailableSubCommands}}{{cmdGroupsString .}}
    48  
    49  {{end}}`
    50  
    51  	// SectionFlags is the help template section that displays the command's flags.
    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  	// SectionUsage is the help template section that displays the command's usage.
    59  	SectionUsage = `{{if and .Runnable (ne .UseLine "") (ne .UseLine $rootCmd)}}Usage:
    60    {{$usageLine}}
    61  
    62  {{end}}`
    63  
    64  	// SectionTipsHelp is the help template section that displays the '--help' hint.
    65  	SectionTipsHelp = `{{if .HasSubCommands}}Use "{{range $reverseParentsNames}}{{.}} {{end}}<command> --help" for more information about a given command.
    66  {{end}}`
    67  
    68  	// SectionTipsGlobalOptions is the help template section that displays the 'options' hint for displaying global flags.
    69  	SectionTipsGlobalOptions = `{{if $optionsCmdFor}}Use "{{$optionsCmdFor}}" for a list of global command-line options (applies to all commands).
    70  {{end}}`
    71  )
    72  
    73  // MainHelpTemplate if the template for 'help' used by most commands.
    74  func MainHelpTemplate() string {
    75  	return `{{with or .Long .Short }}{{. | trim}}{{end}}{{if or .Runnable .HasSubCommands}}{{.UsageString}}{{end}}`
    76  }
    77  
    78  // MainUsageTemplate if the template for 'usage' used by most commands.
    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  // OptionsHelpTemplate if the template for 'help' used by the 'options' command.
    95  func OptionsHelpTemplate() string {
    96  	return ""
    97  }
    98  
    99  // OptionsUsageTemplate if the template for 'usage' used by the 'options' command.
   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