Skip to content

Commit

Permalink
auto merge of #19365 : frewsxcv/rust/getopts-cleanup, r=alexcrichton
Browse files Browse the repository at this point in the history
* Remove public reexports, as a part of #19253
* Rename getopts::Fail_ to getopts::Fail
 * Didn't see a reason for the suffixed '_'
* Removed getopts::FailType
 * Looked like it was only beings used for tests; refactored the tests
   to stop requiring it
* A few other non-breaking trivial refactoring changes

[breaking-change]
  • Loading branch information
bors committed Nov 30, 2014
2 parents 8d8f41b + 1919b12 commit dfaad04
Showing 1 changed file with 42 additions and 70 deletions.
112 changes: 42 additions & 70 deletions src/libgetopts/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,10 @@

#[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;
Expand Down Expand Up @@ -191,7 +190,7 @@ pub struct Matches {
/// 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.
Expand All @@ -204,19 +203,8 @@ pub enum Fail_ {
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 {
Expand Down Expand Up @@ -264,7 +252,7 @@ impl OptGroup {
(1,_) => Opt {
name: Long((long_name)),
hasarg: hasarg,
occur: occur,
occur: occur,
aliases: vec!(
Opt {
name: Short(short_name.as_slice().char_at(0)),
Expand Down Expand Up @@ -366,11 +354,12 @@ impl Matches {
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())
}
}
}

Expand Down Expand Up @@ -534,15 +523,15 @@ pub fn opt(short_name: &str,
}
}

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) => {
Expand Down Expand Up @@ -570,7 +559,7 @@ impl fmt::Show for Fail_ {
/// `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();
Expand Down Expand Up @@ -681,21 +670,15 @@ pub fn getopts(args: &[String], optgrps: &[OptGroup]) -> Result {
}
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,
Expand Down Expand Up @@ -966,20 +949,11 @@ fn test_split_within() {
#[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() {
Expand Down Expand Up @@ -1013,7 +987,7 @@ mod tests {
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!()
}
}
Expand All @@ -1024,12 +998,12 @@ mod tests {
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!()
}
}
Expand All @@ -1040,7 +1014,7 @@ mod tests {
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!()
}
}
Expand Down Expand Up @@ -1092,12 +1066,12 @@ mod tests {
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!()
}
}
Expand All @@ -1108,7 +1082,7 @@ mod tests {
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!()
}
}
Expand Down Expand Up @@ -1156,9 +1130,7 @@ mod tests {
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!()
}
}
Expand All @@ -1169,7 +1141,7 @@ mod tests {
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!()
}
}
Expand Down Expand Up @@ -1317,12 +1289,12 @@ mod tests {
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!()
}
}
Expand Down Expand Up @@ -1352,12 +1324,12 @@ mod tests {
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!()
}
}
Expand Down Expand Up @@ -1493,14 +1465,14 @@ mod tests {
#[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");

Expand Down

0 comments on commit dfaad04

Please sign in to comment.