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

Error text for derived numeric ranges is unclear #3199

Closed
2 tasks done
epage opened this issue Dec 20, 2021 · 3 comments
Closed
2 tasks done

Error text for derived numeric ranges is unclear #3199

epage opened this issue Dec 20, 2021 · 3 comments
Labels
A-derive Area: #[derive]` macro API C-bug Category: Updating dependencies S-waiting-on-design Status: Waiting on user-facing design to be resolved before implementing

Comments

@epage
Copy link
Member

epage commented Dec 20, 2021

Please complete the following tasks

  • I have searched the discussions
  • I have searched the existing issues

Rust Version

rustc 1.55.0 (c8dfcfe04 2021-09-06)

Clap Version

master

Minimal reproducible code

use clap::Parser;

/// Simple program to greet a person
#[derive(Parser, Debug)]
#[clap(about, version, author)]
struct Args {
    /// Name of the person to greet
    #[clap(short, long)]
    name: String,

    /// Number of times to greet
    #[clap(short, long, default_value_t = 1)]
    count: u8,
}

fn main() {
    let args = Args::parse();

    for _ in 0..args.count {
        println!("Hello {}!", args.name)
    }
}

Steps to reproduce the bug with the above code

In the clap repo

$ cargo run --example demo --features derive -- --name foo --count 20000000

Actual Behaviour

error: Invalid value for '--count <COUNT>': number too large to fit in target type

For more information try --help

Expected Behaviour

error: Invalid value for '--count <COUNT>': 20000000 too large, must be <255

For more information try --help

Additional Context

From reddit post https://www.reddit.com/r/rust/comments/rhxkau/clap_300rc7/hp9drzd/

Debug Output

No response

@epage epage added C-bug Category: Updating dependencies A-derive Area: #[derive]` macro API S-triage Status: New; needs maintainer attention. labels Dec 20, 2021
@epage
Copy link
Member Author

epage commented Dec 20, 2021

The challenge with fixing this is that the error is coming from FromStr. We don't inspect the actual type information to be able to provide a more user-meaningful error.

A workaround is for the user to provide their own parse function.

@epage
Copy link
Member Author

epage commented Feb 2, 2022

From the core of clap's perspective, this is a wont-fix. The user is supplying the type and relying on the default parser.

Two thoughts on where we can go with this

@epage epage added S-waiting-on-design Status: Waiting on user-facing design to be resolved before implementing and removed S-triage Status: New; needs maintainer attention. labels Feb 2, 2022
epage added a commit to epage/clap that referenced this issue May 17, 2022
There are several approaches with this
- `value_parser(N..M)`: creates an i64 range
- `value_parser(value_parser!(u16).range(10..))`: creates an u16 range
  that starts at 10
- `RangeI64ValueParser`: create whatever range you want

I was hoping to generalize `RangeI64ValueParser` for any source type,
not just i64, but ran into issues and decided to punt.  I chose `i64` as
the source type as it seemed the most general and didn't run into
portability issues like `isize`.

This is a step towards clap-rs#3199.  All that is left is for the derive to use
this.
@epage
Copy link
Member Author

epage commented May 25, 2022

Huh, this was supposed to be closed by #3743 3734

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-derive Area: #[derive]` macro API C-bug Category: Updating dependencies S-waiting-on-design Status: Waiting on user-facing design to be resolved before implementing
Projects
None yet
Development

No branches or pull requests

1 participant