-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Excesive string prefix to usage function output #1532
Comments
In version 1.1.2 of Cobra has been included a change[0] that changes the behaviour of how custom usage functions are printed. The change prepends "Error: " to the text. This is not desired since the usage text is not an error text. 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]. This change has the positive benefit of getting more helpful usage messages. E.g., for command 'toolbox enter' the usage text is no longer "Run 'toolbox --help' for usage." but "Run 'toolbox enter --help' for usage.". 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
In version 1.1.2 of Cobra has been included a change[0] that changes the behaviour of how custom usage functions are printed. The change prepends "Error: " to the text. This is not desired since the usage text is not an error text. Example of the wrong behaviour: $ toolbox --foo Error: unknown flag: --foo 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
In version 1.1.2 of Cobra has been included a change[0] that changes the behaviour of how custom usage functions are printed. The change prepends "Error: " to the text. This is not desired since the usage text is not an error text. Example of the wrong behaviour: $ toolbox --foo Error: unknown flag: --foo 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
In version 1.1.2 of Cobra has been included a change[0] that changes the behaviour of how custom usage functions are printed. The change prepends "Error: " to the text. This is not desired since the usage text is not an error text. Example of the wrong behaviour: $ toolbox --foo Error: unknown flag: --foo 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
In version 1.1.2 of Cobra has been included a change[0] that changes the behaviour of how custom usage functions are printed. The change prepends "Error: " to the text. This is not desired since the usage text is not an error text. Example of the wrong behaviour: $ toolbox --foo Error: unknown flag: --foo 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
In version 1.1.2 of Cobra has been included a change[0] that changes the behaviour of how custom usage functions are printed. The change prepends "Error: " to the text. This is not desired since the usage text is not an error text. Example of the wrong behaviour: $ toolbox --foo Error: unknown flag: --foo 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
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
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
This issue is being marked as stale due to a long period of inactivity |
This issue is still present in Cobra. |
This seems like correct behaviour to me. If you return an error, it is explicitly communicated as an error. There are plenty of use-cases where the usage function could return an unexpected error (as opposed to an expected error). You should be using var rootCmd = &cobra.Command{
Use: "cmd"
}
func usage(cmd *cobra.Command) error {
fmt.Println("Custom usage message")
return nil
}
rootCmd.SetUsageFunc(usage) |
This behaviour should then be documented. It is highly perplexing when encountered at first. Also the reliance on side-effects in the function causes |
PR #1044 introduced in commit dd3684a a new prefix to the usage function. Example:
Forcing the created binary to print out the usage (e.g., by passing a wrong flag) will yield the following output:
Note the
Error:
prefix before the usage text. The only way to bypass this behaviour is to set a template string instead.The desired output would be:
The text was updated successfully, but these errors were encountered: