2014-06-14 14:43:39 +00:00
|
|
|
extern crate getopts;
|
|
|
|
extern crate regex;
|
|
|
|
|
|
|
|
// parse_style parses a style string into a NumberingStyle.
|
|
|
|
fn parse_style(chars: &[char]) -> Result<::NumberingStyle, String> {
|
|
|
|
match chars {
|
2014-11-19 20:50:37 +00:00
|
|
|
['a'] => { Ok(::NumberingStyle::NumberForAll) },
|
|
|
|
['t'] => { Ok(::NumberingStyle::NumberForNonEmpty) },
|
|
|
|
['n'] => { Ok(::NumberingStyle::NumberForNone) },
|
2014-09-10 06:26:42 +00:00
|
|
|
['p', rest..] => {
|
2014-07-20 01:13:55 +00:00
|
|
|
match regex::Regex::new(String::from_chars(rest).as_slice()) {
|
2014-11-19 20:50:37 +00:00
|
|
|
Ok(re) => Ok(::NumberingStyle::NumberForRegularExpression(re)),
|
2014-06-14 14:43:39 +00:00
|
|
|
Err(_) => Err(String::from_str("Illegal regular expression")),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
Err(String::from_str("Illegal style encountered"))
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse_options loads the options into the settings, returning an array of
|
|
|
|
// error messages.
|
|
|
|
pub fn parse_options(settings: &mut ::Settings, opts: &getopts::Matches) -> Vec<String> {
|
|
|
|
// This vector holds error messages encountered.
|
|
|
|
let mut errs: Vec<String> = vec![];
|
|
|
|
settings.renumber = !opts.opt_present("p");
|
|
|
|
match opts.opt_str("s") {
|
|
|
|
None => {},
|
|
|
|
Some(val) => { settings.number_separator = val; }
|
|
|
|
}
|
|
|
|
match opts.opt_str("n") {
|
|
|
|
None => {},
|
|
|
|
Some(val) => match val.as_slice() {
|
2014-11-19 20:50:37 +00:00
|
|
|
"ln" => { settings.number_format = ::NumberFormat::Left; },
|
|
|
|
"rn" => { settings.number_format = ::NumberFormat::Right; },
|
|
|
|
"rz" => { settings.number_format = ::NumberFormat::RightZero; },
|
2014-06-14 14:43:39 +00:00
|
|
|
_ => { errs.push(String::from_str("Illegal value for -n")); },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
match opts.opt_str("b") {
|
|
|
|
None => {},
|
|
|
|
Some(val) => {
|
|
|
|
let chars: Vec<char> = val.as_slice().chars().collect();
|
|
|
|
match parse_style(chars.as_slice()) {
|
|
|
|
Ok(s) => { settings.body_numbering = s; }
|
|
|
|
Err(message) => { errs.push(message); }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
match opts.opt_str("f") {
|
|
|
|
None => {},
|
|
|
|
Some(val) => {
|
|
|
|
let chars: Vec<char> = val.as_slice().chars().collect();
|
|
|
|
match parse_style(chars.as_slice()) {
|
|
|
|
Ok(s) => { settings.footer_numbering = s; }
|
|
|
|
Err(message) => { errs.push(message); }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
match opts.opt_str("h") {
|
|
|
|
None => {},
|
|
|
|
Some(val) => {
|
|
|
|
let chars: Vec<char> = val.as_slice().chars().collect();
|
|
|
|
match parse_style(chars.as_slice()) {
|
|
|
|
Ok(s) => { settings.header_numbering = s; }
|
|
|
|
Err(message) => { errs.push(message); }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
match opts.opt_str("i") {
|
|
|
|
None => {}
|
|
|
|
Some(val) => {
|
2014-12-25 18:55:32 +00:00
|
|
|
let conv: Option<u64> = val.as_slice().parse();
|
2014-06-14 14:43:39 +00:00
|
|
|
match conv {
|
|
|
|
None => {
|
|
|
|
errs.push(String::from_str("Illegal value for -i"));
|
|
|
|
}
|
|
|
|
Some(num) => { settings.line_increment = num }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
match opts.opt_str("w") {
|
|
|
|
None => {}
|
|
|
|
Some(val) => {
|
2014-12-25 18:55:32 +00:00
|
|
|
let conv: Option<uint> = val.as_slice().parse();
|
2014-06-14 14:43:39 +00:00
|
|
|
match conv {
|
|
|
|
None => {
|
|
|
|
errs.push(String::from_str("Illegal value for -w"));
|
|
|
|
}
|
|
|
|
Some(num) => { settings.number_width = num }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
match opts.opt_str("v") {
|
|
|
|
None => {}
|
|
|
|
Some(val) => {
|
2014-12-25 18:55:32 +00:00
|
|
|
let conv: Option<u64> = val.as_slice().parse();
|
2014-06-14 14:43:39 +00:00
|
|
|
match conv {
|
|
|
|
None => {
|
|
|
|
errs.push(String::from_str("Illegal value for -v"));
|
|
|
|
}
|
|
|
|
Some(num) => { settings.starting_line_number = num }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
match opts.opt_str("l") {
|
|
|
|
None => {}
|
|
|
|
Some(val) => {
|
2014-12-25 18:55:32 +00:00
|
|
|
let conv: Option<u64> = val.as_slice().parse();
|
2014-06-14 14:43:39 +00:00
|
|
|
match conv {
|
|
|
|
None => {
|
|
|
|
errs.push(String::from_str("Illegal value for -l"));
|
|
|
|
}
|
|
|
|
Some(num) => { settings.join_blank_lines = num }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
errs
|
|
|
|
}
|