61
61
//! ];
62
62
//! let matches = match getopts(args.tail(), opts) {
63
63
//! Ok(m) => { m }
64
- //! Err(f) => { fail!(f.to_err_msg ()) }
64
+ //! Err(f) => { fail!(f.to_str ()) }
65
65
//! };
66
66
//! if matches.opt_present("h") {
67
67
//! print_usage(program.as_slice(), opts);
94
94
#[ cfg( test, not( stage0) ) ] #[ phase( plugin, link) ] extern crate log;
95
95
96
96
use std:: cmp:: PartialEq ;
97
+ use std:: fmt;
97
98
use std:: result:: { Err , Ok } ;
98
99
use std:: result;
99
100
use std:: string:: String ;
100
101
101
102
/// Name of an option. Either a string or a single char.
102
- #[ deriving( Clone , PartialEq ) ]
103
+ #[ deriving( Clone , PartialEq , Eq ) ]
103
104
pub enum Name {
104
105
/// A string representing the long name of an option.
105
106
/// For example: "help"
@@ -110,7 +111,7 @@ pub enum Name {
110
111
}
111
112
112
113
/// Describes whether an option has an argument.
113
- #[ deriving( Clone , PartialEq ) ]
114
+ #[ deriving( Clone , PartialEq , Eq ) ]
114
115
pub enum HasArg {
115
116
/// The option requires an argument.
116
117
Yes ,
@@ -121,7 +122,7 @@ pub enum HasArg {
121
122
}
122
123
123
124
/// Describes how often an option may occur.
124
- #[ deriving( Clone , PartialEq ) ]
125
+ #[ deriving( Clone , PartialEq , Eq ) ]
125
126
pub enum Occur {
126
127
/// The option occurs once.
127
128
Req ,
@@ -132,7 +133,7 @@ pub enum Occur {
132
133
}
133
134
134
135
/// A description of a possible option.
135
- #[ deriving( Clone , PartialEq ) ]
136
+ #[ deriving( Clone , PartialEq , Eq ) ]
136
137
pub struct Opt {
137
138
/// Name of the option
138
139
pub name : Name ,
@@ -141,12 +142,12 @@ pub struct Opt {
141
142
/// How often it can occur
142
143
pub occur : Occur ,
143
144
/// Which options it aliases
144
- pub aliases : Vec < Opt > ,
145
+ pub aliases : Vec < Opt > ,
145
146
}
146
147
147
148
/// One group of options, e.g., both -h and --help, along with
148
149
/// their shared description and properties.
149
- #[ deriving( Clone , PartialEq ) ]
150
+ #[ deriving( Clone , PartialEq , Eq ) ]
150
151
pub struct OptGroup {
151
152
/// Short Name of the `OptGroup`
152
153
pub short_name : String ,
@@ -163,28 +164,28 @@ pub struct OptGroup {
163
164
}
164
165
165
166
/// Describes whether an option is given at all or has a value.
166
- #[ deriving( Clone , PartialEq ) ]
167
+ #[ deriving( Clone , PartialEq , Eq ) ]
167
168
enum Optval {
168
169
Val ( String ) ,
169
170
Given ,
170
171
}
171
172
172
173
/// The result of checking command line arguments. Contains a vector
173
174
/// of matches and a vector of free strings.
174
- #[ deriving( Clone , PartialEq ) ]
175
+ #[ deriving( Clone , PartialEq , Eq ) ]
175
176
pub struct Matches {
176
177
/// Options that matched
177
- opts : Vec < Opt > ,
178
+ opts : Vec < Opt > ,
178
179
/// Values of the Options that matched
179
- vals : Vec < Vec < Optval > > ,
180
+ vals : Vec < Vec < Optval > > ,
180
181
/// Free string fragments
181
182
pub free : Vec < String > ,
182
183
}
183
184
184
185
/// 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 ) ]
188
189
pub enum Fail_ {
189
190
/// The option requires an argument but none was passed.
190
191
ArgumentMissing ( String ) ,
@@ -199,7 +200,7 @@ pub enum Fail_ {
199
200
}
200
201
201
202
/// The type of failure that occurred.
202
- #[ deriving( PartialEq ) ]
203
+ #[ deriving( PartialEq , Eq ) ]
203
204
#[ allow( missing_doc) ]
204
205
pub enum FailType {
205
206
ArgumentMissing_ ,
@@ -498,22 +499,29 @@ pub fn opt(short_name: &str,
498
499
499
500
impl Fail_ {
500
501
/// Convert a `Fail_` enum into an error string.
502
+ #[ deprecated="use `Show` (`{}` format specifier)" ]
501
503
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 {
503
511
ArgumentMissing ( ref nm) => {
504
- format ! ( "Argument to option '{}' missing." , * nm)
512
+ write ! ( f , "Argument to option '{}' missing." , * nm)
505
513
}
506
514
UnrecognizedOption ( ref nm) => {
507
- format ! ( "Unrecognized option: '{}'." , * nm)
515
+ write ! ( f , "Unrecognized option: '{}'." , * nm)
508
516
}
509
517
OptionMissing ( ref nm) => {
510
- format ! ( "Required option '{}' missing." , * nm)
518
+ write ! ( f , "Required option '{}' missing." , * nm)
511
519
}
512
520
OptionDuplicated ( ref nm) => {
513
- format ! ( "Option '{}' given more than once." , * nm)
521
+ write ! ( f , "Option '{}' given more than once." , * nm)
514
522
}
515
523
UnexpectedArgument ( ref nm) => {
516
- format ! ( "Option '{}' does not take an argument." , * nm)
524
+ write ! ( f , "Option '{}' does not take an argument." , * nm)
517
525
}
518
526
}
519
527
}
@@ -522,8 +530,9 @@ impl Fail_ {
522
530
/// Parse command line arguments according to the provided options.
523
531
///
524
532
/// 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.
527
536
pub fn getopts ( args : & [ String ] , optgrps : & [ OptGroup ] ) -> Result {
528
537
let opts: Vec < Opt > = optgrps. iter ( ) . map ( |x| x. long_to_short ( ) ) . collect ( ) ;
529
538
let n_opts = opts. len ( ) ;
@@ -1110,7 +1119,6 @@ mod tests {
1110
1119
let rs = getopts ( args. as_slice ( ) , opts. as_slice ( ) ) ;
1111
1120
match rs {
1112
1121
Err ( f) => {
1113
- error ! ( "{:?}" , f. clone( ) . to_err_msg( ) ) ;
1114
1122
check_fail_type ( f, UnexpectedArgument_ ) ;
1115
1123
}
1116
1124
_ => fail ! ( )
0 commit comments