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

Bring back possible_values (because ValueEnum does not fit everything) #4504

Closed
2 tasks done
KSXGitHub opened this issue Nov 23, 2022 · 3 comments · Fixed by #4509
Closed
2 tasks done

Bring back possible_values (because ValueEnum does not fit everything) #4504

KSXGitHub opened this issue Nov 23, 2022 · 3 comments · Fixed by #4509
Labels
C-enhancement Category: Raise on the bar on expectations

Comments

@KSXGitHub
Copy link

KSXGitHub commented Nov 23, 2022

Please complete the following tasks

Clap Version

4.0.26

Describe your use case

There are cases where I want to only limit user choices between strings. The behavior of the program remain exactly the same (i.e. no branches related to the user choice). For example: Choosing a weekday only to be printed out, choosing a profile between "release" and "debug" only to infer the binary path, etc.

In such cases, an enum (that derives ValueEnum) would be inconvenient since it requires me to write match to convert every single enum variant to corresponding strings.

Describe the solution you'd like

Bring back possible_values or something similar.

Alternatives, if applicable

Derive strum on the value enum. However, this method is not optimal as the resulting program still parse a string into a value enum only to convert it back to the original string.

Use value_parser = [PossibleValue::new("..."), ...] but this is unwieldy and cumbersome, especially when written in derive code.

Additional Context

I exclusively use #[derive(clap::Parser)]. I've never use the args builder directly.

@KSXGitHub KSXGitHub added the C-enhancement Category: Raise on the bar on expectations label Nov 23, 2022
@epage
Copy link
Member

epage commented Nov 23, 2022

Use value_parser = [PossibleValue::new("..."), ...] but this is unwieldy and cumbersome, especially when written in derive code.

You can also do value_parser = ["always", "auto", "never"]) as shown in the (builder) example for Arg::value_parser

@KSXGitHub
Copy link
Author

@epage I apologize, the fault lies with me for not reading the documentation carefully. I searched for the term "possible" on the docs.rs page for clap and find only the PossibleValue struct. The example for PossibleValue does not includes this convenient shorthand. May I request adding this shorthand to the code examples for PossibleValue?

@timvisee
Copy link

@epage

.value_parser(["always", "auto", "never"]) only seems to accept a list as array.

It doesn't seem to be possible to pass a runtime slice or Vec holding strings into there, or am I missing something?

epage added a commit to epage/clap that referenced this issue Nov 24, 2022
epage added a commit to epage/clap that referenced this issue Nov 24, 2022
This dynamically generated list of possible values.  Inspired by clap-rs#4504
epage added a commit to epage/clap that referenced this issue Nov 24, 2022
epage added a commit to epage/clap that referenced this issue Nov 24, 2022
This dynamically generated list of possible values.  Inspired by clap-rs#4504
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: Raise on the bar on expectations
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants