-
Notifications
You must be signed in to change notification settings - Fork 152
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
Custom collections #79
Comments
You can do https://github.com/TeXitoi/structopt/blob/master/examples/keyvalue.rs It may be interesting to be able to choose to use a dedicated collection, but that's really not trivial to propose a great syntax. Maybe something like: fn parse_key_val(s: &str) -> Result<(String, String), String> {
unimplemented!()
}
struct Opt {
#[structopt(short = "D", collect(try_from_str = "parse_key_val"))]
defines: HashMap<String, String>,
} |
I understand I can „force“ it to do what I want with custom functions and so on. My point was more about if structopt should be able to handle these out of the box. If there should be a way for it to recognize these other collections (sets could be easy) and do The Right Thing. But I admit I don't have a concrete idea how that should work exactly ‒ because I might want to specify how the separator looks like, or how to specify the parser for both left and right side. Maybe having a special-case of Anyway, that's mostly brainstorming. |
As structopt works at the macro level, there is no type information. You can't check if some type implement |
I understand you can't check if the type implements I understand having many special cases isn't good for the code. So if that wouldn't look good, does it make sense to provide some common parser functions as part of the library, so one could reuse them instead of implementing (I'm talking about the one from the example). The |
What about adding support for |
I'm not a big fun of specializing types that are not in |
I agree, and I'd prefere to support all the collections implementing FromIterator. |
Hey, @TeXitoi , care to join https://rust-lang.zulipchat.com/#narrow/stream/220302-wg-cli ? |
Any news on this? |
No, nothing new AFAIK. |
@TeXitoi Can the automatic |
@pickfire Could you explain what you mean? A piece of pseudocode would help a lot |
#[derive(StructOpt)]
struct Opt {
#[structopt(..., try_from_str = parse_format_string)]
// without format: Format,
format: Vec<Token>,
}
// without fn parse_format_string(input: &str) -> Format {
fn parse_format_string(input: &str) -> Vec<Token> {
...
}
// without type Format = Vec<Token>; |
This is not how validators in clap work: they check arguments one by one. |
No, I mean the function needs to return |
I believe a solution to "custom" collections should:
I think a good idea would be to first initialize a empty container (using some For example something like following rust sketch: use thiserror::Error;
use std::collections::HashMap;
[derive(Debug, StructOpt)[
struct Options {
#[structopt(
short="k",
// implies takes_value=true,multiple=true
try_fold(fold_kv_map),
)]
kv_map: HashMap<String, String>
}
fn fold_kv_map(
mut state: HashMap<String, String>,
raw: &str
) -> Result<HashMap<String, String>, Error> {
let (key, value) = parse_key_value(raw)?;
let old = state.insert(key.to_owned(), value.to_owned());
if old.is_some() {
Err(Error::DuplicateKey { key: key.to_owned() })
} else {
Ok(state)
}
}
#[derive(Debug, Error)]
enum Error {
#[error("...")]
MalformedKVPair,
#[error("...")]
DuplicateKey { key: String }
} By default Alternatively something like:
To not rely on EDIT: Shortened sketch example. I myself have run recently into this issue when I needed arguments like |
This is an enhancement, and structopt is now feature frozen. |
Hello
I discovered I'm missing one use case of command line parsing. Something like the
-D
parameter of GCC, where you can do-D NDEBUG=1
‒ basically way to parse key-value pairs.I'm not sure if there needs to be some support from clap, but AFAIK clap just returns bunch of strings for each parameter (if multiple are allowed) and type-conversion is up to structop.
So I was wondering, similar as structopt provides
Vec<T>
, it could be extended to eitherVec<(Key, Value)>
orHashMap/BTreeMap<Key, Value>
.Does that make sense? Or is a better place to add such feature?
The text was updated successfully, but these errors were encountered: