From 0b12b2bae1130746ed49cc3c7a2daa819ede1b58 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sat, 23 Apr 2016 02:18:14 -0400 Subject: Specify alias file as a command line argument * Add a dependency on 'getopts' * Remove the hard-coded "testaliases" file used previously. * write_alias: Update to include a `file` attribute that can reference an arbitrary file * find_alias_in_file: Change the `file` parameter to be a Path reference instead of a string so that it can be called correctly from `write_alias`. Also because it matches the File module's signature. Originally I planned to make the file argument available under a `-f` command-line option. Later I decided instead to make it a required argument, so it made more sense not to prefix it with an option flag. Since I no longer need command line options—just the first argument—I realised that I could get rid of the `getopts` dependency and use `std::env::args`. Will do this in a later commit. --- Cargo.lock | 8 ++++++++ Cargo.toml | 1 + src/main.rs | 34 +++++++++++++++++++++++++++++----- 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 81030eb..8db7560 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,4 +1,12 @@ [root] name = "alias-auto-add" version = "0.0.1" +dependencies = [ + "getopts 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "getopts" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" diff --git a/Cargo.toml b/Cargo.toml index 4fa9b0b..582992c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,3 +3,4 @@ name = "alias-auto-add" version = "0.0.1" [dependencies] +getopts = "0.2" diff --git a/src/main.rs b/src/main.rs index ae7bf17..6b9d79e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,7 @@ +extern crate getopts; + +use getopts::Options; +use std::env; use std::error::{self, Error}; use std::io::{self, BufRead, BufReader, Write}; use std::fmt; @@ -65,11 +69,11 @@ impl Alias { } } -fn write_alias(from: String) -> Result<(), AliasSearchError> { +fn write_alias>(from: String, file: P) -> Result<(), AliasSearchError> { let mut alias = Alias::new(&from); - let similar_aliases = try!(find_alias_in_file(&alias, "./testaliases")); + let similar_aliases = try!(find_alias_in_file(&alias, &file)); alias.update_alias_id(similar_aliases); - try!(alias.write_to_file("./testaliases")); + try!(alias.write_to_file(&file)); Ok(()) } @@ -133,7 +137,7 @@ impl PartialEq for AliasSearchError { } } -fn find_alias_in_file(alias: &Alias, file: &str) -> Result, AliasSearchError> { +fn find_alias_in_file>(alias: &Alias, file: P) -> Result, AliasSearchError> { let mut matches = Vec::new(); let f = try!(File::open(file)); let file = BufReader::new(&f); @@ -157,7 +161,27 @@ fn find_alias_in_file(alias: &Alias, file: &str) -> Result, AliasSea } } +fn print_usage(program: &str) { + println!("Usage: {} FILE", program); +} + fn main() { + let args: Vec = env::args().collect(); + let program = args[0].clone(); + let opts = Options::new(); + + let opt_matches = match opts.parse(&args[1..]) { + Ok(m) => m, + Err(f) => panic!(f.to_string()), + }; + + let file = if !opt_matches.free.is_empty() { + opt_matches.free[0].clone() + } else { + print_usage(&program); + return; + }; + let stdin = io::stdin(); for line in stdin.lock().lines() { @@ -167,7 +191,7 @@ fn main() { println!("{}", line); if line.starts_with("From: ") { - match write_alias(line) { + match write_alias(line, &file) { Ok(_) => continue, Err(e @ AliasSearchError::NotFound) | Err(e @ AliasSearchError::EmailExists) => io::stderr().write(e.to_string().as_bytes()).ok(), -- cgit v1.2.3