#[cfg(test)] #[phase(plugin, link)] extern crate log;
-pub use self::Name::*;
-pub use self::HasArg::*;
-pub use self::Occur::*;
-pub use self::Fail_::*;
-pub use self::FailType::*;
+use self::Name::*;
+use self::HasArg::*;
+use self::Occur::*;
+use self::Fail::*;
use self::Optval::*;
use std::fmt;
/// expected format. Use the `Show` implementation to output detailed
/// information.
#[deriving(Clone, PartialEq, Eq)]
-pub enum Fail_ {
+pub enum Fail {
/// The option requires an argument but none was passed.
ArgumentMissing(String),
/// The passed option is not declared among the possible options.
UnexpectedArgument(String),
}
-/// The type of failure that occurred.
-#[deriving(PartialEq, Eq)]
-#[allow(missing_docs)]
-pub enum FailType {
- ArgumentMissing_,
- UnrecognizedOption_,
- OptionMissing_,
- OptionDuplicated_,
- UnexpectedArgument_,
-}
-
/// The result of parsing a command line with a set of options.
-pub type Result = result::Result<Matches, Fail_>;
+pub type Result = result::Result<Matches, Fail>;
impl Name {
fn from_str(nm: &str) -> Name {
(1,_) => Opt {
name: Long((long_name)),
hasarg: hasarg,
- occur: occur,
+ occur: occur,
aliases: vec!(
Opt {
name: Short(short_name.as_slice().char_at(0)),
pub fn opt_default(&self, nm: &str, def: &str) -> Option<String> {
let vals = self.opt_vals(nm);
if vals.is_empty() {
- return None;
- }
- match vals[0] {
- Val(ref s) => Some((*s).clone()),
- _ => Some(def.to_string())
+ None
+ } else {
+ match vals[0] {
+ Val(ref s) => Some((*s).clone()),
+ _ => Some(def.to_string())
+ }
}
}
}
}
-impl Fail_ {
- /// Convert a `Fail_` enum into an error string.
+impl Fail {
+ /// Convert a `Fail` enum into an error string.
#[deprecated="use `Show` (`{}` format specifier)"]
pub fn to_err_msg(self) -> String {
self.to_string()
}
}
-impl fmt::Show for Fail_ {
+impl fmt::Show for Fail {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
ArgumentMissing(ref nm) => {
/// `opt_str`, etc. to interrogate results.
/// # Panics
///
-/// Returns `Err(Fail_)` on failure: use the `Show` implementation of `Fail_` to display
+/// Returns `Err(Fail)` on failure: use the `Show` implementation of `Fail` to display
/// information about it.
pub fn getopts(args: &[String], optgrps: &[OptGroup]) -> Result {
let opts: Vec<Opt> = optgrps.iter().map(|x| x.long_to_short()).collect();
}
i += 1;
}
- i = 0u;
- while i < n_opts {
+ for i in range(0u, n_opts) {
let n = vals[i].len();
let occ = opts[i].occur;
- if occ == Req {
- if n == 0 {
- return Err(OptionMissing(opts[i].name.to_string()));
- }
+ if occ == Req && n == 0 {
+ return Err(OptionMissing(opts[i].name.to_string()));
}
- if occ != Multi {
- if n > 1 {
- return Err(OptionDuplicated(opts[i].name.to_string()));
- }
+ if occ != Multi && n > 1 {
+ return Err(OptionDuplicated(opts[i].name.to_string()));
}
- i += 1;
}
Ok(Matches {
opts: opts,
#[cfg(test)]
mod tests {
use super::*;
+ use super::Fail::*;
use std::result::{Err, Ok};
use std::result;
- fn check_fail_type(f: Fail_, ft: FailType) {
- match f {
- ArgumentMissing(_) => assert!(ft == ArgumentMissing_),
- UnrecognizedOption(_) => assert!(ft == UnrecognizedOption_),
- OptionMissing(_) => assert!(ft == OptionMissing_),
- OptionDuplicated(_) => assert!(ft == OptionDuplicated_),
- UnexpectedArgument(_) => assert!(ft == UnexpectedArgument_)
- }
- }
-
// Tests for reqopt
#[test]
fn test_reqopt() {
let opts = vec!(reqopt("t", "test", "testing", "TEST"));
let rs = getopts(args.as_slice(), opts.as_slice());
match rs {
- Err(f) => check_fail_type(f, OptionMissing_),
+ Err(OptionMissing(_)) => {},
_ => panic!()
}
}
let opts = vec!(reqopt("t", "test", "testing", "TEST"));
let rs = getopts(long_args.as_slice(), opts.as_slice());
match rs {
- Err(f) => check_fail_type(f, ArgumentMissing_),
+ Err(ArgumentMissing(_)) => {},
_ => panic!()
}
let short_args = vec!("-t".to_string());
match getopts(short_args.as_slice(), opts.as_slice()) {
- Err(f) => check_fail_type(f, ArgumentMissing_),
+ Err(ArgumentMissing(_)) => {},
_ => panic!()
}
}
let opts = vec!(reqopt("t", "test", "testing", "TEST"));
let rs = getopts(args.as_slice(), opts.as_slice());
match rs {
- Err(f) => check_fail_type(f, OptionDuplicated_),
+ Err(OptionDuplicated(_)) => {},
_ => panic!()
}
}
let opts = vec!(optopt("t", "test", "testing", "TEST"));
let rs = getopts(long_args.as_slice(), opts.as_slice());
match rs {
- Err(f) => check_fail_type(f, ArgumentMissing_),
+ Err(ArgumentMissing(_)) => {},
_ => panic!()
}
let short_args = vec!("-t".to_string());
match getopts(short_args.as_slice(), opts.as_slice()) {
- Err(f) => check_fail_type(f, ArgumentMissing_),
+ Err(ArgumentMissing(_)) => {},
_ => panic!()
}
}
let opts = vec!(optopt("t", "test", "testing", "TEST"));
let rs = getopts(args.as_slice(), opts.as_slice());
match rs {
- Err(f) => check_fail_type(f, OptionDuplicated_),
+ Err(OptionDuplicated(_)) => {},
_ => panic!()
}
}
let opts = vec!(optflag("t", "test", "testing"));
let rs = getopts(args.as_slice(), opts.as_slice());
match rs {
- Err(f) => {
- check_fail_type(f, UnexpectedArgument_);
- }
+ Err(UnexpectedArgument(_)) => {},
_ => panic!()
}
}
let opts = vec!(optflag("t", "test", "testing"));
let rs = getopts(args.as_slice(), opts.as_slice());
match rs {
- Err(f) => check_fail_type(f, OptionDuplicated_),
+ Err(OptionDuplicated(_)) => {},
_ => panic!()
}
}
let opts = vec!(optmulti("t", "test", "testing", "TEST"));
let rs = getopts(long_args.as_slice(), opts.as_slice());
match rs {
- Err(f) => check_fail_type(f, ArgumentMissing_),
+ Err(ArgumentMissing(_)) => {},
_ => panic!()
}
let short_args = vec!("-t".to_string());
match getopts(short_args.as_slice(), opts.as_slice()) {
- Err(f) => check_fail_type(f, ArgumentMissing_),
+ Err(ArgumentMissing(_)) => {},
_ => panic!()
}
}
let opts = vec!(optmulti("t", "test", "testing", "TEST"));
let rs = getopts(long_args.as_slice(), opts.as_slice());
match rs {
- Err(f) => check_fail_type(f, UnrecognizedOption_),
+ Err(UnrecognizedOption(_)) => {},
_ => panic!()
}
let short_args = vec!("-u".to_string());
match getopts(short_args.as_slice(), opts.as_slice()) {
- Err(f) => check_fail_type(f, UnrecognizedOption_),
+ Err(UnrecognizedOption(_)) => {},
_ => panic!()
}
}
#[test]
fn test_long_to_short() {
let mut short = Opt {
- name: Long("banana".to_string()),
- hasarg: Yes,
- occur: Req,
+ name: Name::Long("banana".to_string()),
+ hasarg: HasArg::Yes,
+ occur: Occur::Req,
aliases: Vec::new(),
};
- short.aliases = vec!(Opt { name: Short('b'),
- hasarg: Yes,
- occur: Req,
+ short.aliases = vec!(Opt { name: Name::Short('b'),
+ hasarg: HasArg::Yes,
+ occur: Occur::Req,
aliases: Vec::new() });
let verbose = reqopt("b", "banana", "some bananas", "VAL");