Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cmd/root: Work around Cobra 1.1.2's handling of usage functions
In version 1.1.2 of Cobra has been included a change[0] that changes how custom usage functions are handled. Example of the wrong behaviour: $ toolbox --foo Error: unknown flag: --foo Run 'toolbox --help' for usage.Error: Run 'toolbox --help' for usage. Desired behaviour: $ toolbox --foo Error: unknown flag: --foo Run 'toolbox --help' for usage. A workaround is to define a template string for the usage instead. The template uses the templating language of Go[1]. See the default template string in version 1.2.1[2]. Because the template is set only once, the executableBase needs to be set before the template is applied. That required the move of setUpGlobals() into init() of the cmd package. This is a better place for the function call as init() is called earlier than Execute()[3]. Upstream issue: spf13/cobra#1532 [0] spf13/cobra#1044 [1] https://pkg.go.dev/text/template [2] https://github.com/spf13/cobra/blob/v1.2.1/command.go#L491 [3] https://golang.org/doc/effective_go#init containers#917
- Loading branch information