Skip to content

Commit 81b6ba8

Browse files
committed
add support for stdin parsing
1 parent 58e0c35 commit 81b6ba8

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

src/main.rs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
use clap::Parser;
22
use regex::Regex;
3+
use std::io::{self, Read};
34

45
#[derive(Parser)]
56
struct Cli {
6-
/// The input string
7-
input: String,
87
/// The template string
98
template: String,
9+
/// The input string (if not provided, reads from stdin)
10+
input: Option<String>,
1011
}
1112

1213
#[derive(Debug, Clone)]
@@ -447,9 +448,30 @@ fn process(input: &str, template: &str) -> Result<String, String> {
447448
apply_ops(input, &ops)
448449
}
449450

451+
fn read_stdin() -> Result<String, String> {
452+
let mut buffer = String::new();
453+
io::stdin()
454+
.read_to_string(&mut buffer)
455+
.map_err(|e| format!("Failed to read from stdin: {}", e))?;
456+
Ok(buffer)
457+
}
458+
450459
fn main() {
451460
let cli = Cli::parse();
452-
match process(&cli.input, &cli.template) {
461+
462+
// Get input from argument or stdin
463+
let input = match cli.input {
464+
Some(input) => input,
465+
None => match read_stdin() {
466+
Ok(input) => input,
467+
Err(e) => {
468+
eprintln!("Error: {}", e);
469+
std::process::exit(1);
470+
}
471+
},
472+
};
473+
474+
match process(&input, &cli.template) {
453475
Ok(result) => println!("{}", result),
454476
Err(e) => {
455477
eprintln!("Error: {}", e);

0 commit comments

Comments
 (0)