feat: allow reading numbers from stdin #1
cargo devel CI / cargo CI (push) Successful in 1m16s Details

This commit is contained in:
Christoph J. Scherr 2024-05-13 16:26:35 +02:00
parent 1f4818ff98
commit 00c6b35ef3
1 changed files with 29 additions and 1 deletions

View File

@ -3,13 +3,41 @@
//! This binary should just take any amount of numbers and print them out formatted to some other
//! system.
use std::io::Read;
use std::process::exit;
use clap::Parser;
mod format;
use format::*;
fn main() {
let options = FormatOptions::parse();
// try to read from stdin first, appending the numbers we read to the FormatOptions
let mut args: Vec<String> = std::env::args_os()
.map(|x| x.into_string().unwrap())
.collect();
let mut stdin_nums = Vec::new();
match std::io::stdin().lock().read_to_end(&mut stdin_nums) {
Ok(_) => {
let whole: String = match String::from_utf8(stdin_nums) {
Ok(r) => r,
Err(e) => {
eprintln!("stdin for this program only accepts text: {e:#?}");
exit(1);
}
};
let whole = whole.replace('\n', "");
for s in whole.split(' ') {
args.push(s.to_string());
}
}
Err(e) => {
eprintln!("could not read from stdin: {e:#?}");
exit(2);
}
};
let options = FormatOptions::parse_from(args);
let mut out: Vec<String> = Vec::new();