Explorar o código

Extract CLI argument handling into a module

timvisee %!s(int64=7) %!d(string=hai) anos
pai
achega
a2e5df3c41
Modificáronse 2 ficheiros con 46 adicións e 17 borrados
  1. 41 0
      src/arg_handler.rs
  2. 5 17
      src/main.rs

+ 41 - 0
src/arg_handler.rs

@@ -0,0 +1,41 @@
+extern crate clap;
+
+use self::clap::{Arg, ArgMatches, App};
+
+use app::*;
+
+/// CLI argument handler.
+pub struct ArgHandler<'a> {
+    matches: ArgMatches<'a>,
+}
+
+impl<'a: 'b, 'b> ArgHandler<'a> {
+    /// Parse CLI arguments.
+    pub fn parse() -> ArgHandler<'a> {
+        // Handle/parse arguments
+        let matches = App::new(APP_NAME)
+            .version(APP_VERSION)
+            .author(APP_AUTHOR)
+            .about(APP_ABOUT)
+            .arg(Arg::with_name("file")
+                .short("f")
+                .long("file")
+                .value_name("PATH")
+                .help("The file to upload")
+                .required(true)
+                .multiple(false))
+            .get_matches();
+
+        // Instantiate
+        ArgHandler {
+            matches,
+        }
+    }
+
+    /// Get the selected file to upload.
+    pub fn file(&'a self) -> &'a str {
+        self.matches.value_of("file")
+            .expect("please specify a file to upload")
+    }
+}
+

+ 5 - 17
src/main.rs

@@ -1,4 +1,3 @@
-extern crate clap;
 extern crate hyper;
 extern crate mime_guess;
 extern crate open;
@@ -9,6 +8,7 @@ extern crate reqwest;
 extern crate serde_derive;
 
 mod app;
+mod arg_handler;
 mod b64;
 mod crypto;
 mod metadata;
@@ -18,35 +18,23 @@ use std::fs::File;
 use std::io::BufReader;
 use std::path::Path;
 
-use clap::{App, Arg};
 use openssl::symm::{Cipher, encrypt_aead};
 use rand::{Rng, thread_rng};
 use reqwest::header::Authorization;
 use reqwest::mime::APPLICATION_OCTET_STREAM;
 use reqwest::multipart::Part;
 
-use app::*;
+use arg_handler::ArgHandler;
 use crypto::{derive_auth_key, derive_file_key, derive_meta_key};
 use metadata::{Metadata, XFileMetadata};
 use reader::EncryptedFileReaderTagged;
 
 fn main() {
-    // Handle CLI arguments
-    let matches = App::new(APP_NAME)
-            .version(APP_VERSION)
-            .author(APP_AUTHOR)
-            .about(APP_ABOUT)
-        .arg(Arg::with_name("file")
-            .short("f")
-            .long("file")
-            .value_name("PATH")
-            .help("The file to upload")
-            .required(true)
-            .multiple(false))
-        .get_matches();
+    // Parse CLI arguments
+    let arg_handler = ArgHandler::parse();
 
     // Get the path
-    let path = Path::new(matches.value_of("file").unwrap());
+    let path = Path::new(arg_handler.file());
 
     // Make sure the path is a file
     if !path.is_file() {