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 ;
100101
101102/// Name of an option. Either a string or a single char.
102- #[ deriving( Clone , PartialEq ) ]
103+ #[ deriving( Clone , PartialEq , Eq ) ]
103104pub 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 ) ]
114115pub 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 ) ]
125126pub 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 ) ]
136137pub 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 ) ]
150151pub 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 ) ]
167168enum 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 ) ]
175176pub 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 ) ]
188189pub 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) ]
204205pub enum FailType {
205206 ArgumentMissing_ ,
@@ -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