new variable MousetrapDisplayDuration allows to modify the default
display duration of 5s, or to completely disable the timeout and wait
for the user to press the return key.
There were template functions which we defined and others started using.
Although we no longer want those functions, since others use them,
deleting them breaks our API. Putting those (unused) functions back.
* Delete Eq, Gt, appendIfNotPresent and trim functions
* Add "[flags]" in UseLine
* Simplify other functions
* Simplify templates
Minor performance improvement.
Benchmark for command with 4 flags and one child command:
benchmark old ns/op new ns/op delta
BenchmarkCmdUsageFunc-4 335860 319290 -4.93%
benchmark old allocs new allocs delta
BenchmarkCmdUsageFunc-4 562 543 -3.38%
benchmark old bytes new bytes delta
BenchmarkCmdUsageFunc-4 21623 21037 -2.71%
* Moving final return outside of if-else
* Removing type declarations that Go can infer from values
* Cleaning up some existing comments
* Changing snake_case variables to camelCase
The flags usage template from pflags has a trailing \n. We need to
include a newline in case there are no flags in our template. This will
trim the newline from the end of the flags from pflag and we can do it
right outselves.
This patch enables developers to add one to many template functions that
can be used by custom Usage and Help templates. Here is an example that
is included in the file cobra_test.go as the test function named
TestAddTemplateFunctions:
AddTemplateFunc("t", func() bool { return true })
AddTemplateFuncs(template.FuncMap{
"f": func() bool { return false },
"h": func() string { return "Hello," },
"w": func() string { return "world." }})
const usage = "Hello, world."
c := &Command{}
c.SetUsageTemplate(`{{if t}}{{h}}{{end}}{{if f}}{{h}}{{end}} {{w}}`)
if us := c.UsageString(); us != usage {
t.Errorf("c.UsageString() != \"%s\", is \"%s\"", usage, us)
}
In the above example four functions are added to the template function
map used when the Usage and Help text is generated from the templates
that enable custom logic as well as data injection during template
execution.