diff options
| author | Teddy Wing | 2016-04-23 02:18:14 -0400 |
|---|---|---|
| committer | Teddy Wing | 2016-04-23 02:18:14 -0400 |
| commit | 0b12b2bae1130746ed49cc3c7a2daa819ede1b58 (patch) | |
| tree | 56418880921075e914e295739b77287a8c9f9263 | |
| parent | 0d3e1073e4a3f683b746a5248227c57004ba8cec (diff) | |
| download | mutt-alias-auto-add-0b12b2bae1130746ed49cc3c7a2daa819ede1b58.tar.bz2 | |
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.
| -rw-r--r-- | Cargo.lock | 8 | ||||
| -rw-r--r-- | Cargo.toml | 1 | ||||
| -rw-r--r-- | src/main.rs | 34 |
3 files changed, 38 insertions, 5 deletions
@@ -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" @@ -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<P: AsRef<Path>>(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<AliasSearchError> for AliasSearchError { } } -fn find_alias_in_file(alias: &Alias, file: &str) -> Result<Vec<String>, AliasSearchError> { +fn find_alias_in_file<P: AsRef<Path>>(alias: &Alias, file: P) -> Result<Vec<String>, 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<Vec<String>, AliasSea } } +fn print_usage(program: &str) { + println!("Usage: {} FILE", program); +} + fn main() { + let args: Vec<String> = 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(), |
