Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve RichFormatter's multi-line error formatting #5550

Open
2 tasks done
mahor1221 opened this issue Jun 25, 2024 · 1 comment
Open
2 tasks done

Improve RichFormatter's multi-line error formatting #5550

mahor1221 opened this issue Jun 25, 2024 · 1 comment
Labels
A-help Area: documentation, including docs.rs, readme, examples, etc... C-enhancement Category: Raise on the bar on expectations M-breaking-change Meta: Implementing or merging this will introduce a breaking change.

Comments

@mahor1221
Copy link

mahor1221 commented Jun 25, 2024

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:

let _ = write!(styled, ": {source}");

@mahor1221 mahor1221 added the C-enhancement Category: Raise on the bar on expectations label Jun 25, 2024
@epage epage added M-breaking-change Meta: Implementing or merging this will introduce a breaking change. A-help Area: documentation, including docs.rs, readme, examples, etc... labels Jun 26, 2024
@epage
Copy link
Member

epage commented Jun 26, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-help Area: documentation, including docs.rs, readme, examples, etc... C-enhancement Category: Raise on the bar on expectations M-breaking-change Meta: Implementing or merging this will introduce a breaking change.
Projects
None yet
Development

No branches or pull requests

2 participants