From 93e64fa8dbdc376d83e827b95b13702e90b0f1cd Mon Sep 17 00:00:00 2001 From: Ade Attwood Date: Sat, 11 May 2024 21:44:16 +0100 Subject: [PATCH] feat: add input argument to read input from text or stdin You can now provide input as the content you want to search and replace in. If you don't provide input, the program will read from stdin. --- src/main.rs | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/main.rs b/src/main.rs index 1eaafda..65df5bd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,17 +11,26 @@ struct Args { /// The replacement pattern. #[arg(short, long)] replace: String, + + /// The input content to search and replace. If not provided, input will be read from stdin. + #[arg(short, long)] + input: Option, } fn main() { let args = Args::parse(); - let mut input = String::new(); - match std::io::stdin().read_to_string(&mut input) { - Ok(_) => (), - Err(err) => { - eprintln!("Error reading from stdin: {}", err); - return; + let input = match args.input { + Some(input) => input, + None => { + let mut input = String::new(); + match std::io::stdin().read_to_string(&mut input) { + Ok(_) => input, + Err(err) => { + eprintln!("Error reading from stdin: {}", err); + return; + } + } } };