Thu, 19 Oct 2023 15:35:05 -0500
Add command line options for input and output files
README.md | file | annotate | diff | comparison | revisions | |
src/main.rs | file | annotate | diff | comparison | revisions |
--- a/README.md Thu Oct 19 15:10:12 2023 -0500 +++ b/README.md Thu Oct 19 15:35:05 2023 -0500 @@ -6,11 +6,14 @@ ## Installation and usage 1. Install [Rust](https://www.rust-lang.org) following instructions. -2. Run `cargo build` to compile the program (optional; the next step does it if needed). -3. To process a document, use +2. To compile and install the program (typically in `~/.cargo/bin/`), run + ``` + cargo install --path=. ``` - cargo run < input.tex > output.tex + If you would prefer just building or running the program without installing, use `cargo build` or `cargo run`. +4. To process a document, use ``` - You may also find the built binary under `target/debug/strip-changes-markup`, and copy it to a place that works for you, instead of using `cargo` to run the program. + strip-change-markup input.tex -o output.tex + ``` - \ No newline at end of file +For further options, such as whitespace and comment stripping, use the `--help` argument (`cargo run -- --help` when running through `cargo` without installing).
--- 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,