update github.com/spf13/cobra for new flag options

pull/6/head
Eric Paris 2015-02-20 09:44:14 -05:00
parent 4c87805870
commit 4ab4803b6b
3 changed files with 80 additions and 4 deletions

2
Godeps/Godeps.json generated
View File

@ -231,7 +231,7 @@
},
{
"ImportPath": "github.com/spf13/cobra",
"Rev": "07a9dc0024fcc97a0dcb3117bdf8933367037f4e"
"Rev": "f8e1ec56bdd7494d309c69681267859a6bfb7549"
},
{
"ImportPath": "github.com/spf13/pflag",

View File

@ -467,8 +467,16 @@ func TestRootHelp(t *testing.T) {
t.Errorf("--help shouldn't trigger an error, Got: \n %s", x.Output)
}
if strings.Contains(x.Output, cmdEcho.Use) {
t.Errorf("--help shouldn't display subcommand's usage, Got: \n %s", x.Output)
}
x = fullSetupTest("echo --help")
if strings.Contains(x.Output, cmdTimes.Use) {
t.Errorf("--help shouldn't display subsubcommand's usage, Got: \n %s", x.Output)
}
checkResultContains(t, x, "Available Commands:")
checkResultContains(t, x, "for more information about a command")

View File

@ -40,6 +40,8 @@ type Command struct {
Short string
// The long message shown in the 'help <this-command>' output.
Long string
// Examples of how to use the command
Example string
// Full set of flags
flags *flag.FlagSet
// Set of flags childrens of this command will inherit
@ -54,6 +56,7 @@ type Command struct {
// max lengths of commands' string lengths for use in padding
commandsMaxUseLen int
commandsMaxCommandPathLen int
commandsMaxNameLen int
flagErrorBuf *bytes.Buffer
cmdErrorBuf *bytes.Buffer
@ -181,6 +184,16 @@ func (c *Command) CommandPathPadding() int {
}
}
var minNamePadding int = 11
func (c *Command) NamePadding() int {
if c.parent == nil || minNamePadding > c.parent.commandsMaxNameLen {
return minNamePadding
} else {
return c.parent.commandsMaxNameLen
}
}
func (c *Command) UsageTemplate() string {
if c.usageTemplate != "" {
return c.usageTemplate
@ -195,10 +208,13 @@ Usage: {{if .Runnable}}
{{ .CommandPath}} [command]{{end}}{{if gt .Aliases 0}}
Aliases:
{{.NameAndAliases}}{{end}}
{{ if .HasSubCommands}}
{{.NameAndAliases}}
{{end}}{{if .HasExample}}
Examples:
{{ .Example }}
{{end}}{{ if .HasSubCommands}}
Available Commands: {{range .Commands}}{{if .Runnable}}
{{rpad .Use .UsagePadding }} {{.Short}}{{end}}{{end}}
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}
{{end}}
{{ if .HasLocalFlags}}Flags:
{{.LocalFlags.FlagUsages}}{{end}}
@ -529,6 +545,10 @@ func (c *Command) AddCommand(cmds ...*Command) {
if commandPathLen > c.commandsMaxCommandPathLen {
c.commandsMaxCommandPathLen = commandPathLen
}
nameLen := len(x.Name())
if nameLen > c.commandsMaxNameLen {
c.commandsMaxNameLen = nameLen
}
c.commands = append(c.commands, x)
}
}
@ -669,6 +689,10 @@ func (c *Command) NameAndAliases() string {
return strings.Join(append([]string{c.Name()}, c.Aliases...), ", ")
}
func (c *Command) HasExample() bool {
return len(c.Example) > 0
}
// Determine if the command is itself runnable
func (c *Command) Runnable() bool {
return c.Run != nil
@ -713,6 +737,50 @@ func (c *Command) LocalFlags() *flag.FlagSet {
return local
}
// All Flags which were inherited from parents commands
func (c *Command) InheritedFlags() *flag.FlagSet {
c.mergePersistentFlags()
local := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
var rmerge func(x *Command)
rmerge = func(x *Command) {
if x.HasPersistentFlags() {
x.PersistentFlags().VisitAll(func(f *flag.Flag) {
if local.Lookup(f.Name) == nil {
local.AddFlag(f)
}
})
}
if x.HasParent() {
rmerge(x.parent)
}
}
if c.HasParent() {
rmerge(c.parent)
}
return local
}
// All Flags which were not inherited from parent commands
func (c *Command) NonInheritedFlags() *flag.FlagSet {
c.mergePersistentFlags()
local := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
inheritedFlags := c.InheritedFlags()
c.Flags().VisitAll(func(f *flag.Flag) {
if inheritedFlags.Lookup(f.Name) == nil {
local.AddFlag(f)
}
})
return local
}
// Get the Persistent FlagSet specifically set in the current command
func (c *Command) PersistentFlags() *flag.FlagSet {
if c.pflags == nil {