Tokens are now in Opts
This commit is contained in:
parent
c0d0a99c25
commit
d1de4d9431
3 changed files with 32 additions and 26 deletions
|
@ -16,9 +16,9 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
pub mod errors;
|
pub mod errors;
|
||||||
pub mod lexer;
|
|
||||||
pub mod matches;
|
pub mod matches;
|
||||||
mod opts;
|
mod opts;
|
||||||
|
mod token;
|
||||||
|
|
||||||
use std::env::Args;
|
use std::env::Args;
|
||||||
|
|
||||||
|
|
38
src/opts.rs
38
src/opts.rs
|
@ -18,33 +18,45 @@
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::collections::VecDeque;
|
use std::collections::VecDeque;
|
||||||
|
|
||||||
use lexer;
|
use errors::Error;
|
||||||
|
use token::Token;
|
||||||
|
|
||||||
pub struct Opts {
|
pub struct Opts {
|
||||||
pub opts: HashMap<String, bool>,
|
pub opts: HashMap<String, bool>,
|
||||||
pub args: VecDeque<String>,
|
pub args: VecDeque<String>,
|
||||||
|
tokens: Vec<Token>
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Opts {
|
impl Opts {
|
||||||
pub fn new(options: &[&'static str]) -> Opts {
|
pub fn new(options: &[&str]) -> Result<Opts, Error> {
|
||||||
let opts: Hashmap<String, bool> = HashMap::new();
|
let mut opts: Hashmap<String, bool> = HashMap::new();
|
||||||
let args: VecDeque<String> = VecDeque::new();
|
let mut args: VecDeque<String> = VecDeque::new();
|
||||||
|
let mut tokens: Vec<Token> = Vec::new();
|
||||||
|
|
||||||
for opt in opts.iter() {
|
for opt in options.iter() {
|
||||||
if opt.is_arg {
|
let token = match Token::new(opt) {
|
||||||
args.push_back(String::from(opt.name()));
|
Ok(t) => t,
|
||||||
} else {
|
Err(why) => return Err(why)
|
||||||
opts.insert(String::from(opt.name()), opt.has_arg);
|
};
|
||||||
}
|
|
||||||
|
if !token.is_group {
|
||||||
|
if token.is_arg {
|
||||||
|
args.push_back(String::from(token.name()));
|
||||||
|
} else {
|
||||||
|
opts.insert(String::from(token.name()), token.has_arg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
tokens.push(token);
|
||||||
}
|
}
|
||||||
|
|
||||||
opts.insert(String::from("-h", false));
|
opts.insert(String::from("-h", false));
|
||||||
opts.insert(String::from("--help", false));
|
opts.insert(String::from("--help", false));
|
||||||
|
|
||||||
Opts {
|
Ok(Opts {
|
||||||
opts: opts,
|
opts: opts,
|
||||||
args: args
|
args: args,
|
||||||
}
|
tokens: tokens
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_opt(&self, opt_name: &String) -> Option<&bool> {
|
pub fn get_opt(&self, opt_name: &String) -> Option<&bool> {
|
||||||
|
|
18
src/token.rs
18
src/token.rs
|
@ -31,7 +31,7 @@ pub struct Token {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Token {
|
impl Token {
|
||||||
pub fn new(input: &str, padding: usize) -> Result<Token, Error> {
|
pub fn new(input: &str) -> Result<Token, Error> {
|
||||||
let mut short_name = String::new();
|
let mut short_name = String::new();
|
||||||
let mut long_name = String::new();
|
let mut long_name = String::new();
|
||||||
let mut description = String::new();
|
let mut description = String::new();
|
||||||
|
@ -88,7 +88,7 @@ impl Token {
|
||||||
has_arg: has_arg,
|
has_arg: has_arg,
|
||||||
is_group: is_group,
|
is_group: is_group,
|
||||||
description: description,
|
description: description,
|
||||||
padding: padding
|
padding: 0
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -101,15 +101,9 @@ impl Token {
|
||||||
""
|
""
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn fmt_with_padding(&self, padding: usize) -> String {
|
pub fn adjust_padding(&self, padding: usize) {
|
||||||
let mut name = format!("-{}, --{}", self.short_name, self.long_name);
|
self.padding = padding;
|
||||||
|
|
||||||
for _ in 0..padding {
|
|
||||||
name.push(' ');
|
|
||||||
}
|
|
||||||
|
|
||||||
name
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -123,7 +117,7 @@ impl Display for Token {
|
||||||
let repr = if self.is_group {
|
let repr = if self.is_group {
|
||||||
format!("{}:", self.description)
|
format!("{}:", self.description)
|
||||||
} else {
|
} else {
|
||||||
format!("-{}, --{}{}{}", self.short_name, self.long_name, spacing self.description)
|
format!(" -{}, --{}{}{}", self.short_name, self.long_name, spacing self.description)
|
||||||
};
|
};
|
||||||
|
|
||||||
write!(f, "{}", repr)
|
write!(f, "{}", repr)
|
||||||
|
|
Loading…
Add table
Reference in a new issue