Skip to content

Commit

Permalink
Issue 1200: use single quotes when printing usage for string flags
Browse files Browse the repository at this point in the history
  • Loading branch information
fabianofranz committed Mar 30, 2015
1 parent 21e160c commit 6b2e4d3
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
2 changes: 1 addition & 1 deletion pkg/cmd/cli/cmd/options.go
Expand Up @@ -13,7 +13,7 @@ func NewCmdOptions(f *clientcmd.Factory, out io.Writer) *cobra.Command {
cmd := &cobra.Command{
Use: "options",
Run: func(cmd *cobra.Command, args []string) {
cmd.Help()
cmd.Usage()
},
}

Expand Down
22 changes: 22 additions & 0 deletions pkg/cmd/templates/templater.go
@@ -1,6 +1,7 @@
package templates

import (
"bytes"
"fmt"
"strings"
"text/template"
Expand Down Expand Up @@ -37,6 +38,27 @@ func (templater *Templater) UsageFunc() func(*cobra.Command) error {
}
return exposed
},
"flagsUsages": func(f *flag.FlagSet) string {
x := new(bytes.Buffer)

f.VisitAll(func(flag *flag.Flag) {
format := "--%s=%s: %s\n"

if flag.Value.Type() == "string" {
format = "--%s='%s': %s\n"
}

if len(flag.Shorthand) > 0 {
format = " -%s, " + format
} else {
format = " %s " + format
}

fmt.Fprintf(x, format, flag.Shorthand, flag.Name, flag.DefValue, flag.Usage)
})

return x.String()
},
})

template.Must(t.Parse(templater.UsageTemplate))
Expand Down
15 changes: 7 additions & 8 deletions pkg/cmd/templates/templates.go
Expand Up @@ -61,9 +61,9 @@ Available Commands: {{range .Commands}}{{if .Runnable}}
{{rpad .Use .UsagePadding }} {{.Short}}{{end}}{{end}}
{{end}}
{{ if .HasLocalFlags}}Options:
{{.LocalFlags.FlagUsages}}{{end}}
{{flagsUsages .LocalFlags}}{{end}}
{{ if .HasAnyPersistentFlags}}Global Options:
{{.AllPersistentFlags.FlagUsages}}{{end}}{{ if .HasSubCommands }}
{{flagsUsages .AllPersistentFlags}}{{end}}{{ if .HasSubCommands }}
Use "{{.Root.Name}} <command> --help" for more information about a given command.
{{end}}`

Expand All @@ -75,7 +75,7 @@ Available Commands: {{range .Commands}}{{if .Runnable}}{{if ne .Name "options"}}
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}
{{end}}
{{ if or .HasLocalFlags $exposedFlags.HasFlags}}Options:
{{ if .HasLocalFlags}}{{.LocalFlags.FlagUsages}}{{end}}{{ if $exposedFlags.HasFlags}}{{$exposedFlags.FlagUsages}}{{end}}
{{ if .HasLocalFlags}}{{flagsUsages .LocalFlags}}{{end}}{{ if $exposedFlags.HasFlags}}{{flagsUsages $exposedFlags}}{{end}}
{{end}}{{ if not $isRootCmd}}Use "{{template "rootCli" .}} --help" for a list of all commands available in {{template "rootCli" .}}.
{{end}}{{ if .HasSubCommands }}Use "{{template "rootCli" .}} <command> --help" for more information about a given command.
{{end}}{{ if .HasAnyPersistentFlags}}Use "{{template "rootCli" .}} options" for a list of global command-line options (applies to all commands).
Expand All @@ -89,16 +89,15 @@ Available Commands: {{range .Commands}}{{if .Runnable}}{{if ne .Name "options"}}
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}
{{end}}
{{ if or .HasLocalFlags $exposedFlags.HasFlags}}Options:
{{ if .HasLocalFlags}}{{.LocalFlags.FlagUsages}}{{end}}{{ if $exposedFlags.HasFlags}}{{$exposedFlags.FlagUsages}}{{end}}
{{ if .HasLocalFlags}}{{flagsUsages .LocalFlags}}{{end}}{{ if $exposedFlags.HasFlags}}{{flagsUsages $exposedFlags}}{{end}}
{{end}}{{ if not $isRootCmd}}Use "{{template "rootCli" .}} --help" for a list of all commands available in {{template "rootCli" .}}.
{{end}}{{ if .HasSubCommands }}Use "{{template "rootCli" .}} <command> --help" for more information about a given command.
{{end}}{{ if .HasAnyPersistentFlags}}Use "{{template "rootCli" .}} options" for a list of global command-line options (applies to all commands).
{{end}}`

optionsHelpTemplate = `{{ if .HasAnyPersistentFlags}}The following options can be passed to any command:
optionsHelpTemplate = ``

{{.AllPersistentFlags.FlagUsages}}{{end}}
`
optionsUsageTemplate = `{{ if .HasAnyPersistentFlags}}The following options can be passed to any command:
optionsUsageTemplate = ``
{{flagsUsages .AllPersistentFlags}}{{end}}`
)

0 comments on commit 6b2e4d3

Please sign in to comment.