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);
9494#[ cfg( test, not( stage0) ) ] #[ phase( plugin, link) ] extern crate log;
9595
9696use std:: cmp:: PartialEq ;
97+ use std:: fmt;
9798use std:: result:: { Err , Ok } ;
9899use std:: result;
99100use 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 ) ]
188189pub 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
499500impl 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.
527536pub 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 ! ( )
0 commit comments