diff --git a/src/lib.rs b/src/lib.rs
index 57a6e59..1f9f054 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -15,7 +15,12 @@
* along with this program. If not, see .
*/
-pub mod errors;
-pub mod matches;
-pub mod vars;
-mod token;
\ No newline at end of file
+mod errors;
+mod matches;
+mod token;
+mod usage;
+mod vars;
+
+pub use matches::Matches;
+pub use vars::Vars;
+pub use usage::usage;
diff --git a/src/token.rs b/src/token.rs
index a1a9c7d..97f722c 100644
--- a/src/token.rs
+++ b/src/token.rs
@@ -19,14 +19,14 @@ use std::fmt::{self, Display, Formatter};
use errors::{Error, ErrorKind};
-#[derive(Clone)]
+#[derive(Clone, Debug, PartialEq)]
pub struct Token {
short_name: String,
long_name: String,
+ description: String,
pub is_arg: bool,
pub has_arg: bool,
pub is_group: bool,
- description: String,
padding: usize
}
@@ -134,7 +134,7 @@ impl Display for Token {
let repr = if self.is_group {
format!("{}:", self.description)
} else {
- format!(" -{}, --{}{} {}", self.short_name, self.long_name, spacing, self.description)
+ format!(" -{}, --{}{} {}", self.short_name, self.long_name, spacing, self.description)
};
write!(f, "{}", repr)
@@ -145,4 +145,72 @@ enum AnalysisStage {
ShortName,
LongName,
Description
-}
\ No newline at end of file
+}
+
+#[cfg(test)]
+mod tests {
+ use super::Token;
+
+ #[test]
+ fn test_new_token() {
+ let opt = "h/help(Display the program usage)";
+ let token = match Token::new(opt) {
+ Ok(t) => t,
+ Err(why) => panic!("Received error: {}", why)
+ };
+ let control_token = Token {
+ short_name: String::from("h"),
+ long_name: String::from("help"),
+ description: String::from("Display the program usage"),
+ is_arg: false,
+ has_arg: false,
+ is_group: false,
+ padding: 0
+ };
+
+ println!("{}", token);
+ assert_eq!(token, control_token);
+ }
+
+ #[test]
+ fn test_new_group() {
+ let opt = "(This is a group)";
+ let token = match Token::new(opt) {
+ Ok(t) => t,
+ Err(why) => panic!("Received error: {}", why)
+ };
+ let control_token = Token {
+ short_name: String::new(),
+ long_name: String::new(),
+ description: String::from("This is a group"),
+ is_arg: false,
+ has_arg: false,
+ is_group: true,
+ padding: 0
+ };
+
+ println!("{}", token);
+ assert_eq!(token, control_token);
+ }
+
+ #[test]
+ fn control_token_with_arg() {
+ let opt = "o/option(An option with an argument):";
+ let token = match Token::new(opt) {
+ Ok(t) => t,
+ Err(why) => panic!("Received error: {}", why)
+ };
+ let control_token = Token {
+ short_name: String::from("o"),
+ long_name: String::from("option"),
+ description: String::from("An option with an argument"),
+ is_arg: false,
+ has_arg: true,
+ is_group: false,
+ padding: 0
+ };
+
+ println!("{}", token);
+ assert_eq!(token, control_token);
+ }
+}
diff --git a/src/vars.rs b/src/vars.rs
index c5f871e..d8521e9 100644
--- a/src/vars.rs
+++ b/src/vars.rs
@@ -17,7 +17,6 @@
use std::collections::HashMap;
use std::collections::VecDeque;
-use std::slice::Iter;
use errors::Error;
use token::Token;
@@ -87,4 +86,4 @@ impl Vars {
pub fn arg_len(&self) -> usize {
self.args.len()
}
-}
\ No newline at end of file
+}
diff --git a/tests/main.rs b/tests/main.rs
index 23254a0..25fcfb3 100644
--- a/tests/main.rs
+++ b/tests/main.rs
@@ -1,50 +1,26 @@
extern crate pirate;
-use std::env;
-
-use pirate::Matches;
+use pirate::{Matches, usage, Vars};
fn main() {
let opts = vec!["n:", "b/boop", ":input"];
-
- let matches: Matches = match pirate::parse(env::args(), &opts) {
- Err(ref e) => {
- println!("Error: {}", e);
- help();
+ let mut vars = match Vars::new(&opts) {
+ Ok(v) => v,
+ Err(why) => {
+ println!("{}", why);
return;
- },
- Ok(m) => m
+ }
+ };
+ let matches = match Matches::new(&mut vars) {
+ Ok(m) => m,
+ Err(why) => {
+ println!("{}", why);
+ return;
+ }
};
- // Print the program help if necessary
- if matches.has_arg("h") || matches.has_arg("help") {
- help();
+ if matches.has_arg("-h") || matches.has_arg("--help") {
+ usage(&vars);
return;
}
-
- let input = matches.get("input").unwrap().parse::().unwrap();
-
- let num = match matches.get("n") {
- Some(n) => n.parse::().unwrap(),
- None => 1
- };
-
- let sum = input + num;
-
- println!("{} + {} = {}", input, num, sum);
-
- if matches.has_arg("b") || matches.has_arg("boop") {
- println!("Boop!!");
- }
-}
-
-fn help() {
- println!("usage: pirate-test [-n NUM] [-b|--boop] INPUT\n");
-
- println!("Options:");
- println!(" -n NUM\tChange the default number that's added to the input");
- println!(" -b, --boop\tIt's a surprise!");
-
- println!("\nRequired arguments:");
- println!(" INPUT\tWe're gonna manipulate this somehow, you'll see!");
}