Fixed bug where the correct usage format wasn't being used.
TODO: Fix a spacing issue with the usage display.
This commit is contained in:
parent
3c9dae504b
commit
77f65c79c2
6 changed files with 39 additions and 17 deletions
|
@ -21,6 +21,6 @@ mod token;
|
|||
mod usage;
|
||||
mod vars;
|
||||
|
||||
pub use matches::{Matches, matches};
|
||||
pub use matches::{Matches, MatchesTrait, matches};
|
||||
pub use vars::{Vars, vars};
|
||||
pub use usage::usage;
|
||||
|
|
|
@ -76,7 +76,7 @@ pub fn matches(vars: &mut Vars, env_args: &[String]) -> Result<Matches, Error> {
|
|||
}
|
||||
}
|
||||
|
||||
trait MatchesTrait {
|
||||
pub trait MatchesTrait {
|
||||
fn get(&self, arg: &str) -> Option<&String>;
|
||||
|
||||
fn has_match(&self, arg: &str) -> bool;
|
||||
|
|
13
src/token.rs
13
src/token.rs
|
@ -119,7 +119,7 @@ impl Token {
|
|||
} else if !self.short_name.is_empty() {
|
||||
self.short_name.clone()
|
||||
} else {
|
||||
String::new()
|
||||
self.description.clone()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -166,10 +166,17 @@ impl Display for Token {
|
|||
spacing.push(' ');
|
||||
}
|
||||
|
||||
let short_name_empty = self.short_name.is_empty();
|
||||
let long_name_empty = self.long_name.is_empty();
|
||||
|
||||
let repr = if self.is_group {
|
||||
format!("{}:", self.description)
|
||||
} else {
|
||||
format!("\n{}:", self.description)
|
||||
} else if !short_name_empty && !long_name_empty {
|
||||
format!(" -{}, --{}{} {}", self.short_name, self.long_name, spacing, self.description)
|
||||
} else if short_name_empty && !long_name_empty {
|
||||
format!(" --{}{} {}", self.long_name, spacing, self.description)
|
||||
} else {
|
||||
format!(" -{}{} {}", self.short_name, spacing, self.description)
|
||||
};
|
||||
|
||||
write!(f, "{}", repr)
|
||||
|
|
11
src/usage.rs
11
src/usage.rs
|
@ -18,7 +18,16 @@
|
|||
use vars::Vars;
|
||||
|
||||
pub fn usage(vars: &Vars) {
|
||||
print!("Usage: {} ", vars.program_name);
|
||||
|
||||
for token in vars.tokens() {
|
||||
if let Some(usage) = token.usage() {
|
||||
print!("{} ", usage);
|
||||
}
|
||||
}
|
||||
|
||||
println!("\nOptions:");
|
||||
for token in vars.tokens() {
|
||||
println!("{}", token);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -55,7 +55,6 @@ pub fn vars(program_name: &str, options: &[&str]) -> Result<Vars, Error> {
|
|||
}
|
||||
|
||||
let token_len = token.len();
|
||||
println!("Token {} length: {}", token.name(), token_len);
|
||||
if token_len > 0 {
|
||||
if token_len > longest_token_len {
|
||||
longest_token_len = token_len;
|
||||
|
@ -81,8 +80,8 @@ pub fn vars(program_name: &str, options: &[&str]) -> Result<Vars, Error> {
|
|||
};
|
||||
|
||||
tokens.push(help_token);
|
||||
opts.insert(String::from("-h"), index);
|
||||
opts.insert(String::from("--help"), index);
|
||||
opts.insert(String::from("h"), index);
|
||||
opts.insert(String::from("help"), index);
|
||||
|
||||
Ok(Vars {
|
||||
opts: opts,
|
||||
|
|
|
@ -2,25 +2,32 @@ extern crate pirate;
|
|||
|
||||
use std::env;
|
||||
|
||||
use pirate::{matches, vars};
|
||||
use pirate::{Matches, MatchesTrait, matches, usage, vars};
|
||||
|
||||
fn main() {
|
||||
let env_args: Vec<String> = env::args().collect();
|
||||
let opts = vec!["o/opt(An option)", "a(Argument):"];
|
||||
let mut vars = vars("A Test Program", &opts).unwrap();
|
||||
let opts = vec!["o/opt(An option)", "a(An Argument):"];
|
||||
let mut vars = vars("test", &opts).unwrap();
|
||||
|
||||
let matches = match matches(&mut vars, &env_args) {
|
||||
let matches: Matches = match matches(&mut vars, &env_args) {
|
||||
Ok(m) => m,
|
||||
Err(why) => panic!("An error occurred: {}", why)
|
||||
};
|
||||
|
||||
if matches.has_match("a") {
|
||||
let m = matches.get("a").unwrap();
|
||||
println!("{}", m);
|
||||
if matches.has_match("help") {
|
||||
usage(&vars);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
let arg = match matches.get("a") {
|
||||
Some(a) => a.clone(),
|
||||
None => String::from("Pickle")
|
||||
};
|
||||
|
||||
match matches.get("opt") {
|
||||
Some(_) => println!("Opt was passed to the program"),
|
||||
None => println!("Opt was not passed to the program")
|
||||
}
|
||||
|
||||
println!("{}", arg);
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue