From eb83cc416bbe2879b18397f9c81db644451687fa Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Tue, 4 May 2021 23:38:57 +0200 Subject: Add `--help` and `--version` command line options Make `--find` and `--replace` options optional instead of required, otherwise they cause errors when `--help` or `--version` are used independently. --- src/main.rs | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 957dbe6..00c1493 100644 --- a/src/main.rs +++ b/src/main.rs @@ -22,8 +22,8 @@ fn run () -> Result<(), anyhow::Error> { let args: Vec = env::args().collect(); let mut opts = Options::new(); - opts.reqopt("f", "find", "original font", ""); - opts.reqopt("r", "replace", "replacement font", ""); + opts.optopt("f", "find", "original font", ""); + opts.optopt("r", "replace", "replacement font", ""); opts.optopt("o", "output", "output file", "FILE"); opts.optflag("h", "help", "print this help menu"); @@ -31,6 +31,20 @@ fn run () -> Result<(), anyhow::Error> { let opt_matches = opts.parse(&args[1..])?; + if opt_matches.opt_present("h") { + print!( + "{}", + opts.usage("usage: pdf-form-replace-font --fill ORIGINAL_FONT --replace REPLACEMENT_FONT [-o FILE] PDF_FILE"), + ); + + process::exit(exitcode::USAGE); + } + + if opt_matches.opt_present("V") { + println!("{}", env!("CARGO_PKG_VERSION")); + process::exit(exitcode::OK); + } + let input_pdf = if opt_matches.free.is_empty() { "-" } else { @@ -38,9 +52,9 @@ fn run () -> Result<(), anyhow::Error> { }; let find = opt_matches.opt_str("find") - .ok_or(anyhow::anyhow!("no original font"))?; + .ok_or(anyhow::anyhow!("required option 'find' missing"))?; let replace = opt_matches.opt_str("replace") - .ok_or(anyhow::anyhow!("no replacement font"))?; + .ok_or(anyhow::anyhow!("required option 'replace' missing"))?; let output_pdf = opt_matches.opt_str("output") .unwrap_or("-".to_owned()); -- cgit v1.2.3