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

Duplicated "error:" prefix #5579

Open
2 tasks done
d-e-s-o opened this issue Jul 10, 2024 · 4 comments
Open
2 tasks done

Duplicated "error:" prefix #5579

d-e-s-o opened this issue Jul 10, 2024 · 4 comments
Labels
C-bug Category: Updating dependencies

Comments

@d-e-s-o
Copy link

d-e-s-o commented Jul 10, 2024

Please complete the following tasks

Rust Version

rustc 1.79.0-nightly (129f3b996 2024-06-10) (gentoo)

Clap Version

4.5.4

Minimal reproducible code

use std::env::args_os;
use anyhow::Result;
use clap::Parser;

/// Simple program to greet a person
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
struct Args {
    /// Name of the person to greet
    #[arg(short, long)]
    name: String,
}

fn main() -> Result<()> {
    let _args = Args::try_parse_from(args_os())?;
    Ok(())
}

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=6221486eaf814c633157f1c9fdf27c12

Steps to reproduce the bug with the above code

cargo run

Actual Behaviour

Prints:

Error: error: the following required arguments were not provided:
  --name <NAME>

Usage: playground --name <NAME>

For more information, try '--help'.

Expected Behaviour

Prints:

Error: the following required arguments were not provided:
  --name <NAME>

Usage: playground --name <NAME>

For more information, try '--help'.

Additional Context

The "error:" prefix is duplicated. I find this behavior irritating. One prefix seems to be added by the standard library, the other by clap (I don't believe clap is alone with such behavior)

In my opinion the standard library's behavior is questionable -- it's just way too opinionated and this is a good example -- but I wanted to get your guys' take and have an issue to reference to make the case for changing it (which I suppose may be a hard sell irrespectively).

In general, though, it also feels as if just the typical wording of "failed to [...]" or similar would be sufficient for convey the issue. Meaning that there is no need for an "error:" at all, even from the clap side. It just depends too much on the context how the error is reported whether it makes sense to include such a prefix or not.

Any opinions/comments/thoughts?

Debug Output

No response

@d-e-s-o d-e-s-o added the C-bug Category: Updating dependencies label Jul 10, 2024
@epage
Copy link
Member

epage commented Jul 10, 2024

Is there a reason you are using try_parse_from, rather than parse?

@d-e-s-o
Copy link
Author

d-e-s-o commented Jul 10, 2024

Is there a reason you are using try_parse_from, rather than parse?

Yep.

@epage
Copy link
Member

epage commented Aug 13, 2024

This isn't the only peculiarity about claps' error reporting that doesn't make it work well with policies like anyhow. For example, you likely should condition your exit code based on er.use_stderr() (not a great name) or else --help is considered an error when its generally recognized as not one.

We need more information to better understand your use case to determine what, if anything, we should be done about this.

@d-e-s-o
Copy link
Author

d-e-s-o commented Aug 18, 2024

This has very little if not nothing to do with anyhow from what I can tell.

My use case is using regular error reporting paths as every other crate does and have a single program exit path. If every crate thinks it knows best when to exit then I have no control over or idea of what is going on. The same is true (and, to the best I can tell, accepted practice) for panics: you can unwrap or panic on invariants and everything else (certainly everything controllable by a user) shouldn't panic, but report a handleable error.

I assume that is the reason why the try_parse_from API exist in the first place, right?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Updating dependencies
Projects
None yet
Development

No branches or pull requests

2 participants