templates.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. Copyright 2016 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package templates
  14. import "strings"
  15. func MainHelpTemplate() string {
  16. return decorate(mainHelpTemplate, false)
  17. }
  18. func MainUsageTemplate() string {
  19. return decorate(mainUsageTemplate, true) + "\n"
  20. }
  21. func OptionsHelpTemplate() string {
  22. return decorate(optionsHelpTemplate, false)
  23. }
  24. func OptionsUsageTemplate() string {
  25. return decorate(optionsUsageTemplate, false)
  26. }
  27. func decorate(template string, trim bool) string {
  28. if trim && len(strings.Trim(template, " ")) > 0 {
  29. template = strings.Trim(template, "\n")
  30. }
  31. return template
  32. }
  33. const (
  34. vars = `{{$isRootCmd := isRootCmd .}}` +
  35. `{{$rootCmd := rootCmd .}}` +
  36. `{{$visibleFlags := visibleFlags (flagsNotIntersected .LocalFlags .PersistentFlags)}}` +
  37. `{{$explicitlyExposedFlags := exposed .}}` +
  38. `{{$optionsCmdFor := optionsCmdFor .}}` +
  39. `{{$usageLine := usageLine .}}`
  40. mainHelpTemplate = `{{with or .Long .Short }}{{. | trim}}{{end}}{{if or .Runnable .HasSubCommands}}{{.UsageString}}{{end}}`
  41. mainUsageTemplate = vars +
  42. // ALIASES
  43. `{{if gt .Aliases 0}}
  44. Aliases:
  45. {{.NameAndAliases}}{{end}}` +
  46. // EXAMPLES
  47. `{{if .HasExample}}
  48. Examples:
  49. {{ indentLines (.Example | trimLeft) 2 }}{{end}}` +
  50. // SUBCOMMANDS
  51. `{{ if .HasAvailableSubCommands}}
  52. {{range cmdGroups . .Commands}}
  53. {{.Message}}
  54. {{range .Commands}}{{if .Runnable}} {{rpad .Name .NamePadding }} {{.Short}}
  55. {{end}}{{end}}{{end}}{{end}}` +
  56. // VISIBLE FLAGS
  57. `{{ if or $visibleFlags.HasFlags $explicitlyExposedFlags.HasFlags}}
  58. Options:
  59. {{ if $visibleFlags.HasFlags}}{{flagsUsages $visibleFlags}}{{end}}{{ if $explicitlyExposedFlags.HasFlags}}{{flagsUsages $explicitlyExposedFlags}}{{end}}{{end}}` +
  60. // USAGE LINE
  61. `{{if and .Runnable (ne .UseLine "") (ne .UseLine $rootCmd)}}
  62. Usage:
  63. {{$usageLine}}
  64. {{end}}` +
  65. // TIPS: --help
  66. `{{ if .HasSubCommands }}
  67. Use "{{$rootCmd}} <command> --help" for more information about a given command.{{end}}` +
  68. // TIPS: global options
  69. `{{ if $optionsCmdFor}}
  70. Use "{{$optionsCmdFor}}" for a list of global command-line options (applies to all commands).{{end}}`
  71. optionsHelpTemplate = ``
  72. optionsUsageTemplate = `{{ if .HasInheritedFlags}}The following options can be passed to any command:
  73. {{flagsUsages .InheritedFlags}}{{end}}`
  74. )