Skip to content

Commit 0642cbb

Browse files
committed
getopts: format failure messages with Show.
This obsoletes the old `to_err_msg` method. Replace println!("Error: {}", failure.to_err_msg()) let string = failure.to_err_msg(); with println!("Error: {}", failure) let string = failure.to_str(); [breaking-change]
1 parent 3851d68 commit 0642cbb

File tree

7 files changed

+28
-21
lines changed

7 files changed

+28
-21
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: 21 additions & 13 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,6 +94,7 @@
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;
@@ -182,9 +183,9 @@ pub struct Matches {
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)]
188189
pub enum Fail_ {
189190
/// The option requires an argument but none was passed.
190191
ArgumentMissing(String),
@@ -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)