forked from mirror/cobra
Merge pull request #254 from fabianofranz/optional_options_at_the_end_of_usage
Must only add "[flags]" to the end of usage if not yet present
This commit is contained in:
commit
1bacefc9a2
19
cobra.go
19
cobra.go
|
@ -27,11 +27,12 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var templateFuncs template.FuncMap = template.FuncMap{
|
var templateFuncs template.FuncMap = template.FuncMap{
|
||||||
"trim": strings.TrimSpace,
|
"trim": strings.TrimSpace,
|
||||||
"trimRightSpace": trimRightSpace,
|
"trimRightSpace": trimRightSpace,
|
||||||
"rpad": rpad,
|
"appendIfNotPresent": appendIfNotPresent,
|
||||||
"gt": Gt,
|
"rpad": rpad,
|
||||||
"eq": Eq,
|
"gt": Gt,
|
||||||
|
"eq": Eq,
|
||||||
}
|
}
|
||||||
|
|
||||||
var initializers []func()
|
var initializers []func()
|
||||||
|
@ -111,6 +112,14 @@ func trimRightSpace(s string) string {
|
||||||
return strings.TrimRightFunc(s, unicode.IsSpace)
|
return strings.TrimRightFunc(s, unicode.IsSpace)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// appendIfNotPresent will append stringToAppend to the end of s, but only if it's not yet present in s
|
||||||
|
func appendIfNotPresent(s, stringToAppend string) string {
|
||||||
|
if strings.Contains(s, stringToAppend) {
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
return s + " " + stringToAppend
|
||||||
|
}
|
||||||
|
|
||||||
//rpad adds padding to the right of a string
|
//rpad adds padding to the right of a string
|
||||||
func rpad(s string, padding int) string {
|
func rpad(s string, padding int) string {
|
||||||
template := fmt.Sprintf("%%-%ds", padding)
|
template := fmt.Sprintf("%%-%ds", padding)
|
||||||
|
|
|
@ -20,7 +20,7 @@ var tp, te, tt, t1, tr []string
|
||||||
var rootPersPre, echoPre, echoPersPre, timesPersPre []string
|
var rootPersPre, echoPre, echoPersPre, timesPersPre []string
|
||||||
var flagb1, flagb2, flagb3, flagbr, flagbp bool
|
var flagb1, flagb2, flagb3, flagbr, flagbp bool
|
||||||
var flags1, flags2a, flags2b, flags3, outs string
|
var flags1, flags2a, flags2b, flags3, outs string
|
||||||
var flagi1, flagi2, flagi3, flagir int
|
var flagi1, flagi2, flagi3, flagi4, flagir int
|
||||||
var globalFlag1 bool
|
var globalFlag1 bool
|
||||||
var flagEcho, rootcalled bool
|
var flagEcho, rootcalled bool
|
||||||
var versionUsed int
|
var versionUsed int
|
||||||
|
@ -125,6 +125,14 @@ var cmdSubNoRun = &Command{
|
||||||
Long: "A long output about a subcommand without a Run function",
|
Long: "A long output about a subcommand without a Run function",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var cmdCustomFlags = &Command{
|
||||||
|
Use: "customflags [flags] -- REMOTE_COMMAND",
|
||||||
|
Short: "A command that expects flags in a custom location",
|
||||||
|
Long: "A long output about a command that expects flags in a custom location",
|
||||||
|
Run: func(cmd *Command, args []string) {
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
var cmdVersion1 = &Command{
|
var cmdVersion1 = &Command{
|
||||||
Use: "version",
|
Use: "version",
|
||||||
Short: "Print the version number",
|
Short: "Print the version number",
|
||||||
|
@ -157,10 +165,12 @@ func flagInit() {
|
||||||
cmdRootSameName.ResetFlags()
|
cmdRootSameName.ResetFlags()
|
||||||
cmdRootWithRun.ResetFlags()
|
cmdRootWithRun.ResetFlags()
|
||||||
cmdSubNoRun.ResetFlags()
|
cmdSubNoRun.ResetFlags()
|
||||||
|
cmdCustomFlags.ResetFlags()
|
||||||
cmdRootNoRun.PersistentFlags().StringVarP(&flags2a, "strtwo", "t", "two", strtwoParentHelp)
|
cmdRootNoRun.PersistentFlags().StringVarP(&flags2a, "strtwo", "t", "two", strtwoParentHelp)
|
||||||
cmdEcho.Flags().IntVarP(&flagi1, "intone", "i", 123, "help message for flag intone")
|
cmdEcho.Flags().IntVarP(&flagi1, "intone", "i", 123, "help message for flag intone")
|
||||||
cmdTimes.Flags().IntVarP(&flagi2, "inttwo", "j", 234, "help message for flag inttwo")
|
cmdTimes.Flags().IntVarP(&flagi2, "inttwo", "j", 234, "help message for flag inttwo")
|
||||||
cmdPrint.Flags().IntVarP(&flagi3, "intthree", "i", 345, "help message for flag intthree")
|
cmdPrint.Flags().IntVarP(&flagi3, "intthree", "i", 345, "help message for flag intthree")
|
||||||
|
cmdCustomFlags.Flags().IntVar(&flagi4, "intfour", 456, "help message for flag intfour")
|
||||||
cmdEcho.PersistentFlags().StringVarP(&flags1, "strone", "s", "one", "help message for flag strone")
|
cmdEcho.PersistentFlags().StringVarP(&flags1, "strone", "s", "one", "help message for flag strone")
|
||||||
cmdEcho.PersistentFlags().BoolVarP(&flagbp, "persistentbool", "p", false, "help message for flag persistentbool")
|
cmdEcho.PersistentFlags().BoolVarP(&flagbp, "persistentbool", "p", false, "help message for flag persistentbool")
|
||||||
cmdTimes.PersistentFlags().StringVarP(&flags2b, "strtwo", "t", "2", strtwoChildHelp)
|
cmdTimes.PersistentFlags().StringVarP(&flags2b, "strtwo", "t", "2", strtwoChildHelp)
|
||||||
|
@ -180,6 +190,7 @@ func commandInit() {
|
||||||
cmdRootSameName.ResetCommands()
|
cmdRootSameName.ResetCommands()
|
||||||
cmdRootWithRun.ResetCommands()
|
cmdRootWithRun.ResetCommands()
|
||||||
cmdSubNoRun.ResetCommands()
|
cmdSubNoRun.ResetCommands()
|
||||||
|
cmdCustomFlags.ResetCommands()
|
||||||
}
|
}
|
||||||
|
|
||||||
func initialize() *Command {
|
func initialize() *Command {
|
||||||
|
@ -271,7 +282,7 @@ func fullTester(c *Command, input string) resulter {
|
||||||
// Testing flag with invalid input
|
// Testing flag with invalid input
|
||||||
c.SetOutput(buf)
|
c.SetOutput(buf)
|
||||||
cmdEcho.AddCommand(cmdTimes)
|
cmdEcho.AddCommand(cmdTimes)
|
||||||
c.AddCommand(cmdPrint, cmdEcho, cmdSubNoRun, cmdDeprecated)
|
c.AddCommand(cmdPrint, cmdEcho, cmdSubNoRun, cmdCustomFlags, cmdDeprecated)
|
||||||
c.SetArgs(strings.Split(input, " "))
|
c.SetArgs(strings.Split(input, " "))
|
||||||
|
|
||||||
err := c.Execute()
|
err := c.Execute()
|
||||||
|
@ -441,6 +452,14 @@ func TestGrandChildSameName(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestUsage(t *testing.T) {
|
||||||
|
x := fullSetupTest("help")
|
||||||
|
checkResultContains(t, x, cmdRootWithRun.Use + " [flags]")
|
||||||
|
x = fullSetupTest("help customflags")
|
||||||
|
checkResultContains(t, x, cmdCustomFlags.Use)
|
||||||
|
checkResultOmits(t, x, cmdCustomFlags.Use + " [flags]")
|
||||||
|
}
|
||||||
|
|
||||||
func TestFlagLong(t *testing.T) {
|
func TestFlagLong(t *testing.T) {
|
||||||
noRRSetupTest("echo --intone=13 something -- here")
|
noRRSetupTest("echo --intone=13 something -- here")
|
||||||
|
|
||||||
|
|
|
@ -263,7 +263,7 @@ func (c *Command) UsageTemplate() string {
|
||||||
return c.parent.UsageTemplate()
|
return c.parent.UsageTemplate()
|
||||||
} else {
|
} else {
|
||||||
return `Usage:{{if .Runnable}}
|
return `Usage:{{if .Runnable}}
|
||||||
{{.UseLine}}{{if .HasFlags}} [flags]{{end}}{{end}}{{if .HasSubCommands}}
|
{{if .HasFlags}}{{appendIfNotPresent .UseLine "[flags]"}}{{else}}{{.UseLine}}{{end}}{{end}}{{if .HasSubCommands}}
|
||||||
{{ .CommandPath}} [command]{{end}}{{if gt .Aliases 0}}
|
{{ .CommandPath}} [command]{{end}}{{if gt .Aliases 0}}
|
||||||
|
|
||||||
Aliases:
|
Aliases:
|
||||||
|
|
Loading…
Reference in New Issue