I'm Brand new to Rust and I have been writing some practice apps. I am trying to accept command-line arguments using Clap. The code below takes a string and a number and prints them back out, as such:
$ cargo run "this is a test" -n11
this is a test
11
This works fine, but I want to be able to pipe input in place of the string like this:
$ echo "this is a test" | cargo run -- -n11
this is a test
11
Trying this yields:
error: The following required arguments were not provided:
<INPUT>
USAGE:
clap_sample --num <num> <INPUT>
For more information try --help
I can get around this using xargs like this:
$ echo "this is a test" | xargs -d '\n' cargo run -- -n11
Is there a better way to do this so that I can accept piped in strings while still using the -n option? Thanks in advance.
use clap::{Arg, App};
fn main() {
let matches = App::new("Clap Sample")
.arg(Arg::new("INPUT")
.required(true)
.index(1))
.arg(Arg::new("num")
.short('n')
.long("num")
.takes_value(true))
.get_matches();
let usr_string = matches.value_of("INPUT").unwrap();
let key: u8 = matches.value_of("num").unwrap()
.parse()
.expect("NaN :(");
println!("{}", usr_string);
println!("{}", key);
}
Bonus question: If I pipe a string in with xargs, I can have newlines in the string (with delimiter set to \0) and they are reflected in the output. If I pass it directly without echo and xargs, a literal '\n' shows in the output. Is there a way to have the newline represented when run directly?