src/main.rs

changeset 3
cec573b16b46
parent 2
254e1e4bd795
child 6
de1cf8032322
--- a/src/main.rs	Thu Oct 19 15:10:12 2023 -0500
+++ b/src/main.rs	Thu Oct 19 15:35:05 2023 -0500
@@ -2,7 +2,8 @@
 #![doc = include_str!("../README.md")]
 
 use std::io;
-use std::io::BufWriter;
+use std::fs::File;
+use std::io::{BufWriter, BufRead, BufReader};
 use std::io::Write;
 use clap::Parser;
 
@@ -14,6 +15,13 @@
     version = env!("CARGO_PKG_VERSION"),
 )]
 struct CommandLineArgs {
+    /// Input file (default is stdin)
+    input : Option<String>,
+
+    /// Output file (defalt is stdout)
+    #[arg(long, short = 'o')]
+    output : Option<String>,
+
     #[arg(long, short = 'c')]
     /// Strip comments
     strip_comments : bool,
@@ -113,12 +121,19 @@
 
 fn main() {
     let cli = CommandLineArgs::parse();
-    let input = io::stdin();
-
+    let input = cli.input.map_or_else(
+        || Box::new(BufReader::new(io::stdin())) as Box<dyn BufRead>,
+        |f| Box::new(BufReader::new(File::open(f).unwrap())) as Box<dyn BufRead>
+    );
+    let output = cli.output.map_or_else(
+        || Box::new(BufWriter::new(io::stdout())) as Box<dyn Write>,
+        |f| Box::new(BufWriter::new(File::create(f).unwrap())) as Box<dyn Write>
+    );
+    
     let mut o = Out {
         only_whitespace : true,
         stored_whitespace : String::new(),
-        output : BufWriter::new(io::stdout()),
+        output,
         stack : Vec::new(),
         whitespace_satisfied : true,
         par_satisfied : true,

mercurial