aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTeddy Wing2021-05-04 23:38:57 +0200
committerTeddy Wing2021-05-04 23:38:57 +0200
commiteb83cc416bbe2879b18397f9c81db644451687fa (patch)
treed1012671066816b262e6c8e001647f2cc3b9de84
parente12178ffac507065d96ae8a5e1e3f33c6d05833a (diff)
downloadpdf-form-replace-font-eb83cc416bbe2879b18397f9c81db644451687fa.tar.bz2
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.
-rw-r--r--src/main.rs22
1 files changed, 18 insertions, 4 deletions
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<String> = 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());