62
62
* }
63
63
*/
64
64
65
+ #[ forbid( deprecated_mode) ] ;
66
+ #[ forbid( deprecated_pattern) ] ;
67
+
65
68
import core:: result:: { Err , Ok } ;
66
69
import core:: option;
67
70
import core:: option:: { Some , None } ;
@@ -93,37 +96,38 @@ enum occur { req, optional, multi, }
93
96
/// A description of a possible option
94
97
type opt = { name : name , hasarg : hasarg , occur : occur } ;
95
98
96
- fn mkname ( nm : ~str ) -> name {
99
+ fn mkname ( nm : & str ) -> name {
100
+ let unm = str:: from_slice ( nm) ;
97
101
return if str:: len ( nm) == 1 u {
98
- short ( str:: char_at ( nm , 0 u) )
99
- } else { long ( nm ) } ;
102
+ short ( str:: char_at ( unm , 0 u) )
103
+ } else { long ( unm ) } ;
100
104
}
101
105
102
106
/// Create an option that is required and takes an argument
103
- fn reqopt ( name : ~ str ) -> opt {
107
+ fn reqopt ( name : & str ) -> opt {
104
108
return { name: mkname ( name) , hasarg: yes, occur: req} ;
105
109
}
106
110
107
111
/// Create an option that is optional and takes an argument
108
- fn optopt ( name : ~ str ) -> opt {
112
+ fn optopt ( name : & str ) -> opt {
109
113
return { name: mkname ( name) , hasarg: yes, occur: optional} ;
110
114
}
111
115
112
116
/// Create an option that is optional and does not take an argument
113
- fn optflag ( name : ~ str ) -> opt {
117
+ fn optflag ( name : & str ) -> opt {
114
118
return { name: mkname ( name) , hasarg: no, occur: optional} ;
115
119
}
116
120
117
121
/// Create an option that is optional and takes an optional argument
118
- fn optflagopt ( name : ~ str ) -> opt {
122
+ fn optflagopt ( name : & str ) -> opt {
119
123
return { name: mkname ( name) , hasarg: maybe, occur: optional} ;
120
124
}
121
125
122
126
/**
123
127
* Create an option that is optional, takes an argument, and may occur
124
128
* multiple times
125
129
*/
126
- fn optmulti ( name : ~ str ) -> opt {
130
+ fn optmulti ( name : & str ) -> opt {
127
131
return { name: mkname ( name) , hasarg: yes, occur: multi} ;
128
132
}
129
133
@@ -135,18 +139,18 @@ enum optval { val(~str), given, }
135
139
*/
136
140
type matches = { opts : ~[ opt ] , vals : ~[ ~[ optval ] ] , free : ~[ ~str ] } ;
137
141
138
- fn is_arg ( arg : ~ str ) -> bool {
142
+ fn is_arg ( arg : & str ) -> bool {
139
143
return str:: len ( arg) > 1 u && arg[ 0 ] == '-' as u8 ;
140
144
}
141
145
142
- fn name_str ( nm : name ) -> ~str {
143
- return match nm {
146
+ fn name_str ( nm : & name ) -> ~str {
147
+ return match * nm {
144
148
short( ch) => str:: from_char ( ch) ,
145
149
long( s) => s
146
150
} ;
147
151
}
148
152
149
- fn find_opt ( opts : ~ [ opt ] , nm : name ) -> Option < uint > {
153
+ fn find_opt ( opts : & [ opt ] , + nm : name ) -> Option < uint > {
150
154
vec:: position ( opts, |opt| opt. name == nm)
151
155
}
152
156
@@ -163,7 +167,7 @@ enum fail_ {
163
167
}
164
168
165
169
/// Convert a `fail_` enum into an error string
166
- fn fail_str ( f : fail_ ) -> ~str {
170
+ fn fail_str ( + f : fail_ ) -> ~str {
167
171
return match f {
168
172
argument_missing( nm) => ~"Argument to option ' " + nm + ~"' missing. ",
169
173
unrecognized_option ( nm) => ~"Unrecognized option: ' " + nm + ~"' . ",
@@ -188,7 +192,7 @@ type result = result::Result<matches, fail_>;
188
192
* `opt_str`, etc. to interrogate results. Returns `err(fail_)` on failure.
189
193
* Use <fail_str> to get an error message.
190
194
*/
191
- fn getopts( args : ~ [ ~str ] , opts : ~ [ opt ] ) -> result unsafe {
195
+ fn getopts( args : & [ ~str ] , opts : & [ opt ] ) -> result unsafe {
192
196
let n_opts = vec:: len :: < opt > ( opts) ;
193
197
fn f ( _x : uint ) -> ~[ optval ] { return ~[ ] ; }
194
198
let vals = vec:: to_mut ( vec:: from_fn ( n_opts, f) ) ;
@@ -261,12 +265,12 @@ fn getopts(args: ~[~str], opts: ~[opt]) -> result unsafe {
261
265
name_pos += 1u;
262
266
let optid = match find_opt(opts, nm) {
263
267
Some(id) => id,
264
- None => return Err(unrecognized_option(name_str(nm)))
268
+ None => return Err(unrecognized_option(name_str(& nm)))
265
269
};
266
270
match opts[optid].hasarg {
267
271
no => {
268
272
if !option::is_none::<~str>(i_arg) {
269
- return Err(unexpected_argument(name_str(nm)));
273
+ return Err(unexpected_argument(name_str(& nm)));
270
274
}
271
275
vec::push(vals[optid], given);
272
276
}
@@ -283,7 +287,7 @@ fn getopts(args: ~[~str], opts: ~[opt]) -> result unsafe {
283
287
vec::push(vals[optid],
284
288
val(option::get::<~str>(i_arg)));
285
289
} else if i + 1u == l {
286
- return Err(argument_missing(name_str(nm)));
290
+ return Err(argument_missing(name_str(& nm)));
287
291
} else { i += 1u; vec::push(vals[optid], val(args[i])); }
288
292
}
289
293
}
@@ -297,42 +301,44 @@ fn getopts(args: ~[~str], opts: ~[opt]) -> result unsafe {
297
301
let occ = opts[i].occur;
298
302
if occ == req {
299
303
if n == 0u {
300
- return Err(option_missing(name_str(opts[i].name)));
304
+ return Err(option_missing(name_str(&( opts[i].name) )));
301
305
}
302
306
}
303
307
if occ != multi {
304
308
if n > 1u {
305
- return Err(option_duplicated(name_str(opts[i].name)));
309
+ return Err(option_duplicated(name_str(&( opts[i].name) )));
306
310
}
307
311
}
308
312
i += 1u;
309
313
}
310
- return Ok({opts: opts, vals: vec::from_mut(vals), free: free});
314
+ return Ok({opts: vec::from_slice(opts),
315
+ vals: vec::from_mut(vals),
316
+ free: free});
311
317
}
312
318
313
- fn opt_vals(m : matches, nm: ~ str) -> ~[optval] {
314
- return match find_opt(m .opts, mkname(nm)) {
315
- Some(id) => m .vals[id],
319
+ fn opt_vals(+mm : matches, nm: & str) -> ~[optval] {
320
+ return match find_opt(mm .opts, mkname(nm)) {
321
+ Some(id) => mm .vals[id],
316
322
None => {
317
323
error!(" No option ' %s' defined", nm) ;
318
324
fail
319
325
}
320
326
} ;
321
327
}
322
328
323
- fn opt_val( m : matches, nm: ~ str ) -> optval { return opt_vals ( m , nm) [ 0 ] ; }
329
+ fn opt_val( +mm : matches, nm: & str ) -> optval { return opt_vals ( mm , nm) [ 0 ] ; }
324
330
325
331
/// Returns true if an option was matched
326
- fn opt_present ( m : matches , nm : ~ str ) -> bool {
327
- return vec:: len :: < optval > ( opt_vals ( m , nm) ) > 0 u;
332
+ fn opt_present ( + mm : matches , nm : & str ) -> bool {
333
+ return vec:: len :: < optval > ( opt_vals ( mm , nm) ) > 0 u;
328
334
}
329
335
330
336
/// Returns true if any of several options were matched
331
- fn opts_present ( m : matches , names : ~ [ ~str ] ) -> bool {
337
+ fn opts_present ( + mm : matches , names : & [ ~str ] ) -> bool {
332
338
for vec:: each( names) |nm| {
333
- match find_opt ( m . opts , mkname ( nm) ) {
339
+ match find_opt ( mm . opts , mkname ( nm) ) {
334
340
Some ( _) => return true ,
335
- _ => ( )
341
+ None => ( )
336
342
}
337
343
}
338
344
return false ;
@@ -345,8 +351,8 @@ fn opts_present(m: matches, names: ~[~str]) -> bool {
345
351
* Fails if the option was not matched or if the match did not take an
346
352
* argument
347
353
*/
348
- fn opt_str ( m : matches , nm : ~ str ) -> ~str {
349
- return match opt_val ( m , nm) { val( s) => s, _ => fail } ;
354
+ fn opt_str ( + mm : matches , nm : & str ) -> ~str {
355
+ return match opt_val ( mm , nm) { val( s) => s, _ => fail } ;
350
356
}
351
357
352
358
/**
@@ -355,9 +361,9 @@ fn opt_str(m: matches, nm: ~str) -> ~str {
355
361
* Fails if the no option was provided from the given list, or if the no such
356
362
* option took an argument
357
363
*/
358
- fn opts_str ( m : matches , names : ~ [ ~str ] ) -> ~str {
364
+ fn opts_str ( + mm : matches , names : & [ ~str ] ) -> ~str {
359
365
for vec:: each( names) |nm| {
360
- match opt_val ( m , nm) {
366
+ match opt_val ( mm , nm) {
361
367
val( s) => return s,
362
368
_ => ( )
363
369
}
@@ -372,17 +378,17 @@ fn opts_str(m: matches, names: ~[~str]) -> ~str {
372
378
*
373
379
* Used when an option accepts multiple values.
374
380
*/
375
- fn opt_strs ( m : matches , nm : ~ str ) -> ~[ ~str ] {
381
+ fn opt_strs ( + mm : matches , nm : & str ) -> ~[ ~str ] {
376
382
let mut acc: ~[ ~str ] = ~[ ] ;
377
- for vec:: each( opt_vals( m , nm) ) |v| {
383
+ for vec:: each( opt_vals( mm , nm) ) |v| {
378
384
match v { val( s) => vec:: push ( acc, s) , _ => ( ) }
379
385
}
380
386
return acc;
381
387
}
382
388
383
389
/// Returns the string argument supplied to a matching option or none
384
- fn opt_maybe_str ( m : matches , nm : ~ str ) -> Option < ~str > {
385
- let vals = opt_vals ( m , nm) ;
390
+ fn opt_maybe_str ( + mm : matches , nm : & str ) -> Option < ~str > {
391
+ let vals = opt_vals ( mm , nm) ;
386
392
if vec:: len :: < optval > ( vals) == 0 u { return None :: < ~str > ; }
387
393
return match vals[ 0 ] { val( s) => Some :: < ~str > ( s) , _ => None :: < ~str > } ;
388
394
}
@@ -395,10 +401,11 @@ fn opt_maybe_str(m: matches, nm: ~str) -> Option<~str> {
395
401
* present but no argument was provided, and the argument if the option was
396
402
* present and an argument was provided.
397
403
*/
398
- fn opt_default ( m : matches , nm : ~ str , def : ~ str ) -> Option < ~str > {
399
- let vals = opt_vals ( m , nm) ;
404
+ fn opt_default ( + mm : matches , nm : & str , def : & str ) -> Option < ~str > {
405
+ let vals = opt_vals ( mm , nm) ;
400
406
if vec:: len :: < optval > ( vals) == 0 u { return None :: < ~str > ; }
401
- return match vals[ 0 ] { val( s) => Some :: < ~str > ( s) , _ => Some :: < ~str > ( def) }
407
+ return match vals[ 0 ] { val( s) => Some :: < ~str > ( s) ,
408
+ _ => Some :: < ~str > ( str:: from_slice ( def) ) }
402
409
}
403
410
404
411
#[ cfg( test) ]
@@ -414,7 +421,7 @@ mod tests {
414
421
unexpected_argument_,
415
422
}
416
423
417
- fn check_fail_type ( f : fail_ , ft : fail_type ) {
424
+ fn check_fail_type ( + f : fail_ , ft : fail_type ) {
418
425
match f {
419
426
argument_missing( _) => assert ft == argument_missing_,
420
427
unrecognized_option( _) => assert ft == unrecognized_option_,
@@ -877,7 +884,7 @@ mod tests {
877
884
let opts = ~[ optopt ( ~"e") , optopt ( ~"encrypt") ] ;
878
885
let matches = match getopts ( args, opts) {
879
886
result:: Ok ( m) => m,
880
- result:: Err ( _ ) => fail
887
+ result:: Err ( _f ) => fail
881
888
} ;
882
889
assert opts_present ( matches, ~[ ~"e"] ) ;
883
890
assert opts_present ( matches, ~[ ~"encrypt"] ) ;
@@ -898,7 +905,7 @@ mod tests {
898
905
let opts = ~[optmulti(~" L ")];
899
906
let matches = match getopts(args, opts) {
900
907
result::Ok(m) => m,
901
- result::Err(_ ) => fail
908
+ result::Err(_f ) => fail
902
909
};
903
910
assert opts_present(matches, ~[~" L "]);
904
911
assert opts_str(matches, ~[~" L "]) == ~" foo";
0 commit comments