1package kingpin 2 3// Default usage template. 4var DefaultUsageTemplate = `{{define "FormatCommand"}}\ 5{{if .FlagSummary}} {{.FlagSummary}}{{end}}\ 6{{range .Args}} {{if not .Required}}[{{end}}<{{.Name}}>{{if .Value|IsCumulative}}...{{end}}{{if not .Required}}]{{end}}{{end}}\ 7{{end}}\ 8 9{{define "FormatCommands"}}\ 10{{range .FlattenedCommands}}\ 11{{if not .Hidden}}\ 12 {{.FullCommand}}{{if .Default}}*{{end}}{{template "FormatCommand" .}} 13{{.Help|Wrap 4}} 14{{end}}\ 15{{end}}\ 16{{end}}\ 17 18{{define "FormatUsage"}}\ 19{{template "FormatCommand" .}}{{if .Commands}} <command> [<args> ...]{{end}} 20{{if .Help}} 21{{.Help|Wrap 0}}\ 22{{end}}\ 23 24{{end}}\ 25 26{{if .Context.SelectedCommand}}\ 27usage: {{.App.Name}} {{.Context.SelectedCommand}}{{template "FormatUsage" .Context.SelectedCommand}} 28{{else}}\ 29usage: {{.App.Name}}{{template "FormatUsage" .App}} 30{{end}}\ 31{{if .Context.Flags}}\ 32Flags: 33{{.Context.Flags|FlagsToTwoColumns|FormatTwoColumns}} 34{{end}}\ 35{{if .Context.Args}}\ 36Args: 37{{.Context.Args|ArgsToTwoColumns|FormatTwoColumns}} 38{{end}}\ 39{{if .Context.SelectedCommand}}\ 40{{if len .Context.SelectedCommand.Commands}}\ 41Subcommands: 42{{template "FormatCommands" .Context.SelectedCommand}} 43{{end}}\ 44{{else if .App.Commands}}\ 45Commands: 46{{template "FormatCommands" .App}} 47{{end}}\ 48` 49 50// Usage template where command's optional flags are listed separately 51var SeparateOptionalFlagsUsageTemplate = `{{define "FormatCommand"}}\ 52{{if .FlagSummary}} {{.FlagSummary}}{{end}}\ 53{{range .Args}} {{if not .Required}}[{{end}}<{{.Name}}>{{if .Value|IsCumulative}}...{{end}}{{if not .Required}}]{{end}}{{end}}\ 54{{end}}\ 55 56{{define "FormatCommands"}}\ 57{{range .FlattenedCommands}}\ 58{{if not .Hidden}}\ 59 {{.FullCommand}}{{if .Default}}*{{end}}{{template "FormatCommand" .}} 60{{.Help|Wrap 4}} 61{{end}}\ 62{{end}}\ 63{{end}}\ 64 65{{define "FormatUsage"}}\ 66{{template "FormatCommand" .}}{{if .Commands}} <command> [<args> ...]{{end}} 67{{if .Help}} 68{{.Help|Wrap 0}}\ 69{{end}}\ 70 71{{end}}\ 72{{if .Context.SelectedCommand}}\ 73usage: {{.App.Name}} {{.Context.SelectedCommand}}{{template "FormatUsage" .Context.SelectedCommand}} 74{{else}}\ 75usage: {{.App.Name}}{{template "FormatUsage" .App}} 76{{end}}\ 77 78{{if .Context.Flags|RequiredFlags}}\ 79Required flags: 80{{.Context.Flags|RequiredFlags|FlagsToTwoColumns|FormatTwoColumns}} 81{{end}}\ 82{{if .Context.Flags|OptionalFlags}}\ 83Optional flags: 84{{.Context.Flags|OptionalFlags|FlagsToTwoColumns|FormatTwoColumns}} 85{{end}}\ 86{{if .Context.Args}}\ 87Args: 88{{.Context.Args|ArgsToTwoColumns|FormatTwoColumns}} 89{{end}}\ 90{{if .Context.SelectedCommand}}\ 91Subcommands: 92{{if .Context.SelectedCommand.Commands}}\ 93{{template "FormatCommands" .Context.SelectedCommand}} 94{{end}}\ 95{{else if .App.Commands}}\ 96Commands: 97{{template "FormatCommands" .App}} 98{{end}}\ 99` 100 101// Usage template with compactly formatted commands. 102var CompactUsageTemplate = `{{define "FormatCommand"}}\ 103{{if .FlagSummary}} {{.FlagSummary}}{{end}}\ 104{{range .Args}} {{if not .Required}}[{{end}}<{{.Name}}>{{if .Value|IsCumulative}}...{{end}}{{if not .Required}}]{{end}}{{end}}\ 105{{end}}\ 106 107{{define "FormatCommandList"}}\ 108{{range .}}\ 109{{if not .Hidden}}\ 110{{.Depth|Indent}}{{.Name}}{{if .Default}}*{{end}}{{template "FormatCommand" .}} 111{{end}}\ 112{{template "FormatCommandList" .Commands}}\ 113{{end}}\ 114{{end}}\ 115 116{{define "FormatUsage"}}\ 117{{template "FormatCommand" .}}{{if .Commands}} <command> [<args> ...]{{end}} 118{{if .Help}} 119{{.Help|Wrap 0}}\ 120{{end}}\ 121 122{{end}}\ 123 124{{if .Context.SelectedCommand}}\ 125usage: {{.App.Name}} {{.Context.SelectedCommand}}{{template "FormatUsage" .Context.SelectedCommand}} 126{{else}}\ 127usage: {{.App.Name}}{{template "FormatUsage" .App}} 128{{end}}\ 129{{if .Context.Flags}}\ 130Flags: 131{{.Context.Flags|FlagsToTwoColumns|FormatTwoColumns}} 132{{end}}\ 133{{if .Context.Args}}\ 134Args: 135{{.Context.Args|ArgsToTwoColumns|FormatTwoColumns}} 136{{end}}\ 137{{if .Context.SelectedCommand}}\ 138{{if .Context.SelectedCommand.Commands}}\ 139Commands: 140 {{.Context.SelectedCommand}} 141{{template "FormatCommandList" .Context.SelectedCommand.Commands}} 142{{end}}\ 143{{else if .App.Commands}}\ 144Commands: 145{{template "FormatCommandList" .App.Commands}} 146{{end}}\ 147` 148 149var ManPageTemplate = `{{define "FormatFlags"}}\ 150{{range .Flags}}\ 151{{if not .Hidden}}\ 152.TP 153\fB{{if .Short}}-{{.Short|Char}}, {{end}}--{{.Name}}{{if not .IsBoolFlag}}={{.FormatPlaceHolder}}{{end}}\\fR 154{{.Help}} 155{{end}}\ 156{{end}}\ 157{{end}}\ 158 159{{define "FormatCommand"}}\ 160{{if .FlagSummary}} {{.FlagSummary}}{{end}}\ 161{{range .Args}} {{if not .Required}}[{{end}}<{{.Name}}{{if .Default}}*{{end}}>{{if .Value|IsCumulative}}...{{end}}{{if not .Required}}]{{end}}{{end}}\ 162{{end}}\ 163 164{{define "FormatCommands"}}\ 165{{range .FlattenedCommands}}\ 166{{if not .Hidden}}\ 167.SS 168\fB{{.FullCommand}}{{template "FormatCommand" .}}\\fR 169.PP 170{{.Help}} 171{{template "FormatFlags" .}}\ 172{{end}}\ 173{{end}}\ 174{{end}}\ 175 176{{define "FormatUsage"}}\ 177{{template "FormatCommand" .}}{{if .Commands}} <command> [<args> ...]{{end}}\\fR 178{{end}}\ 179 180.TH {{.App.Name}} 1 {{.App.Version}} "{{.App.Author}}" 181.SH "NAME" 182{{.App.Name}} 183.SH "SYNOPSIS" 184.TP 185\fB{{.App.Name}}{{template "FormatUsage" .App}} 186.SH "DESCRIPTION" 187{{.App.Help}} 188.SH "OPTIONS" 189{{template "FormatFlags" .App}}\ 190{{if .App.Commands}}\ 191.SH "COMMANDS" 192{{template "FormatCommands" .App}}\ 193{{end}}\ 194` 195 196// Default usage template. 197var LongHelpTemplate = `{{define "FormatCommand"}}\ 198{{if .FlagSummary}} {{.FlagSummary}}{{end}}\ 199{{range .Args}} {{if not .Required}}[{{end}}<{{.Name}}>{{if .Value|IsCumulative}}...{{end}}{{if not .Required}}]{{end}}{{end}}\ 200{{end}}\ 201 202{{define "FormatCommands"}}\ 203{{range .FlattenedCommands}}\ 204{{if not .Hidden}}\ 205 {{.FullCommand}}{{template "FormatCommand" .}} 206{{.Help|Wrap 4}} 207{{with .Flags|FlagsToTwoColumns}}{{FormatTwoColumnsWithIndent . 4 2}}{{end}} 208{{end}}\ 209{{end}}\ 210{{end}}\ 211 212{{define "FormatUsage"}}\ 213{{template "FormatCommand" .}}{{if .Commands}} <command> [<args> ...]{{end}} 214{{if .Help}} 215{{.Help|Wrap 0}}\ 216{{end}}\ 217 218{{end}}\ 219 220usage: {{.App.Name}}{{template "FormatUsage" .App}} 221{{if .Context.Flags}}\ 222Flags: 223{{.Context.Flags|FlagsToTwoColumns|FormatTwoColumns}} 224{{end}}\ 225{{if .Context.Args}}\ 226Args: 227{{.Context.Args|ArgsToTwoColumns|FormatTwoColumns}} 228{{end}}\ 229{{if .App.Commands}}\ 230Commands: 231{{template "FormatCommands" .App}} 232{{end}}\ 233` 234 235var BashCompletionTemplate = ` 236_{{.App.Name}}_bash_autocomplete() { 237 local cur prev opts base 238 COMPREPLY=() 239 cur="${COMP_WORDS[COMP_CWORD]}" 240 opts=$( ${COMP_WORDS[0]} --completion-bash ${COMP_WORDS[@]:1:$COMP_CWORD} ) 241 COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) 242 return 0 243} 244complete -F _{{.App.Name}}_bash_autocomplete {{.App.Name}} 245 246` 247 248var ZshCompletionTemplate = ` 249#compdef {{.App.Name}} 250autoload -U compinit && compinit 251autoload -U bashcompinit && bashcompinit 252 253_{{.App.Name}}_bash_autocomplete() { 254 local cur prev opts base 255 COMPREPLY=() 256 cur="${COMP_WORDS[COMP_CWORD]}" 257 opts=$( ${COMP_WORDS[0]} --completion-bash ${COMP_WORDS[@]:1:$COMP_CWORD} ) 258 COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) 259 return 0 260} 261complete -F _{{.App.Name}}_bash_autocomplete {{.App.Name}} 262` 263