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

Introduce possible_values_t #3201

Closed
2 tasks done
Gordon01 opened this issue Dec 21, 2021 · 4 comments
Closed
2 tasks done

Introduce possible_values_t #3201

Gordon01 opened this issue Dec 21, 2021 · 4 comments
Labels
A-derive Area: #[derive]` macro API C-enhancement Category: Raise on the bar on expectations E-medium Call for participation: Experience needed to fix: Medium / intermediate

Comments

@Gordon01
Copy link

Please complete the following tasks

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

Clap Version

3.0.0-rc.7

Describe your use case

possible_values are not chacked for ranges at compile time, like a default_value_t and this slightly reduce the robustness of the code.

For example, this code compiles and fails at runtime:

#[clap(short, long, default_value_t = 16, possible_values = ["16", "512"])]
fat: u8,

Describe the solution you'd like

The following code should result in error like this:

#[clap(short, long, default_value_t = 16, possible_values_t = [16, 512])]
fat: u8,

error: literal out of range for `u8`
note: the literal `512` does not fit into the type `u8` whose range is `0..=255`

Alternatives, if applicable

No response

Additional Context

No response

@Gordon01 Gordon01 added the C-enhancement Category: Raise on the bar on expectations label Dec 21, 2021
@epage epage added the A-derive Area: #[derive]` macro API label Dec 21, 2021
@epage
Copy link
Member

epage commented Dec 21, 2021

A not-great workaround until this is implemented is to use ArgEnum where you #[clap(name = "N")] each variant.

I've normally thought of possible values for enums. It makes sense to also support numbers. I am curious though, what is your use case for only accepting specific numbers?

@epage epage added the E-medium Call for participation: Experience needed to fix: Medium / intermediate label Dec 21, 2021
@Gordon01
Copy link
Author

I am curious though, what is your use case for only accepting specific numbers?

FAT filesystem type (12, 16 or 32)

@epage
Copy link
Member

epage commented May 17, 2022

#3732 will have an impact on how we design / expose this

@epage
Copy link
Member

epage commented May 25, 2022

With #3732 and friends, possible_values has been deprecated and replaced by value_parser, so I'm closing this out

@epage epage closed this as completed May 25, 2022
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-enhancement Category: Raise on the bar on expectations E-medium Call for participation: Experience needed to fix: Medium / intermediate
Projects
None yet
Development

No branches or pull requests

2 participants