add an attribute on option argument to let `from_str_fn` can return `Vec<T>` or `Option<T>` as-is
- Dominant language
- Rust
- Stars
- 2k
- Forks
- 102
- PR merge metrics
- No merged PRs in 30d
Description
In some cases I don't expect `Vec` to mean that the argument can be repeated, or `Option` to mean that the argument is optional.
For example an argument expects a comma-separated list of integers, which I would expect to be parsed by `from_str_fn` as `Vec`:
```rust
#[derive(FromArgs)]
/// some description
struct Args {
/// some description
#[argh(option, from_str_fn(parse_list))]
list: Vec,
}
fn parse_list(s: &str) -> Result, String> {
s.split(",").map(|n| n.parse()).collect::, _>>().map_err(|err| format!("invaild number {}", err))
}
```
The compiler raised error:
```
mismatched types
expected enum `Result`
found enum `Result, _>`
```
I had to create a newtype:
```rust
#[derive(FromArgs)]
/// some description
struct Args {
/// some description
#[argh(option, from_str_fn(parse_list))]
list: List,
}
struct List(pub Vec);
fn parse_list(s: &str) -> Result {
match s.split(",").map(|n| n.parse()).collect::, _>>() {
Ok(list) => Ok(List(list)),
Err(err) => Err(format!("invaild number {}", err)),
}
}
```
If an attribute can be provided to make `from_str_fn` return `Vec` or `Option` as-is, the newtype will not be needed.
Contributor guide
Assessment
This issue has not been assessed yet.