diff options
author | Teddy Wing | 2021-04-11 17:01:48 +0200 |
---|---|---|
committer | Teddy Wing | 2021-04-11 17:01:48 +0200 |
commit | 521b13211aac9e5f0c005bd0a1f804ec1e19b187 (patch) | |
tree | d4bc4bdb8fe49d513b4ccc5c7526f4be6f89dccf | |
parent | 5ba4efa907c8b4d91afb6cc0cfd7c7bb07f78158 (diff) | |
download | formurapid-521b13211aac9e5f0c005bd0a1f804ec1e19b187.tar.bz2 |
Get PDF file path from free command line argument
Add 'getopts' to parse command line arguments. Replace `--fill` argument
condition with 'getopts'. Replace the hard-coded PDF file path with one
from the command line argument.
-rw-r--r-- | Cargo.lock | 16 | ||||
-rw-r--r-- | Cargo.toml | 1 | ||||
-rw-r--r-- | src/main.rs | 16 |
3 files changed, 31 insertions, 2 deletions
@@ -332,12 +332,22 @@ name = "formurapid" version = "0.0.1" dependencies = [ "derive_builder", + "getopts", "pdf_forms", "serde", "toml", ] [[package]] +name = "getopts" +version = "0.2.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "14dbbfd5c71d70241ecf9e6f13737f7b5ce823821063188d7e46c41d371eebd5" +dependencies = [ + "unicode-width", +] + +[[package]] name = "gif" version = "0.11.2" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -855,6 +865,12 @@ dependencies = [ ] [[package]] +name = "unicode-width" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3" + +[[package]] name = "unicode-xid" version = "0.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -5,6 +5,7 @@ edition = "2018" [dependencies] derive_builder = "0.10.0" +getopts = "0.2.21" pdf_forms = "0.3.4" serde = { version = "1.0.125", features = ["derive"] } toml = "0.5.8" diff --git a/src/main.rs b/src/main.rs index 0577197..ff765e1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,5 @@ use derive_builder::Builder; +use getopts::Options; use pdf_forms::{Form, FieldType}; use serde::{Deserialize, Serialize}; @@ -27,9 +28,20 @@ struct Field<'a> { fn main() { let args: Vec<String> = env::args().collect(); - let mut form = Form::load("./f1040.pdf").unwrap(); + let mut opts = Options::new(); + opts.optflag("", "fill", "fill in the form using a markup file"); - if args.len() == 2 && args[1] == "--fill" { + let opt_matches = opts.parse(&args[1..]).unwrap(); + + if opt_matches.free.is_empty() { + return; + } + + let form_path = &opt_matches.free[0]; + + let mut form = Form::load(form_path).unwrap(); + + if opt_matches.opt_present("fill") { fill("./f1040.toml", &mut form); return; |