-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Suggestion: Allow arguments to be differently typed #817
Comments
Well, you have to write that logic somewhere. :-) If you want to keep it separate, you can use any of Rust's abstraction facilities to tuck that logic away. For example: struct ArgMatches<'a>(clap::ArgMatches<'a>);
impl<'a> ArgMatches<'a> {
fn get_xml_struct(&self) -> Result<XMLStruct, Error> {
let file_path = self.0.value_of("xml-file").unwrap();
...
}
} |
This has been on my mind since back in #146. There are some macros for doing this, but it's still a less than ideal solution. Aside, this is something that @BurntSushi's library docopt does really well and he's being super modest by not saying anything 😉 The biggest issue with doing this is the intermediate step of What I'd like to implement, and have been passivly waiting on macros 1.1, is a struct Args {
file_path: Option<XMLStruct>
} The one downside is you'd still have @BurntSushi's way is great too, and some also do a struct Args {
file_path: XMLStruct
}
impl<'a> From<ArgMatches<'a>> for Args {
fn from(m: ArgMatches) -> Self {
let file_path = m.value_of("xml-file").unwrap();
// ...
Args { files_path: file_path }
}
} Which is essentially the same thing, but allows you to do let args: Args = App::new("")
// stuff
.get_matches()
.into(); |
What is a situation where a combination of overrides and settings overrules It would be really nice if This might require a combination of defaults, "requires", and matching subcommands to (optional) embedded structs instead of flattening everything into a single namespace, but it feels in principle doable. |
@quodlibetor I apologize I missed your comment.
When requirements are overriden by subcommands or other arguments, and conditional requirements. This probably doesn't come up often and could probably be avoided by some CLI redesign, but still it could happen. The primary way I can think of to avoid this is make it clear that "required" args if overriden, etc. must implement Plus there are things like some CLIs where the subcommand is required, and others it's not. There are also CLIs where the use of a subcommand negates any args of the partent, to include required args.
I also think there should be two different conversion, one that can fail (returning a |
I'm going to close this issue as it's covered by two other open issues
|
Currently all arguments are always
stringly typed
. However, in some cases, it makes sense for the argument parser to return data of a different type. For example, my use case is this:The user passes an XML file name as a parameter. While parsing the arguments, I would like to validate if the file exists, is readable and has the expected schema. I would also like to parse the file at this point, extract the relevant data and store it in a
struct
. Hence, when the application callsmatches.get_value(arg)
, it returns the struct for the application to use. This allows keeping some of the logic separate and not polluting the actual program with such sanity checking logic.Unfortunately I do not have a suggestion on the API or how it can be designed. I am only starting to learn Rust and came across this is a feature that I would like in my application code
The text was updated successfully, but these errors were encountered: