Skip to content

Commit d64f18c

Browse files
committed
auto merge of #14884 : huonw/rust/getoptsfail, r=alexcrichton
2 parents dbd29ea + 09eb95f commit d64f18c

File tree

7 files changed

+39
-32
lines changed

7 files changed

+39
-32
lines changed

src/compiletest/compiletest.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ pub fn parse_config(args: Vec<String> ) -> Config {
110110
let matches =
111111
&match getopts::getopts(args_.as_slice(), groups.as_slice()) {
112112
Ok(m) => m,
113-
Err(f) => fail!("{}", f.to_err_msg())
113+
Err(f) => fail!("{}", f)
114114
};
115115

116116
if matches.opt_present("h") || matches.opt_present("help") {

src/libgetopts/lib.rs

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
//! ];
6262
//! let matches = match getopts(args.tail(), opts) {
6363
//! Ok(m) => { m }
64-
//! Err(f) => { fail!(f.to_err_msg()) }
64+
//! Err(f) => { fail!(f.to_str()) }
6565
//! };
6666
//! if matches.opt_present("h") {
6767
//! print_usage(program.as_slice(), opts);
@@ -94,12 +94,13 @@
9494
#[cfg(test, not(stage0))] #[phase(plugin, link)] extern crate log;
9595

9696
use std::cmp::PartialEq;
97+
use std::fmt;
9798
use std::result::{Err, Ok};
9899
use std::result;
99100
use std::string::String;
100101

101102
/// Name of an option. Either a string or a single char.
102-
#[deriving(Clone, PartialEq)]
103+
#[deriving(Clone, PartialEq, Eq)]
103104
pub enum Name {
104105
/// A string representing the long name of an option.
105106
/// For example: "help"
@@ -110,7 +111,7 @@ pub enum Name {
110111
}
111112

112113
/// Describes whether an option has an argument.
113-
#[deriving(Clone, PartialEq)]
114+
#[deriving(Clone, PartialEq, Eq)]
114115
pub enum HasArg {
115116
/// The option requires an argument.
116117
Yes,
@@ -121,7 +122,7 @@ pub enum HasArg {
121122
}
122123

123124
/// Describes how often an option may occur.
124-
#[deriving(Clone, PartialEq)]
125+
#[deriving(Clone, PartialEq, Eq)]
125126
pub enum Occur {
126127
/// The option occurs once.
127128
Req,
@@ -132,7 +133,7 @@ pub enum Occur {
132133
}
133134

134135
/// A description of a possible option.
135-
#[deriving(Clone, PartialEq)]
136+
#[deriving(Clone, PartialEq, Eq)]
136137
pub struct Opt {
137138
/// Name of the option
138139
pub name: Name,
@@ -141,12 +142,12 @@ pub struct Opt {
141142
/// How often it can occur
142143
pub occur: Occur,
143144
/// Which options it aliases
144-
pub aliases: Vec<Opt> ,
145+
pub aliases: Vec<Opt>,
145146
}
146147

147148
/// One group of options, e.g., both -h and --help, along with
148149
/// their shared description and properties.
149-
#[deriving(Clone, PartialEq)]
150+
#[deriving(Clone, PartialEq, Eq)]
150151
pub struct OptGroup {
151152
/// Short Name of the `OptGroup`
152153
pub short_name: String,
@@ -163,28 +164,28 @@ pub struct OptGroup {
163164
}
164165

165166
/// Describes whether an option is given at all or has a value.
166-
#[deriving(Clone, PartialEq)]
167+
#[deriving(Clone, PartialEq, Eq)]
167168
enum Optval {
168169
Val(String),
169170
Given,
170171
}
171172

172173
/// The result of checking command line arguments. Contains a vector
173174
/// of matches and a vector of free strings.
174-
#[deriving(Clone, PartialEq)]
175+
#[deriving(Clone, PartialEq, Eq)]
175176
pub struct Matches {
176177
/// Options that matched
177-
opts: Vec<Opt> ,
178+
opts: Vec<Opt>,
178179
/// Values of the Options that matched
179-
vals: Vec<Vec<Optval> > ,
180+
vals: Vec<Vec<Optval>>,
180181
/// Free string fragments
181182
pub free: Vec<String>,
182183
}
183184

184185
/// The type returned when the command line does not conform to the
185-
/// expected format. Call the `to_err_msg` method to retrieve the
186-
/// error as a string.
187-
#[deriving(Clone, PartialEq, Show)]
186+
/// expected format. Use the `Show` implementation to output detailed
187+
/// information.
188+
#[deriving(Clone, PartialEq, Eq)]
188189
pub enum Fail_ {
189190
/// The option requires an argument but none was passed.
190191
ArgumentMissing(String),
@@ -199,7 +200,7 @@ pub enum Fail_ {
199200
}
200201

201202
/// The type of failure that occurred.
202-
#[deriving(PartialEq)]
203+
#[deriving(PartialEq, Eq)]
203204
#[allow(missing_doc)]
204205
pub enum FailType {
205206
ArgumentMissing_,
@@ -498,22 +499,29 @@ pub fn opt(short_name: &str,
498499

499500
impl Fail_ {
500501
/// Convert a `Fail_` enum into an error string.
502+
#[deprecated="use `Show` (`{}` format specifier)"]
501503
pub fn to_err_msg(self) -> String {
502-
match self {
504+
self.to_str()
505+
}
506+
}
507+
508+
impl fmt::Show for Fail_ {
509+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
510+
match *self {
503511
ArgumentMissing(ref nm) => {
504-
format!("Argument to option '{}' missing.", *nm)
512+
write!(f, "Argument to option '{}' missing.", *nm)
505513
}
506514
UnrecognizedOption(ref nm) => {
507-
format!("Unrecognized option: '{}'.", *nm)
515+
write!(f, "Unrecognized option: '{}'.", *nm)
508516
}
509517
OptionMissing(ref nm) => {
510-
format!("Required option '{}' missing.", *nm)
518+
write!(f, "Required option '{}' missing.", *nm)
511519
}
512520
OptionDuplicated(ref nm) => {
513-
format!("Option '{}' given more than once.", *nm)
521+
write!(f, "Option '{}' given more than once.", *nm)
514522
}
515523
UnexpectedArgument(ref nm) => {
516-
format!("Option '{}' does not take an argument.", *nm)
524+
write!(f, "Option '{}' does not take an argument.", *nm)
517525
}
518526
}
519527
}
@@ -522,8 +530,9 @@ impl Fail_ {
522530
/// Parse command line arguments according to the provided options.
523531
///
524532
/// On success returns `Ok(Opt)`. Use methods such as `opt_present`
525-
/// `opt_str`, etc. to interrogate results. Returns `Err(Fail_)` on failure.
526-
/// Use `to_err_msg` to get an error message.
533+
/// `opt_str`, etc. to interrogate results. Returns `Err(Fail_)` on
534+
/// failure: use the `Show` implementation of `Fail_` to display
535+
/// information about it.
527536
pub fn getopts(args: &[String], optgrps: &[OptGroup]) -> Result {
528537
let opts: Vec<Opt> = optgrps.iter().map(|x| x.long_to_short()).collect();
529538
let n_opts = opts.len();
@@ -1110,7 +1119,6 @@ mod tests {
11101119
let rs = getopts(args.as_slice(), opts.as_slice());
11111120
match rs {
11121121
Err(f) => {
1113-
error!("{:?}", f.clone().to_err_msg());
11141122
check_fail_type(f, UnexpectedArgument_);
11151123
}
11161124
_ => fail!()

src/librustc/driver/config.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -792,7 +792,7 @@ mod test {
792792
let matches =
793793
&match getopts(["--test".to_string()], optgroups().as_slice()) {
794794
Ok(m) => m,
795-
Err(f) => fail!("test_switch_implies_cfg_test: {}", f.to_err_msg())
795+
Err(f) => fail!("test_switch_implies_cfg_test: {}", f)
796796
};
797797
let sessopts = build_session_options(matches);
798798
let sess = build_session(sessopts, None);
@@ -809,8 +809,7 @@ mod test {
809809
optgroups().as_slice()) {
810810
Ok(m) => m,
811811
Err(f) => {
812-
fail!("test_switch_implies_cfg_test_unless_cfg_test: {}",
813-
f.to_err_msg());
812+
fail!("test_switch_implies_cfg_test_unless_cfg_test: {}", f)
814813
}
815814
};
816815
let sessopts = build_session_options(matches);

src/librustc/driver/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ pub fn handle_options(mut args: Vec<String>) -> Option<getopts::Matches> {
205205
match getopts::getopts(args.as_slice(), config::optgroups().as_slice()) {
206206
Ok(m) => m,
207207
Err(f) => {
208-
early_error(f.to_err_msg().as_slice());
208+
early_error(f.to_str().as_slice());
209209
}
210210
};
211211

src/librustdoc/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ pub fn main_args(args: &[String]) -> int {
148148
let matches = match getopts::getopts(args.tail(), opts().as_slice()) {
149149
Ok(m) => m,
150150
Err(err) => {
151-
println!("{}", err.to_err_msg());
151+
println!("{}", err);
152152
return 1;
153153
}
154154
};

src/libtest/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ pub fn parse_opts(args: &[String]) -> Option<OptRes> {
367367
let matches =
368368
match getopts::getopts(args_.as_slice(), optgroups().as_slice()) {
369369
Ok(m) => m,
370-
Err(f) => return Some(Err(f.to_err_msg().to_string()))
370+
Err(f) => return Some(Err(f.to_str()))
371371
};
372372

373373
if matches.opt_present("h") { usage(args[0].as_slice()); return None; }

src/test/run-pass/getopts_ref.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub fn main() {
2020
match getopts(args.as_slice(), opts.as_slice()) {
2121
Ok(ref m) =>
2222
assert!(!m.opt_present("b")),
23-
Err(ref f) => fail!("{:?}", (*f).clone().to_err_msg())
23+
Err(ref f) => fail!("{}", *f)
2424
};
2525

2626
}

0 commit comments

Comments
 (0)