Skip to content

Implicit conversion to AnsiControlCode can throw #1798

Description

@KalleOlaviNiemitalo

System.CommandLine.Rendering.AnsiControlCode defines an implicit conversion from String. If the string is null or empty, then the conversion throws ArgumentException. IIRC, implicit conversions should not throw. Perhaps it would be better to make the conversion explicit, or remove it and make src/System.CommandLine.Rendering/Ansi.cs just use the constructor like new($"{Esc}[0m");.

public static implicit operator AnsiControlCode(string sequence)
{
return new AnsiControlCode(sequence);
}

public AnsiControlCode(string escapeSequence)
{
if (string.IsNullOrWhiteSpace(escapeSequence))
{
throw new ArgumentException("Value cannot be null or whitespace.", nameof(escapeSequence));

Found while searching for bugs similar to #1797.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions