Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

rust

clap

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

like image 255
TheFishTheySay Avatar asked Dec 29 '25 00:12

TheFishTheySay


1 Answers

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 {
...
}
like image 95
P22 Avatar answered Dec 30 '25 17:12

P22



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!