diff options
Diffstat (limited to 'src/libgetopts/lib.rs')
| -rw-r--r-- | src/libgetopts/lib.rs | 90 |
1 files changed, 47 insertions, 43 deletions
diff --git a/src/libgetopts/lib.rs b/src/libgetopts/lib.rs index 18077795e24..f50e24c6354 100644 --- a/src/libgetopts/lib.rs +++ b/src/libgetopts/lib.rs @@ -85,8 +85,7 @@ html_favicon_url = "http://www.rust-lang.org/favicon.ico", html_root_url = "http://doc.rust-lang.org/nightly/", html_playground_url = "http://play.rust-lang.org/")] -#![feature(globs, slicing_syntax)] -#![feature(unboxed_closures)] +#![feature(slicing_syntax)] #![deny(missing_docs)] #[cfg(test)] #[macro_use] extern crate log; @@ -105,7 +104,7 @@ use std::iter::repeat; use std::result; /// Name of an option. Either a string or a single char. -#[derive(Clone, PartialEq, Eq)] +#[derive(Clone, PartialEq, Eq, Show)] pub enum Name { /// A string representing the long name of an option. /// For example: "help" @@ -116,7 +115,7 @@ pub enum Name { } /// Describes whether an option has an argument. -#[derive(Clone, Copy, PartialEq, Eq)] +#[derive(Clone, Copy, PartialEq, Eq, Show)] pub enum HasArg { /// The option requires an argument. Yes, @@ -127,7 +126,7 @@ pub enum HasArg { } /// Describes how often an option may occur. -#[derive(Clone, Copy, PartialEq, Eq)] +#[derive(Clone, Copy, PartialEq, Eq, Show)] pub enum Occur { /// The option occurs once. Req, @@ -138,7 +137,7 @@ pub enum Occur { } /// A description of a possible option. -#[derive(Clone, PartialEq, Eq)] +#[derive(Clone, PartialEq, Eq, Show)] pub struct Opt { /// Name of the option pub name: Name, @@ -152,7 +151,7 @@ pub struct Opt { /// One group of options, e.g., both `-h` and `--help`, along with /// their shared description and properties. -#[derive(Clone, PartialEq, Eq)] +#[derive(Clone, PartialEq, Eq, Show)] pub struct OptGroup { /// Short name of the option, e.g. `h` for a `-h` option pub short_name: String, @@ -169,7 +168,7 @@ pub struct OptGroup { } /// Describes whether an option is given at all or has a value. -#[derive(Clone, PartialEq, Eq)] +#[derive(Clone, PartialEq, Eq, Show)] enum Optval { Val(String), Given, @@ -177,7 +176,7 @@ enum Optval { /// The result of checking command line arguments. Contains a vector /// of matches and a vector of free strings. -#[derive(Clone, PartialEq, Eq)] +#[derive(Clone, PartialEq, Eq, Show)] pub struct Matches { /// Options that matched opts: Vec<Opt>, @@ -190,7 +189,7 @@ pub struct Matches { /// The type returned when the command line does not conform to the /// expected format. Use the `Show` implementation to output detailed /// information. -#[derive(Clone, PartialEq, Eq)] +#[derive(Clone, PartialEq, Eq, Show)] pub enum Fail { /// The option requires an argument but none was passed. ArgumentMissing(String), @@ -205,7 +204,7 @@ pub enum Fail { } /// The type of failure that occurred. -#[derive(Copy, PartialEq, Eq)] +#[derive(Copy, PartialEq, Eq, Show)] #[allow(missing_docs)] pub enum FailType { ArgumentMissing_, @@ -281,7 +280,7 @@ impl OptGroup { impl Matches { fn opt_vals(&self, nm: &str) -> Vec<Optval> { - match find_opt(self.opts[], Name::from_str(nm)) { + match find_opt(self.opts.index(&FullRange), Name::from_str(nm)) { Some(id) => self.vals[id].clone(), None => panic!("No option '{}' defined", nm) } @@ -309,7 +308,7 @@ impl Matches { /// Returns true if any of several options were matched. pub fn opts_present(&self, names: &[String]) -> bool { for nm in names.iter() { - match find_opt(self.opts.as_slice(), Name::from_str(nm[])) { + match find_opt(self.opts.as_slice(), Name::from_str(nm.index(&FullRange))) { Some(id) if !self.vals[id].is_empty() => return true, _ => (), }; @@ -320,7 +319,7 @@ impl Matches { /// Returns the string argument supplied to one of several matching options or `None`. pub fn opts_str(&self, names: &[String]) -> Option<String> { for nm in names.iter() { - match self.opt_val(nm[]) { + match self.opt_val(nm.index(&FullRange)) { Some(Val(ref s)) => return Some(s.clone()), _ => () } @@ -536,13 +535,13 @@ pub fn opt(short_name: &str, impl Fail { /// Convert a `Fail` enum into an error string. - #[deprecated="use `Show` (`{}` format specifier)"] + #[deprecated="use `fmt::String` (`{}` format specifier)"] pub fn to_err_msg(self) -> String { self.to_string() } } -impl fmt::Show for Fail { +impl fmt::String for Fail { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match *self { ArgumentMissing(ref nm) => { @@ -585,7 +584,7 @@ pub fn getopts(args: &[String], optgrps: &[OptGroup]) -> Result { while i < l { let cur = args[i].clone(); let curlen = cur.len(); - if !is_arg(cur[]) { + if !is_arg(cur.index(&FullRange)) { free.push(cur); } else if cur == "--" { let mut j = i + 1; @@ -595,7 +594,7 @@ pub fn getopts(args: &[String], optgrps: &[OptGroup]) -> Result { let mut names; let mut i_arg = None; if cur.as_bytes()[1] == b'-' { - let tail = cur[2..curlen]; + let tail = cur.index(&(2..curlen)); let tail_eq: Vec<&str> = tail.split('=').collect(); if tail_eq.len() <= 1 { names = vec!(Long(tail.to_string())); @@ -631,7 +630,7 @@ pub fn getopts(args: &[String], optgrps: &[OptGroup]) -> Result { }; if arg_follows && range.next < curlen { - i_arg = Some(cur[range.next..curlen].to_string()); + i_arg = Some(cur.index(&(range.next..curlen)).to_string()); break; } @@ -650,29 +649,34 @@ pub fn getopts(args: &[String], optgrps: &[OptGroup]) -> Result { if name_pos == names.len() && !i_arg.is_none() { return Err(UnexpectedArgument(nm.to_string())); } - vals[optid].push(Given); + let v = &mut vals[optid]; + v.push(Given); } Maybe => { if !i_arg.is_none() { - vals[optid] - .push(Val((i_arg.clone()) + let v = &mut vals[optid]; + v.push(Val((i_arg.clone()) .unwrap())); } else if name_pos < names.len() || i + 1 == l || - is_arg(args[i + 1][]) { - vals[optid].push(Given); + is_arg(args[i + 1].index(&FullRange)) { + let v = &mut vals[optid]; + v.push(Given); } else { i += 1; - vals[optid].push(Val(args[i].clone())); + let v = &mut vals[optid]; + v.push(Val(args[i].clone())); } } Yes => { if !i_arg.is_none() { - vals[optid].push(Val(i_arg.clone().unwrap())); + let v = &mut vals[optid]; + v.push(Val(i_arg.clone().unwrap())); } else if i + 1 == l { return Err(ArgumentMissing(nm.to_string())); } else { i += 1; - vals[optid].push(Val(args[i].clone())); + let v = &mut vals[optid]; + v.push(Val(args[i].clone())); } } } @@ -717,7 +721,7 @@ pub fn usage(brief: &str, opts: &[OptGroup]) -> String { 0 => {} 1 => { row.push('-'); - row.push_str(short_name[]); + row.push_str(short_name.index(&FullRange)); row.push(' '); } _ => panic!("the short name should only be 1 ascii char long"), @@ -728,7 +732,7 @@ pub fn usage(brief: &str, opts: &[OptGroup]) -> String { 0 => {} _ => { row.push_str("--"); - row.push_str(long_name[]); + row.push_str(long_name.index(&FullRange)); row.push(' '); } } @@ -736,10 +740,10 @@ pub fn usage(brief: &str, opts: &[OptGroup]) -> String { // arg match hasarg { No => {} - Yes => row.push_str(hint[]), + Yes => row.push_str(hint.index(&FullRange)), Maybe => { row.push('['); - row.push_str(hint[]); + row.push_str(hint.index(&FullRange)); row.push(']'); } } @@ -752,7 +756,7 @@ pub fn usage(brief: &str, opts: &[OptGroup]) -> String { row.push(' '); } } else { - row.push_str(desc_sep[]); + row.push_str(desc_sep.index(&FullRange)); } // Normalize desc to contain words separated by one space character @@ -764,14 +768,14 @@ pub fn usage(brief: &str, opts: &[OptGroup]) -> String { // FIXME: #5516 should be graphemes not codepoints let mut desc_rows = Vec::new(); - each_split_within(desc_normalized_whitespace[], 54, |substr| { + each_split_within(desc_normalized_whitespace.index(&FullRange), 54, |substr| { desc_rows.push(substr.to_string()); true }); // FIXME: #5516 should be graphemes not codepoints // wrapped description - row.push_str(desc_rows.connect(desc_sep[])[]); + row.push_str(desc_rows.connect(desc_sep.index(&FullRange)).index(&FullRange)); row }); @@ -790,10 +794,10 @@ fn format_option(opt: &OptGroup) -> String { // Use short_name is possible, but fallback to long_name. if opt.short_name.len() > 0 { line.push('-'); - line.push_str(opt.short_name[]); + line.push_str(opt.short_name.index(&FullRange)); } else { line.push_str("--"); - line.push_str(opt.long_name[]); + line.push_str(opt.long_name.index(&FullRange)); } if opt.hasarg != No { @@ -801,7 +805,7 @@ fn format_option(opt: &OptGroup) -> String { if opt.hasarg == Maybe { line.push('['); } - line.push_str(opt.hint[]); + line.push_str(opt.hint.index(&FullRange)); if opt.hasarg == Maybe { line.push(']'); } @@ -823,7 +827,7 @@ pub fn short_usage(program_name: &str, opts: &[OptGroup]) -> String { line.push_str(opts.iter() .map(format_option) .collect::<Vec<String>>() - .connect(" ")[]); + .connect(" ").index(&FullRange)); line } @@ -886,9 +890,9 @@ fn each_split_within<F>(ss: &str, lim: uint, mut it: F) -> bool where (B, Cr, UnderLim) => { B } (B, Cr, OverLim) if (i - last_start + 1) > lim => panic!("word starting with {} longer than limit!", - ss[last_start..i + 1]), + ss.index(&(last_start..(i + 1)))), (B, Cr, OverLim) => { - *cont = it(ss[slice_start..last_end]); + *cont = it(ss.index(&(slice_start..last_end))); slice_start = last_start; B } @@ -898,7 +902,7 @@ fn each_split_within<F>(ss: &str, lim: uint, mut it: F) -> bool where } (B, Ws, OverLim) => { last_end = i; - *cont = it(ss[slice_start..last_end]); + *cont = it(ss.index(&(slice_start..last_end))); A } @@ -907,14 +911,14 @@ fn each_split_within<F>(ss: &str, lim: uint, mut it: F) -> bool where B } (C, Cr, OverLim) => { - *cont = it(ss[slice_start..last_end]); + *cont = it(ss.index(&(slice_start..last_end))); slice_start = i; last_start = i; last_end = i; B } (C, Ws, OverLim) => { - *cont = it(ss[slice_start..last_end]); + *cont = it(ss.index(&(slice_start..last_end))); A } (C, Ws, UnderLim) => { |
