Skip to content
Myr

Merge pull request #254 from fabianofranz/optional_options_at_the_end_of_usage

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Must only add "[flags]" to the end of usage if not yet present

authored by Eric Paris · committed by Eric Paris · parents 65a708c, 16b7ba5 · Browse files · 3 files+36 added−8 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+36 added−8 removed

  • other:+36 added−8 removed

3 files+36 added−8 removed

modified:cobra_test.go+21 added-2 removed
18 unmodified lines
19var tp, te, tt, t1, tr []string20var rootPersPre, echoPre, echoPersPre, timesPersPre []string21var flagb1, flagb2, flagb3, flagbr, flagbp bool22var flags1, flags2a, flags2b, flags3, outs string23var flagi1, flagi2, flagi3, flagir int23var flagi1, flagi2, flagi3, flagi4, flagir int24var globalFlag1 bool25var flagEcho, rootcalled bool26var versionUsed int27
96 unmodified lines
124 Short: "A subcommand without a Run function",125 Long: "A long output about a subcommand without a Run function",126}127128var cmdCustomFlags = &Command{129 Use: "customflags [flags] -- REMOTE_COMMAND",130 Short: "A command that expects flags in a custom location",131 Long: "A long output about a command that expects flags in a custom location",132 Run: func(cmd *Command, args []string) {133 },134}135136var cmdVersion1 = &Command{137 Use: "version",138 Short: "Print the version number",139 Long: `First version of the version command`,
24 unmodified lines
164 cmdRootNoRun.ResetFlags()165 cmdRootSameName.ResetFlags()166 cmdRootWithRun.ResetFlags()167 cmdSubNoRun.ResetFlags()168 cmdCustomFlags.ResetFlags()169 cmdRootNoRun.PersistentFlags().StringVarP(&flags2a, "strtwo", "t", "two", strtwoParentHelp)170 cmdEcho.Flags().IntVarP(&flagi1, "intone", "i", 123, "help message for flag intone")171 cmdTimes.Flags().IntVarP(&flagi2, "inttwo", "j", 234, "help message for flag inttwo")172 cmdPrint.Flags().IntVarP(&flagi3, "intthree", "i", 345, "help message for flag intthree")173 cmdCustomFlags.Flags().IntVar(&flagi4, "intfour", 456, "help message for flag intfour")174 cmdEcho.PersistentFlags().StringVarP(&flags1, "strone", "s", "one", "help message for flag strone")175 cmdEcho.PersistentFlags().BoolVarP(&flagbp, "persistentbool", "p", false, "help message for flag persistentbool")176 cmdTimes.PersistentFlags().StringVarP(&flags2b, "strtwo", "t", "2", strtwoChildHelp)177 cmdPrint.PersistentFlags().StringVarP(&flags3, "strthree", "s", "three", "help message for flag strthree")
11 unmodified lines
189 cmdRootNoRun.ResetCommands()190 cmdRootSameName.ResetCommands()191 cmdRootWithRun.ResetCommands()192 cmdSubNoRun.ResetCommands()193 cmdCustomFlags.ResetCommands()194}195196func initialize() *Command {197 tt, tp, te = nil, nil, nil
83 unmodified lines
281 buf := new(bytes.Buffer)282 // Testing flag with invalid input283 c.SetOutput(buf)284 cmdEcho.AddCommand(cmdTimes)274 c.AddCommand(cmdPrint, cmdEcho, cmdSubNoRun, cmdDeprecated)285 c.AddCommand(cmdPrint, cmdEcho, cmdSubNoRun, cmdCustomFlags, cmdDeprecated)286 c.SetArgs(strings.Split(input, " "))287288 err := c.Execute()289 output := buf.String()
161 unmodified lines
451 t.Error("Command didn't parse correctly")452 }453}454455func TestUsage(t *testing.T) {456 x := fullSetupTest("help")457 checkResultContains(t, x, cmdRootWithRun.Use + " [flags]")458 x = fullSetupTest("help customflags")459 checkResultContains(t, x, cmdCustomFlags.Use)460 checkResultOmits(t, x, cmdCustomFlags.Use + " [flags]")461}462463func TestFlagLong(t *testing.T) {464 noRRSetupTest("echo --intone=13 something -- here")465466 if cmdEcho.ArgsLenAtDash() != 1 {
722 unmodified lines
modified:cobra.go+14 added-5 removed
25 unmodified lines
26 "unicode"27)2829var templateFuncs template.FuncMap = template.FuncMap{30 "trim": strings.TrimSpace,31 "trimRightSpace": trimRightSpace,32 "rpad": rpad,33 "gt": Gt,34 "eq": Eq,30 "trim": strings.TrimSpace,31 "trimRightSpace": trimRightSpace,32 "appendIfNotPresent": appendIfNotPresent,33 "rpad": rpad,34 "gt": Gt,35 "eq": Eq,36}3738var initializers []func()39
71 unmodified lines
111func trimRightSpace(s string) string {112 return strings.TrimRightFunc(s, unicode.IsSpace)113}114115// appendIfNotPresent will append stringToAppend to the end of s, but only if it's not yet present in s116func appendIfNotPresent(s, stringToAppend string) string {117 if strings.Contains(s, stringToAppend) {118 return s119 }120 return s + " " + stringToAppend121}122123//rpad adds padding to the right of a string124func rpad(s string, padding int) string {125 template := fmt.Sprintf("%%-%ds", padding)126 return fmt.Sprintf(template, s)
45 unmodified lines
modified:command.go+1 added-1 removed
261 unmodified lines
262 if c.HasParent() {263 return c.parent.UsageTemplate()264 } else {265 return `Usage:{{if .Runnable}}266 {{.UseLine}}{{if .HasFlags}} [flags]{{end}}{{end}}{{if .HasSubCommands}}266 {{if .HasFlags}}{{appendIfNotPresent .UseLine "[flags]"}}{{else}}{{.UseLine}}{{end}}{{end}}{{if .HasSubCommands}}267 {{ .CommandPath}} [command]{{end}}{{if gt .Aliases 0}}268269Aliases:270 {{.NameAndAliases}}
927 unmodified lines