From ce71c916d5bad8fa79e74908621d7762b582b622 Mon Sep 17 00:00:00 2001 From: Faissal Elamraoui Date: Sun, 4 Dec 2016 16:30:38 +0100 Subject: [PATCH] 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". --- bee.go | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/bee.go b/bee.go index 840bf50..d90386f 100644 --- a/bee.go +++ b/bee.go @@ -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)