clap-rs / clap

A full featured, fast Command Line Argument Parser for Rust
docs.rs/clap
Apache License 2.0
14.32k stars 1.05k forks source link

Allow templates to change the error message strings #2196

Closed filipelbc closed 2 years ago

filipelbc commented 4 years ago

Before anything, this library is great! Thanks!

Make sure you completed the following tasks

Describe your use case

I prefer printing ERROR: ... instead of error: ..., and to make the background red, instead of the foreground.

Describe the solution you'd like

Allow the user to set the "error: " string, which is currently hard-coded, see https://github.com/clap-rs/clap/blob/97b4fb639f846c5b910c318dd6c6f7d6da779d7d/src/parse/errors.rs#L401

Allow the user to change the colors, currently hard-coded, see https://github.com/clap-rs/clap/blob/97b4fb639f846c5b910c318dd6c6f7d6da779d7d/src/output/fmt.rs#L53

You could create a StyleSetting struct and a mechanism similar to AppSettings.

struct StyleSetting {
    string: &str,
    style: termcolor::ColorSpec,
}

enum StylePoint {
    ErrorStart,
    UsageStart,
    ...
}

let my_error_style = StyleSetting::new("ERROR: ", my_error_color_spec);

App::new("Foo")
    ...
    .style_setting(ErrorStart, my_error_style)
    ...

Additional context

Similar to https://github.com/clap-rs/clap/issues/2035, but instead of allowing usage of Clap's error formatting, allow the user to customize Clap's messages and style to fit his preferences.

pksunkara commented 4 years ago

Note: Same as #1433 but this issue needs error message template support.

pksunkara commented 4 years ago

Might also be related to #1734

epage commented 2 years ago

This request seems most like #1790 which we closed because we are not looking at allowing granular styling of claps API, instead favoring templates and #1433. Since we have #1734 for an error message template and #1433 for styling it, I'm going to go ahead and close this out.

If there is any concern with this plan, let us know!