cobra icon indicating copy to clipboard operation
cobra copied to clipboard

permit empty string for ErrPrefix

Open jchappelow opened this issue 3 weeks ago • 2 comments

The SetErrPrefix method introduced in https://github.com/spf13/cobra/pull/2023 is nice, but I wanted to set it to an empty string "", but discovered that this is treated the same as if you had not set it at all. For example, I want to print structured JSON with an "error" field or similar. To do this and return a non-zero exit code to the OS requires more hoop jumping unless I can remove teh prefix.

It would be preferable if this were a *string field, which would allow setting it to an empty string.

Another unexpected aspect of the prefix, is that a space is always inserted because it is applied with c.PrintErrln(c.ErrPrefix(), err.Error()). Perhaps at this spot, it could simply run c.PrintErrln(err.Error()) if we find that c.ErrPrefix() is ""? This way the prefix field could remain a string (no pointer).

jchappelow avatar Jan 29 '25 22:01 jchappelow