Skip to content

Begin a command error message with a custom string #1281

@ashpool37

Description

@ashpool37

It is a convention for CLI utilities in Unix-like environments to prefix the name of the utility to any error messages printed. For example, notice how the find command outputs the find: prefix:

$ find nemo
find: ‘nemo’: No such file or directory

This is useful when debugging shell scripts for determining exactly which of the commands used in the script has failed.

Cobra has a convenient facility for declaring commands which might fail and return an error: the Command.RunE() method and friends. However, upon returning an error from such a method, we find that it is hard-coded in Command.ExecuteC() to prefix the string Error: to any error message that results from this.

My proposal is to add a way to change that hard-coded value to a custom string. Maybe use argv[0] by default to follow the convention. Without this feature, a programmer who wishes to adhere to the convention I mentioned is forced to implement their own error output in Run() or RunE() and/or silence the one provided by Cobra.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions