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
Provenance
- Commit
1bacefc- Tree
782e9a4- Author
Eric Paris <eparis@redhat.com>- Committer
Eric Paris <eparis@redhat.com>- Parent 1
65a708c- Parent 2
16b7ba5
3 files changed+36 added−8 removed
modified:cobra_test.go+21 added-2 removed
18 unmodified lines19 var tp, te, tt, t1, tr []string20 var rootPersPre, echoPre, echoPersPre, timesPersPre []string21 var flagb1, flagb2, flagb3, flagbr, flagbp bool22 var flags1, flags2a, flags2b, flags3, outs string23-var flagi1, flagi2, flagi3, flagir int23+var flagi1, flagi2, flagi3, flagi4, flagir int24 var globalFlag1 bool25 var flagEcho, rootcalled bool26 var versionUsed int27 96 unmodified lines124 Short: "A subcommand without a Run function",125 Long: "A long output about a subcommand without a Run function",126 }127 128+var 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+}135+136 var cmdVersion1 = &Command{137 Use: "version",138 Short: "Print the version number",139 Long: `First version of the version command`,24 unmodified lines164 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 lines189 cmdRootNoRun.ResetCommands()190 cmdRootSameName.ResetCommands()191 cmdRootWithRun.ResetCommands()192 cmdSubNoRun.ResetCommands()193+ cmdCustomFlags.ResetCommands()194 }195 196 func initialize() *Command {197 tt, tp, te = nil, nil, nil83 unmodified lines281 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, " "))287 288 err := c.Execute()289 output := buf.String()161 unmodified lines451 t.Error("Command didn't parse correctly")452 }453 }454 455+func 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+}462+463 func TestFlagLong(t *testing.T) {464 noRRSetupTest("echo --intone=13 something -- here")465 466 if cmdEcho.ArgsLenAtDash() != 1 {722 unmodified linesmodified:cobra.go+14 added-5 removed
25 unmodified lines26 "unicode"27 )28 29 var 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 }37 38 var initializers []func()39 71 unmodified lines111 func trimRightSpace(s string) string {112 return strings.TrimRightFunc(s, unicode.IsSpace)113 }114 115+// appendIfNotPresent will append stringToAppend to the end of s, but only if it's not yet present in s116+func appendIfNotPresent(s, stringToAppend string) string {117+ if strings.Contains(s, stringToAppend) {118+ return s119+ }120+ return s + " " + stringToAppend121+}122+123 //rpad adds padding to the right of a string124 func rpad(s string, padding int) string {125 template := fmt.Sprintf("%%-%ds", padding)126 return fmt.Sprintf(template, s)45 unmodified linesmodified:command.go+1 added-1 removed
261 unmodified lines262 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}}268 269 Aliases:270 {{.NameAndAliases}}927 unmodified lines