rustclap

clap.rs not printing colors during `--help`


So I migrated from clap v3.x to v4.x. I am not getting the color during the help output as I got it in v3.x. Everything was completely white. I used the basic code from the example (https://github.com/clap-rs/clap/blob/master/examples/git.rs). Below are two images showing the output of v3 (the first one) and the production from v4 (the second one).

My question is, how to add the colors? v3

v4

I tried setting the color to Always but no help


Solution

  • Create the styles yourself

    pub fn get_styles() -> clap::builder::Styles {
        clap::builder::Styles::styled()
            .usage(
                anstyle::Style::new()
                    .bold()
                    .underline()
                    .fg_color(Some(anstyle::Color::Ansi(anstyle::AnsiColor::Yellow))),
            )
            .header(
                anstyle::Style::new()
                    .bold()
                    .underline()
                    .fg_color(Some(anstyle::Color::Ansi(anstyle::AnsiColor::Yellow))),
            )
            .literal(
                anstyle::Style::new().fg_color(Some(anstyle::Color::Ansi(anstyle::AnsiColor::Green))),
            )
            .invalid(
                anstyle::Style::new()
                    .bold()
                    .fg_color(Some(anstyle::Color::Ansi(anstyle::AnsiColor::Red))),
            )
            .error(
                anstyle::Style::new()
                    .bold()
                    .fg_color(Some(anstyle::Color::Ansi(anstyle::AnsiColor::Red))),
            )
            .valid(
                anstyle::Style::new()
                    .bold()
                    .underline()
                    .fg_color(Some(anstyle::Color::Ansi(anstyle::AnsiColor::Green))),
            )
            .placeholder(
                anstyle::Style::new().fg_color(Some(anstyle::Color::Ansi(anstyle::AnsiColor::White))),
            )
    }
    

    And then call it with styles

    #[command(styles=get_styles())]
    pub struct CliArgs {
    ...
    }