clap.rs 在 `--help` 期间不打印颜色

The*_*Say 9 rust clap

所以我从 clap v3.x 迁移到 v4.x。我在帮助输出期间没有得到颜色,就像我在 v3.x 中得到的那样。一切都是完全白色的。我使用了示例中的基本代码(https://github.com/clap-rs/clap/blob/master/examples/git.rs)。下面的两张图片显示了 v3 的输出(第一个)和 v4 的输出(第二个)。

我的问题是,如何添加颜色? v3

v4

我尝试将颜色设置为“始终”但没有帮助

P22*_*P22 15

自己创建样式

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))),
        )
}
Run Code Online (Sandbox Code Playgroud)

然后用样式调用它

#[command(styles=get_styles())]
pub struct CliArgs {
...
}
Run Code Online (Sandbox Code Playgroud)

  • 这绝对应该是问题的*答案*,而不是建议使用 Python 的另一个答案...... (7认同)
  • 仅供参考,看来样式现在被认为是稳定的,并且“unstable_styles”功能已被删除:https://github.com/clap-rs/clap/pull/5094 (2认同)

Eth*_*S-L 7

颜色默认值在 clap v4 中被删除(非常遗憾)。
我同意这是一个巨大的倒退——因为它使用户扫描变得更加困难,并且对大多数人来说使用起来不太愉快。

主要作者的原因归结为想要支持所有内容,而颜色并不出现在所有内容上。

他们正在研究可选颜色。所以……也许有一天。

解决方案:只需使用v3,直到出现另一个解决方案。(要么是更好的板条箱,要么是更好的拍手选项。)v4 有许多风格上的回归,包括不太友好的错误消息

我半认真地提出了一个替代解决方案:Rust 具有出色的外部函数接口。您可以使用另一种语言来前端处理 rust,并使用更符合人体工程学和现代的 CLI 框架,例如通过PyO3提供接口的 Python Typer