From 0acbf02b7e380ac352f3e090023d9f9b28fbdb30 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sun, 11 Apr 2021 19:25:29 +0200 Subject: Print usage on malformed command line arguments --- src/main.rs | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index aac82d7..8961809 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,6 +7,7 @@ use std::env; use std::fs::{File, OpenOptions}; use std::io::{Read, Write}; use std::path::Path; +use std::process; #[derive(Debug, Default, Deserialize, Serialize)] struct TextForm<'a> { @@ -25,6 +26,13 @@ struct Field<'a> { state: Option, } +fn print_usage(opts: &Options) { + print!( + "{}", + opts.usage("usage: formurapid [options] (--generate | --fill) PDF_FILE"), + ); +} + fn main() { let args: Vec = env::args().collect(); @@ -35,7 +43,9 @@ fn main() { let opt_matches = opts.parse(&args[1..]).unwrap(); if opt_matches.free.is_empty() { - return; + print_usage(&opts); + + process::exit(exitcode::USAGE); } let form_path = Path::new(&opt_matches.free[0]); @@ -52,8 +62,6 @@ fn main() { form_path.with_file_name(output_file_name), &mut form, ); - - return; } else if opt_matches.opt_present("generate") { let ids_path = form_path.with_file_name( format!("{}-ids.pdf", form_file_prefix) @@ -64,6 +72,10 @@ fn main() { ids_path, &mut form, ); + } else { + print_usage(&opts); + + process::exit(exitcode::USAGE); } } -- cgit v1.2.3