5

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

TheFishTheySay
  • 190
  • 2
  • 17
  • 2
    This is documented as part of [polishing `--help` output](https://github.com/clap-rs/clap/issues/4132) and [PR4117](https://github.com/clap-rs/clap/pull/4117) – Jmb Oct 14 '22 at 12:20
  • 2
    So we don't have any option to rollback to the original one till now in v4.x, right? – TheFishTheySay Oct 14 '22 at 12:36
  • 4
    I have the same problem and tried lots of tutorials, however I do not get the colors. The help page does not look modern at all. – iuiz Dec 21 '22 at 12:41
  • I had the same problem and still haven't found a solution – Gamelife Mar 07 '23 at 15:31

2 Answers2

6

Color defaults were (very sadly) removed in v4 of clap.
I agree that this is huge regression -- as it makes scanning by users much more difficult and usage less pleasant for most people.

The main author's reason comes down to wanting to support everything and color not being on everything.

They are working on optional colors. So ... someday, maybe.

Solution: Just use v3 until another solution comes up. (Either a better crate or better options for clap.) v4 has a number of stylistic regressions, including less friendly error messages.

An alternate solution, that I present only half-seriously: Rust has excellent foreign-function interface. You could front-end your rust with another language and use a more ergonomic and modern CLI framework like Python's Typer with interface via PyO3.

Ethan S-L
  • 321
  • 3
  • 6
  • Regarding error message quality, this has since improved in [this issue](https://github.com/clap-rs/clap/issues/4638), particularly due to [this PR](https://github.com/clap-rs/clap/pull/4798). – Wilfred Hughes Aug 03 '23 at 18:43
2

First you need to enable the feature unstable_styles, add a style library

cargo add clap -F unstable-styles
cargo add anstyle

or

clap = { version = "4.3.21", features = ["derive", "unstable-styles"] }
anstyle = "1.0.1"

Next you need to create the styles yourself

pub fn get_styles() -> clap::builder::Styles {
    clap::builder::Styles::styled()
        .usage(
            anstyle::Style::new()
                .fg_color(Some(anstyle::Color::Ansi(anstyle::AnsiColor::Yellow)))
                .bold(),
        )
        .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))),
        )
}

And then call it with styles

#[command(styles=get_styles())]
pub struct CliArgs {
...
}
Praveen Perera
  • 158
  • 3
  • 13
  • 1
    This should definitively be *the* answer to the question, not the other one that suggests using Python... – jthulhu Aug 16 '23 at 19:23