Skip to content

[Feature] ClickException message without color would be helpful #2419

Open

Description

@kriswuollett

Similar to how some runtime environments don't support colors, it may be helpful to programmers that the non-colorized rendering of the ClickException message was available... something I was thinking when encountering and working around #2193.

Color shouldn't matter, nor should creating subclasses be necessary for verifying that the correct user-facing ClickException was raised with a certain message, for example in a test:

"""Tests for ClickException and exception handling."""
import click

def test_exception_message_no_color():
    """Base Exception message should be plain text."""
    exception = click.ClickException(click.style('red', fg='red') + ' text')

    assert 'red text' in str(exception) # fails currently

I'd think that __str__, and the possibly the message property too, should be rendered as plain text. I do have a simple StringIO patch that works, as well as just creating a subclass for my code if click is unpatched.

Activity

  1. changed the title [-]`ClickException` message without color would be helpful[/-] [+][Feature] `ClickException` message without color would be helpful[/+] on Jan 5, 2025
  2. added
    help outputHelp text formatting, layout, and --help rendering
    on Aug 8, 2026
  3. kdeldycke commented on Oct 29, 2026

    @kdeldycke
    Collaborator

    Relates to: #944

  4. kdeldycke commented on Oct 29, 2026

    @kdeldycke
    Collaborator

    Closing this as Click already does this: to get the plain text of a styled message, call click.unstyle().

    Click's own test suite cover this at:

    click/tests/test_testing.py

    Lines 229 to 246 in 8b19813

    def test_with_color_errors():
    class CLIError(ClickException):
    def format_message(self) -> str:
    return click.style(self.message, fg="red")
    @click.command()
    def cli():
    raise CLIError("Red error")
    runner = CliRunner()
    result = runner.invoke(cli)
    assert result.output == "Error: Red error\n"
    assert result.exception
    result = runner.invoke(cli, color=True)
    assert result.output == f"Error: {click.style('Red error', fg='red')}\n"
    assert result.exception

  5. added this to the 8.5.1 milestone on Oct 29, 2026
  6. kdeldycke commented on Oct 29, 2026

    @kdeldycke
    Collaborator

    As a definitive proof, I added some more tests in #3899

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    help outputHelp text formatting, layout, and --help rendering

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions