# HG changeset patch # User Tuomo Valkonen # Date 1697747705 18000 # Node ID cec573b16b462afccd6bfe3c144562512a03d42f # Parent 254e1e4bd79517d06421f9759bcb37de0c8535b2 Add command line options for input and output files diff -r 254e1e4bd795 -r cec573b16b46 README.md --- 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). diff -r 254e1e4bd795 -r cec573b16b46 src/main.rs --- 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, + + /// Output file (defalt is stdout) + #[arg(long, short = 'o')] + output : Option, + #[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, + |f| Box::new(BufReader::new(File::open(f).unwrap())) as Box + ); + let output = cli.output.map_or_else( + || Box::new(BufWriter::new(io::stdout())) as Box, + |f| Box::new(BufWriter::new(File::create(f).unwrap())) as Box + ); + 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,