Skip to content

Commit 1919b12

Browse files
committed
getopts: cleanup, renames, remove reexports
* 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]
1 parent 82fc1aa commit 1919b12

File tree

1 file changed

+42
-70
lines changed

1 file changed

+42
-70
lines changed

src/libgetopts/lib.rs

+42-70
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,10 @@
9292

9393
#[cfg(test)] #[phase(plugin, link)] extern crate log;
9494

95-
pub use self::Name::*;
96-
pub use self::HasArg::*;
97-
pub use self::Occur::*;
98-
pub use self::Fail_::*;
99-
pub use self::FailType::*;
95+
use self::Name::*;
96+
use self::HasArg::*;
97+
use self::Occur::*;
98+
use self::Fail::*;
10099
use self::Optval::*;
101100

102101
use std::fmt;
@@ -191,7 +190,7 @@ pub struct Matches {
191190
/// expected format. Use the `Show` implementation to output detailed
192191
/// information.
193192
#[deriving(Clone, PartialEq, Eq)]
194-
pub enum Fail_ {
193+
pub enum Fail {
195194
/// The option requires an argument but none was passed.
196195
ArgumentMissing(String),
197196
/// The passed option is not declared among the possible options.
@@ -204,19 +203,8 @@ pub enum Fail_ {
204203
UnexpectedArgument(String),
205204
}
206205

207-
/// The type of failure that occurred.
208-
#[deriving(PartialEq, Eq)]
209-
#[allow(missing_docs)]
210-
pub enum FailType {
211-
ArgumentMissing_,
212-
UnrecognizedOption_,
213-
OptionMissing_,
214-
OptionDuplicated_,
215-
UnexpectedArgument_,
216-
}
217-
218206
/// The result of parsing a command line with a set of options.
219-
pub type Result = result::Result<Matches, Fail_>;
207+
pub type Result = result::Result<Matches, Fail>;
220208

221209
impl Name {
222210
fn from_str(nm: &str) -> Name {
@@ -264,7 +252,7 @@ impl OptGroup {
264252
(1,_) => Opt {
265253
name: Long((long_name)),
266254
hasarg: hasarg,
267-
occur: occur,
255+
occur: occur,
268256
aliases: vec!(
269257
Opt {
270258
name: Short(short_name.as_slice().char_at(0)),
@@ -366,11 +354,12 @@ impl Matches {
366354
pub fn opt_default(&self, nm: &str, def: &str) -> Option<String> {
367355
let vals = self.opt_vals(nm);
368356
if vals.is_empty() {
369-
return None;
370-
}
371-
match vals[0] {
372-
Val(ref s) => Some((*s).clone()),
373-
_ => Some(def.to_string())
357+
None
358+
} else {
359+
match vals[0] {
360+
Val(ref s) => Some((*s).clone()),
361+
_ => Some(def.to_string())
362+
}
374363
}
375364
}
376365

@@ -534,15 +523,15 @@ pub fn opt(short_name: &str,
534523
}
535524
}
536525

537-
impl Fail_ {
538-
/// Convert a `Fail_` enum into an error string.
526+
impl Fail {
527+
/// Convert a `Fail` enum into an error string.
539528
#[deprecated="use `Show` (`{}` format specifier)"]
540529
pub fn to_err_msg(self) -> String {
541530
self.to_string()
542531
}
543532
}
544533

545-
impl fmt::Show for Fail_ {
534+
impl fmt::Show for Fail {
546535
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
547536
match *self {
548537
ArgumentMissing(ref nm) => {
@@ -570,7 +559,7 @@ impl fmt::Show for Fail_ {
570559
/// `opt_str`, etc. to interrogate results.
571560
/// # Panics
572561
///
573-
/// Returns `Err(Fail_)` on failure: use the `Show` implementation of `Fail_` to display
562+
/// Returns `Err(Fail)` on failure: use the `Show` implementation of `Fail` to display
574563
/// information about it.
575564
pub fn getopts(args: &[String], optgrps: &[OptGroup]) -> Result {
576565
let opts: Vec<Opt> = optgrps.iter().map(|x| x.long_to_short()).collect();
@@ -681,21 +670,15 @@ pub fn getopts(args: &[String], optgrps: &[OptGroup]) -> Result {
681670
}
682671
i += 1;
683672
}
684-
i = 0u;
685-
while i < n_opts {
673+
for i in range(0u, n_opts) {
686674
let n = vals[i].len();
687675
let occ = opts[i].occur;
688-
if occ == Req {
689-
if n == 0 {
690-
return Err(OptionMissing(opts[i].name.to_string()));
691-
}
676+
if occ == Req && n == 0 {
677+
return Err(OptionMissing(opts[i].name.to_string()));
692678
}
693-
if occ != Multi {
694-
if n > 1 {
695-
return Err(OptionDuplicated(opts[i].name.to_string()));
696-
}
679+
if occ != Multi && n > 1 {
680+
return Err(OptionDuplicated(opts[i].name.to_string()));
697681
}
698-
i += 1;
699682
}
700683
Ok(Matches {
701684
opts: opts,
@@ -966,20 +949,11 @@ fn test_split_within() {
966949
#[cfg(test)]
967950
mod tests {
968951
use super::*;
952+
use super::Fail::*;
969953

970954
use std::result::{Err, Ok};
971955
use std::result;
972956

973-
fn check_fail_type(f: Fail_, ft: FailType) {
974-
match f {
975-
ArgumentMissing(_) => assert!(ft == ArgumentMissing_),
976-
UnrecognizedOption(_) => assert!(ft == UnrecognizedOption_),
977-
OptionMissing(_) => assert!(ft == OptionMissing_),
978-
OptionDuplicated(_) => assert!(ft == OptionDuplicated_),
979-
UnexpectedArgument(_) => assert!(ft == UnexpectedArgument_)
980-
}
981-
}
982-
983957
// Tests for reqopt
984958
#[test]
985959
fn test_reqopt() {
@@ -1013,7 +987,7 @@ mod tests {
1013987
let opts = vec!(reqopt("t", "test", "testing", "TEST"));
1014988
let rs = getopts(args.as_slice(), opts.as_slice());
1015989
match rs {
1016-
Err(f) => check_fail_type(f, OptionMissing_),
990+
Err(OptionMissing(_)) => {},
1017991
_ => panic!()
1018992
}
1019993
}
@@ -1024,12 +998,12 @@ mod tests {
1024998
let opts = vec!(reqopt("t", "test", "testing", "TEST"));
1025999
let rs = getopts(long_args.as_slice(), opts.as_slice());
10261000
match rs {
1027-
Err(f) => check_fail_type(f, ArgumentMissing_),
1001+
Err(ArgumentMissing(_)) => {},
10281002
_ => panic!()
10291003
}
10301004
let short_args = vec!("-t".to_string());
10311005
match getopts(short_args.as_slice(), opts.as_slice()) {
1032-
Err(f) => check_fail_type(f, ArgumentMissing_),
1006+
Err(ArgumentMissing(_)) => {},
10331007
_ => panic!()
10341008
}
10351009
}
@@ -1040,7 +1014,7 @@ mod tests {
10401014
let opts = vec!(reqopt("t", "test", "testing", "TEST"));
10411015
let rs = getopts(args.as_slice(), opts.as_slice());
10421016
match rs {
1043-
Err(f) => check_fail_type(f, OptionDuplicated_),
1017+
Err(OptionDuplicated(_)) => {},
10441018
_ => panic!()
10451019
}
10461020
}
@@ -1092,12 +1066,12 @@ mod tests {
10921066
let opts = vec!(optopt("t", "test", "testing", "TEST"));
10931067
let rs = getopts(long_args.as_slice(), opts.as_slice());
10941068
match rs {
1095-
Err(f) => check_fail_type(f, ArgumentMissing_),
1069+
Err(ArgumentMissing(_)) => {},
10961070
_ => panic!()
10971071
}
10981072
let short_args = vec!("-t".to_string());
10991073
match getopts(short_args.as_slice(), opts.as_slice()) {
1100-
Err(f) => check_fail_type(f, ArgumentMissing_),
1074+
Err(ArgumentMissing(_)) => {},
11011075
_ => panic!()
11021076
}
11031077
}
@@ -1108,7 +1082,7 @@ mod tests {
11081082
let opts = vec!(optopt("t", "test", "testing", "TEST"));
11091083
let rs = getopts(args.as_slice(), opts.as_slice());
11101084
match rs {
1111-
Err(f) => check_fail_type(f, OptionDuplicated_),
1085+
Err(OptionDuplicated(_)) => {},
11121086
_ => panic!()
11131087
}
11141088
}
@@ -1156,9 +1130,7 @@ mod tests {
11561130
let opts = vec!(optflag("t", "test", "testing"));
11571131
let rs = getopts(args.as_slice(), opts.as_slice());
11581132
match rs {
1159-
Err(f) => {
1160-
check_fail_type(f, UnexpectedArgument_);
1161-
}
1133+
Err(UnexpectedArgument(_)) => {},
11621134
_ => panic!()
11631135
}
11641136
}
@@ -1169,7 +1141,7 @@ mod tests {
11691141
let opts = vec!(optflag("t", "test", "testing"));
11701142
let rs = getopts(args.as_slice(), opts.as_slice());
11711143
match rs {
1172-
Err(f) => check_fail_type(f, OptionDuplicated_),
1144+
Err(OptionDuplicated(_)) => {},
11731145
_ => panic!()
11741146
}
11751147
}
@@ -1317,12 +1289,12 @@ mod tests {
13171289
let opts = vec!(optmulti("t", "test", "testing", "TEST"));
13181290
let rs = getopts(long_args.as_slice(), opts.as_slice());
13191291
match rs {
1320-
Err(f) => check_fail_type(f, ArgumentMissing_),
1292+
Err(ArgumentMissing(_)) => {},
13211293
_ => panic!()
13221294
}
13231295
let short_args = vec!("-t".to_string());
13241296
match getopts(short_args.as_slice(), opts.as_slice()) {
1325-
Err(f) => check_fail_type(f, ArgumentMissing_),
1297+
Err(ArgumentMissing(_)) => {},
13261298
_ => panic!()
13271299
}
13281300
}
@@ -1352,12 +1324,12 @@ mod tests {
13521324
let opts = vec!(optmulti("t", "test", "testing", "TEST"));
13531325
let rs = getopts(long_args.as_slice(), opts.as_slice());
13541326
match rs {
1355-
Err(f) => check_fail_type(f, UnrecognizedOption_),
1327+
Err(UnrecognizedOption(_)) => {},
13561328
_ => panic!()
13571329
}
13581330
let short_args = vec!("-u".to_string());
13591331
match getopts(short_args.as_slice(), opts.as_slice()) {
1360-
Err(f) => check_fail_type(f, UnrecognizedOption_),
1332+
Err(UnrecognizedOption(_)) => {},
13611333
_ => panic!()
13621334
}
13631335
}
@@ -1493,14 +1465,14 @@ mod tests {
14931465
#[test]
14941466
fn test_long_to_short() {
14951467
let mut short = Opt {
1496-
name: Long("banana".to_string()),
1497-
hasarg: Yes,
1498-
occur: Req,
1468+
name: Name::Long("banana".to_string()),
1469+
hasarg: HasArg::Yes,
1470+
occur: Occur::Req,
14991471
aliases: Vec::new(),
15001472
};
1501-
short.aliases = vec!(Opt { name: Short('b'),
1502-
hasarg: Yes,
1503-
occur: Req,
1473+
short.aliases = vec!(Opt { name: Name::Short('b'),
1474+
hasarg: HasArg::Yes,
1475+
occur: Occur::Req,
15041476
aliases: Vec::new() });
15051477
let verbose = reqopt("b", "banana", "some bananas", "VAL");
15061478

0 commit comments

Comments
 (0)