clap-rs / clap

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

Improve RichFormatter's multi-line error formatting #5550

Open mahor1221 opened 1 week ago

mahor1221 commented 1 week ago

Please complete the following tasks

Clap Version

4.5.7

Describe your use case

I return multi-line errors in some parsers, which causes minor formatting issues when using RichFormatter. For example:

#[derive(Parser)]
pub struct Cli {
    ...
    #[arg(long, short, value_parser = TransitionScheme::from_str)]
    #[arg(value_name = "TIME-TIME | DEGREE:DEGREE")]
    pub scheme: Option<TransitionScheme>,
    ...
}

Current format:

$: cargo run -- --scheme asdf:ghjk
error: invalid value 'asdf:ghjk' for '--scheme <TIME-TIME | DEGREE:DEGREE>': as time ranges:
- invalid format
as elevation range:
- invalid float literal (asdf)
- invalid float literal (ghjk)

For more information, try '--help'.

Desired format:

$: cargo run -- --scheme asdf:ghjk
error: invalid value 'asdf:asdf' for '--scheme <TIME-TIME | DEGREE:DEGREE>': # a new line is added here
as time ranges:
- invalid format
as elevation range:
- invalid float literal (asdf)
- invalid float literal (ghjk)

For more information, try '--help'.

Describe the solution you'd like

Please note that I'm not very familiar with the clap's code base. Should this feature even be added or not?

A possible solution (but most likely a breaking change) would be to check if the formatted source error is multi-line, and if so, add a newline character to ensure proper formatting.

Another possible solution would be to make RichFormatter configurable.

Alternatives, if applicable

I've created a patch for my own use case here. It was very hard to make :) https://github.com/clap-rs/clap/compare/v4.5.7...mahor1221:clap:patch?expand=1

Additional Context

Here is the location of the issue: https://github.com/clap-rs/clap/blob/6c6839a454c2cbfc3007e6a2b2a55dd64685771e/clap_builder/src/error/format.rs#L370

epage commented 6 days ago

This is a tough situation because it happens to be a problem in your case due to the lack of alignment but in other cases it can be neutral or better the existing way.