Displays the default value of a command flag

Added ability to display the default value of a command's flag,
truncated in case it contains a column (:). Also instead of displaying
the entire command usage/help in case of an undefined flag, output a
small message with "bee help cmd".
This commit is contained in:
Faissal Elamraoui 2016-12-04 16:30:38 +01:00
parent 73aa44e1a7
commit ce71c916d5
1 changed files with 14 additions and 2 deletions

16
bee.go
View File

@ -84,7 +84,7 @@ func (c *Command) Out() io.Writer {
// Usage puts out the usage for the command.
func (c *Command) Usage() {
tmpl(helpTemplate, c)
tmpl(cmdUsage, c)
os.Exit(2)
}
@ -97,7 +97,17 @@ func (c *Command) Runnable() bool {
func (c *Command) Options() map[string]string {
options := make(map[string]string)
c.Flag.VisitAll(func(f *flag.Flag) {
options[f.Name] = f.Usage
defaultVal := f.DefValue
if len(defaultVal) > 0 {
if strings.Contains(defaultVal, ":") {
// Truncate the flag's default value by appending '...' at the end
options[f.Name+"="+strings.Split(defaultVal, ":")[0]+":..."] = f.Usage
} else {
options[f.Name+"="+defaultVal] = f.Usage
}
} else {
options[f.Name] = f.Usage
}
})
return options
}
@ -196,6 +206,8 @@ var errorTemplate = `bee: %s.
Use {{"bee help" | bold}} for more information.
`
var cmdUsage = `Use {{printf "bee help %s" .Name | bold}} for more information.{{endline}}`
func usage() {
tmpl(usageTemplate, availableCommands)
os.Exit(2)