Skip to content

Commit c395846

Browse files
killerswanbrson
authored andcommitted
---
yaml --- r: 23541 b: refs/heads/master c: 4ba9fdd h: refs/heads/master i: 23539: fa4e05f v: v3
1 parent 8b1b97c commit c395846

File tree

2 files changed

+51
-44
lines changed

2 files changed

+51
-44
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
refs/heads/master: 0e5a0e1da54f385c1ac623bb7cfe6cf19fcde8f8
2+
refs/heads/master: 4ba9fdd3627869f04ee39d6146023df822e0936e
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: cd6f24f9d14ac90d167386a56e7a6ac1f0318195
55
refs/heads/try: ffbe0e0e00374358b789b0037bcb3a577cd218be

trunk/src/libstd/getopts.rs

Lines changed: 50 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@
6262
* }
6363
*/
6464

65+
#[forbid(deprecated_mode)];
66+
#[forbid(deprecated_pattern)];
67+
6568
import core::result::{Err, Ok};
6669
import core::option;
6770
import core::option::{Some, None};
@@ -93,37 +96,38 @@ enum occur { req, optional, multi, }
9396
/// A description of a possible option
9497
type opt = {name: name, hasarg: hasarg, occur: occur};
9598

96-
fn mkname(nm: ~str) -> name {
99+
fn mkname(nm: &str) -> name {
100+
let unm = str::from_slice(nm);
97101
return if str::len(nm) == 1u {
98-
short(str::char_at(nm, 0u))
99-
} else { long(nm) };
102+
short(str::char_at(unm, 0u))
103+
} else { long(unm) };
100104
}
101105

102106
/// Create an option that is required and takes an argument
103-
fn reqopt(name: ~str) -> opt {
107+
fn reqopt(name: &str) -> opt {
104108
return {name: mkname(name), hasarg: yes, occur: req};
105109
}
106110

107111
/// Create an option that is optional and takes an argument
108-
fn optopt(name: ~str) -> opt {
112+
fn optopt(name: &str) -> opt {
109113
return {name: mkname(name), hasarg: yes, occur: optional};
110114
}
111115

112116
/// Create an option that is optional and does not take an argument
113-
fn optflag(name: ~str) -> opt {
117+
fn optflag(name: &str) -> opt {
114118
return {name: mkname(name), hasarg: no, occur: optional};
115119
}
116120

117121
/// Create an option that is optional and takes an optional argument
118-
fn optflagopt(name: ~str) -> opt {
122+
fn optflagopt(name: &str) -> opt {
119123
return {name: mkname(name), hasarg: maybe, occur: optional};
120124
}
121125

122126
/**
123127
* Create an option that is optional, takes an argument, and may occur
124128
* multiple times
125129
*/
126-
fn optmulti(name: ~str) -> opt {
130+
fn optmulti(name: &str) -> opt {
127131
return {name: mkname(name), hasarg: yes, occur: multi};
128132
}
129133

@@ -135,18 +139,18 @@ enum optval { val(~str), given, }
135139
*/
136140
type matches = {opts: ~[opt], vals: ~[~[optval]], free: ~[~str]};
137141

138-
fn is_arg(arg: ~str) -> bool {
142+
fn is_arg(arg: &str) -> bool {
139143
return str::len(arg) > 1u && arg[0] == '-' as u8;
140144
}
141145

142-
fn name_str(nm: name) -> ~str {
143-
return match nm {
146+
fn name_str(nm: &name) -> ~str {
147+
return match *nm {
144148
short(ch) => str::from_char(ch),
145149
long(s) => s
146150
};
147151
}
148152

149-
fn find_opt(opts: ~[opt], nm: name) -> Option<uint> {
153+
fn find_opt(opts: &[opt], +nm: name) -> Option<uint> {
150154
vec::position(opts, |opt| opt.name == nm)
151155
}
152156

@@ -163,7 +167,7 @@ enum fail_ {
163167
}
164168

165169
/// Convert a `fail_` enum into an error string
166-
fn fail_str(f: fail_) -> ~str {
170+
fn fail_str(+f: fail_) -> ~str {
167171
return match f {
168172
argument_missing(nm) => ~"Argument to option '" + nm + ~"' missing.",
169173
unrecognized_option(nm) => ~"Unrecognized option: '" + nm + ~"'.",
@@ -188,7 +192,7 @@ type result = result::Result<matches, fail_>;
188192
* `opt_str`, etc. to interrogate results. Returns `err(fail_)` on failure.
189193
* Use <fail_str> to get an error message.
190194
*/
191-
fn getopts(args: ~[~str], opts: ~[opt]) -> result unsafe {
195+
fn getopts(args: &[~str], opts: &[opt]) -> result unsafe {
192196
let n_opts = vec::len::<opt>(opts);
193197
fn f(_x: uint) -> ~[optval] { return ~[]; }
194198
let vals = vec::to_mut(vec::from_fn(n_opts, f));
@@ -261,12 +265,12 @@ fn getopts(args: ~[~str], opts: ~[opt]) -> result unsafe {
261265
name_pos += 1u;
262266
let optid = match find_opt(opts, nm) {
263267
Some(id) => id,
264-
None => return Err(unrecognized_option(name_str(nm)))
268+
None => return Err(unrecognized_option(name_str(&nm)))
265269
};
266270
match opts[optid].hasarg {
267271
no => {
268272
if !option::is_none::<~str>(i_arg) {
269-
return Err(unexpected_argument(name_str(nm)));
273+
return Err(unexpected_argument(name_str(&nm)));
270274
}
271275
vec::push(vals[optid], given);
272276
}
@@ -283,7 +287,7 @@ fn getopts(args: ~[~str], opts: ~[opt]) -> result unsafe {
283287
vec::push(vals[optid],
284288
val(option::get::<~str>(i_arg)));
285289
} else if i + 1u == l {
286-
return Err(argument_missing(name_str(nm)));
290+
return Err(argument_missing(name_str(&nm)));
287291
} else { i += 1u; vec::push(vals[optid], val(args[i])); }
288292
}
289293
}
@@ -297,42 +301,44 @@ fn getopts(args: ~[~str], opts: ~[opt]) -> result unsafe {
297301
let occ = opts[i].occur;
298302
if occ == req {
299303
if n == 0u {
300-
return Err(option_missing(name_str(opts[i].name)));
304+
return Err(option_missing(name_str(&(opts[i].name))));
301305
}
302306
}
303307
if occ != multi {
304308
if n > 1u {
305-
return Err(option_duplicated(name_str(opts[i].name)));
309+
return Err(option_duplicated(name_str(&(opts[i].name))));
306310
}
307311
}
308312
i += 1u;
309313
}
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});
311317
}
312318
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],
316322
None => {
317323
error!("No option '%s' defined", nm);
318324
fail
319325
}
320326
};
321327
}
322328

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]; }
324330

325331
/// 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)) > 0u;
332+
fn opt_present(+mm: matches, nm: &str) -> bool {
333+
return vec::len::<optval>(opt_vals(mm, nm)) > 0u;
328334
}
329335

330336
/// 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 {
332338
for vec::each(names) |nm| {
333-
match find_opt(m.opts, mkname(nm)) {
339+
match find_opt(mm.opts, mkname(nm)) {
334340
Some(_) => return true,
335-
_ => ()
341+
None => ()
336342
}
337343
}
338344
return false;
@@ -345,8 +351,8 @@ fn opts_present(m: matches, names: ~[~str]) -> bool {
345351
* Fails if the option was not matched or if the match did not take an
346352
* argument
347353
*/
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 };
350356
}
351357

352358
/**
@@ -355,9 +361,9 @@ fn opt_str(m: matches, nm: ~str) -> ~str {
355361
* Fails if the no option was provided from the given list, or if the no such
356362
* option took an argument
357363
*/
358-
fn opts_str(m: matches, names: ~[~str]) -> ~str {
364+
fn opts_str(+mm: matches, names: &[~str]) -> ~str {
359365
for vec::each(names) |nm| {
360-
match opt_val(m, nm) {
366+
match opt_val(mm, nm) {
361367
val(s) => return s,
362368
_ => ()
363369
}
@@ -372,17 +378,17 @@ fn opts_str(m: matches, names: ~[~str]) -> ~str {
372378
*
373379
* Used when an option accepts multiple values.
374380
*/
375-
fn opt_strs(m: matches, nm: ~str) -> ~[~str] {
381+
fn opt_strs(+mm: matches, nm: &str) -> ~[~str] {
376382
let mut acc: ~[~str] = ~[];
377-
for vec::each(opt_vals(m, nm)) |v| {
383+
for vec::each(opt_vals(mm, nm)) |v| {
378384
match v { val(s) => vec::push(acc, s), _ => () }
379385
}
380386
return acc;
381387
}
382388

383389
/// 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);
386392
if vec::len::<optval>(vals) == 0u { return None::<~str>; }
387393
return match vals[0] { val(s) => Some::<~str>(s), _ => None::<~str> };
388394
}
@@ -395,10 +401,11 @@ fn opt_maybe_str(m: matches, nm: ~str) -> Option<~str> {
395401
* present but no argument was provided, and the argument if the option was
396402
* present and an argument was provided.
397403
*/
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);
400406
if vec::len::<optval>(vals) == 0u { 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)) }
402409
}
403410

404411
#[cfg(test)]
@@ -414,7 +421,7 @@ mod tests {
414421
unexpected_argument_,
415422
}
416423

417-
fn check_fail_type(f: fail_, ft: fail_type) {
424+
fn check_fail_type(+f: fail_, ft: fail_type) {
418425
match f {
419426
argument_missing(_) => assert ft == argument_missing_,
420427
unrecognized_option(_) => assert ft == unrecognized_option_,
@@ -877,7 +884,7 @@ mod tests {
877884
let opts = ~[optopt(~"e"), optopt(~"encrypt")];
878885
let matches = match getopts(args, opts) {
879886
result::Ok(m) => m,
880-
result::Err(_) => fail
887+
result::Err(_f) => fail
881888
};
882889
assert opts_present(matches, ~[~"e"]);
883890
assert opts_present(matches, ~[~"encrypt"]);
@@ -898,7 +905,7 @@ mod tests {
898905
let opts = ~[optmulti(~"L")];
899906
let matches = match getopts(args, opts) {
900907
result::Ok(m) => m,
901-
result::Err(_) => fail
908+
result::Err(_f) => fail
902909
};
903910
assert opts_present(matches, ~[~"L"]);
904911
assert opts_str(matches, ~[~"L"]) == ~"foo";

0 commit comments

Comments
 (0)