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.
This commit is contained in:
Ade Attwood 2024-05-11 21:44:16 +01:00
parent 23868e9df1
commit 93e64fa8db

View file

@ -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<String>,
}
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;
}
}
}
};