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

Feature Request: choices #138

Open
bbstilson opened this issue Aug 1, 2022 · 5 comments
Open

Feature Request: choices #138

bbstilson opened this issue Aug 1, 2022 · 5 comments

Comments

@bbstilson
Copy link

bbstilson commented Aug 1, 2022

Similar to the click arg parse library from Python, it would be nice if argh had a similar feature. I am new to rust, so I can't suggest a good interface, but perhaps something like:

#[derive(FromArgs, Debug)]
struct Args {
    #[argh(choice, choices = vec!["foo", "bar", "baz"])]
    thing_type: String
}

but an enum might be a better option:

#[derive(Debug)]
enum Choice {
    Foo, Bar, Baz
}

#[derive(FromArgs, Debug)]
struct Args {
    #[argh(choice, choices = Choice)]
    thing_type: Choice
}

Perhaps this is possible already, but I couldn't figure out how to do so given the example and readme. Happy to close this if it is already possible.

Thanks!

@erickt
Copy link
Collaborator

erickt commented Oct 12, 2022

Yeah this would be interesting. We don’t have a built in way to do this at the moment, but you could use a helper function to do it. See this test for an example: https://github.com/google/argh/blob/master/argh/tests/lib.rs#L67. You’d just have to manually parse the argument into your enum.

@athre0z
Copy link

athre0z commented Dec 20, 2022

My preference for this would be another proc macro to annotate enums with, similar to how sub-commands are defined, except that these enums would not be allowed to have any data associated. Clap has this feature and calls it ValueEnum:

https://docs.rs/clap/latest/clap/trait.ValueEnum.html

@grantshandy
Copy link

@bbstilson this is definitely possible with current argh. The intended way you're supposed to do this is by defining an enum with your "choices" then implementing FromStr on it. If your String input is only supposed to represent a finite number of values you should re-think storing it as a String, that's what enum was created for.

Good Luck!

@thcrt
Copy link

thcrt commented Dec 16, 2023

This would be very nice for ergonomics. It would also mean that --help could include accepted values for the argument.

@LunarTulip
Copy link

Yeah. Currently, setting up an enum with FromStr leads to invalid choice-inputs producing very unhelpful error messages like this one:

Error parsing option '-f' with value 'bbq': 

Run trace-cli.exe --help for more information.

...where the help menu then fails to actually provide any more information, so the message is just leading the users on a wild goose chase. Thus, for now, I'm taking in string input and running parsing-into-enum on it later, within the function-that-uses-that-arg, with a manually-specified helpful error message. But, if more properly supported enum-choices-input were enabled, that'd be very nice as an upgrade over that sort of manual handling.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants