I have a clap App
like this:
let m = App::new("test")
.arg(
Arg::with_name("INPUT")
.help("a string to be frobbed")
.multiple(true),
)
.get_matches();
I want to read the arguments as an iterable of strings if there are any myapp str1 str2 str3
but if not, to act as a filter and read an iterable of lines from stdin cat afile | myapp
. This is my attempt:
let stdin = io::stdin();
let strings: Box<Iterator<Item = String>> = if m.is_present("INPUT") {
Box::new(m.values_of("INPUT").unwrap().map(|ln| ln.to_string()))
} else {
Box::new(stdin.lock().lines().map(|ln| ln.unwrap()))
};
for string in strings {
frob(string)
}
I believe that, since I am just requiring the Iterator
trait, a Box<Iterator<Item = String>>
is the only way to go. Is that correct?