Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit fa27724

Browse files
bogglebrson
authored andcommittedDec 19, 2011
std: getopts now uses result::t (fixes #1289)
1 parent bd6b80c commit fa27724

File tree

6 files changed

+72
-63
lines changed

6 files changed

+72
-63
lines changed
 

‎src/comp/driver/rustc.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import syntax::print::{pp, pprust};
1111
import util::{ppaux, filesearch};
1212
import back::link;
1313
import core::{option, str, vec, int, result};
14+
import result::{ok, err};
1415
import std::{fs, io, getopts};
1516
import option::{some, none};
1617
import getopts::{optopt, optmulti, optflag, optflagopt, opt_present};
@@ -621,8 +622,8 @@ fn main(args: [str]) {
621622
let args = args, binary = vec::shift(args);
622623
let match =
623624
alt getopts::getopts(args, opts()) {
624-
getopts::success(m) { m }
625-
getopts::failure(f) {
625+
ok(m) { m }
626+
err(f) {
626627
early_error(getopts::fail_str(f))
627628
}
628629
};
@@ -670,7 +671,7 @@ mod test {
670671
fn test_switch_implies_cfg_test() {
671672
let match =
672673
alt getopts::getopts(["--test"], opts()) {
673-
getopts::success(m) { m }
674+
ok(m) { m }
674675
};
675676
let sessopts = build_session_options(match);
676677
let sess = build_session(sessopts);
@@ -684,7 +685,7 @@ mod test {
684685
fn test_switch_implies_cfg_test_unless_cfg_test() {
685686
let match =
686687
alt getopts::getopts(["--test", "--cfg=test"], opts()) {
687-
getopts::success(m) { m }
688+
ok(m) { m }
688689
};
689690
let sessopts = build_session_options(match);
690691
let sess = build_session(sessopts);

‎src/compiletest/compiletest.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ import str;
99
import vec;
1010
import task;
1111

12+
import core::result;
13+
import result::{ok, err};
14+
1215
import comm::port;
1316
import comm::chan;
1417
import comm::send;
@@ -42,8 +45,8 @@ fn parse_config(args: [str]) -> config {
4245
let args_ = vec::tail(args);
4346
let match =
4447
alt getopts::getopts(args_, opts) {
45-
getopts::success(m) { m }
46-
getopts::failure(f) { fail getopts::fail_str(f) }
48+
ok(m) { m }
49+
err(f) { fail getopts::fail_str(f) }
4750
};
4851

4952
ret {compile_lib_path: getopts::opt_str(match, "compile-lib-path"),

‎src/libstd/getopts.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ name following -o, and accepts both -h and --help as optional flags.
2626
> optflag("help")
2727
> ];
2828
> let match = alt getopts(vec::shift(args), opts) {
29-
> success(m) { m }
30-
> failure(f) { fail fail_str(f) }
29+
> ok(m) { m }
30+
> err(f) { fail fail_str(f) }
3131
> };
3232
> if opt_present(match, "h") || opt_present(match, "help") {
3333
> print_usage();
@@ -45,18 +45,17 @@ name following -o, and accepts both -h and --help as optional flags.
4545
4646
*/
4747

48+
import core::result;
49+
import core::result::{err, ok};
4850
import core::option;
49-
import option::{some, none};
51+
import core::option::{some, none};
5052
export opt;
5153
export reqopt;
5254
export optopt;
5355
export optflag;
5456
export optflagopt;
5557
export optmulti;
5658
export getopts;
57-
export result;
58-
export success;
59-
export failure;
6059
export match;
6160
export fail_;
6261
export fail_str;
@@ -193,13 +192,14 @@ fn fail_str(f: fail_) -> str {
193192
Type: result
194193
195194
The result of parsing a command line with a set of options
195+
(result::t<match, fail_>)
196196
197197
Variants:
198198
199-
success(match) - Returned from getopts on success
200-
failure(fail_) - Returned from getopts on failure
199+
ok(match) - Returned from getopts on success
200+
err(fail_) - Returned from getopts on failure
201201
*/
202-
tag result { success(match); failure(fail_); }
202+
type result = result::t<match, fail_>;
203203

204204
/*
205205
Function: getopts
@@ -208,9 +208,9 @@ Parse command line arguments according to the provided options
208208
209209
Returns:
210210
211-
success(match) - On success. Use functions such as <opt_present>
212-
<opt_str>, etc. to interrogate results.
213-
failure(fail_) - On failure. Use <fail_str> to get an error message.
211+
ok(match) - On success. Use functions such as <opt_present>
212+
<opt_str>, etc. to interrogate results.
213+
err(fail_) - On failure. Use <fail_str> to get an error message.
214214
*/
215215
fn getopts(args: [str], opts: [opt]) -> result {
216216
let n_opts = vec::len::<opt>(opts);
@@ -258,12 +258,12 @@ fn getopts(args: [str], opts: [opt]) -> result {
258258
let optid;
259259
alt find_opt(opts, nm) {
260260
some(id) { optid = id; }
261-
none. { ret failure(unrecognized_option(name_str(nm))); }
261+
none. { ret err(unrecognized_option(name_str(nm))); }
262262
}
263263
alt opts[optid].hasarg {
264264
no. {
265265
if !option::is_none::<str>(i_arg) {
266-
ret failure(unexpected_argument(name_str(nm)));
266+
ret err(unexpected_argument(name_str(nm)));
267267
}
268268
vals[optid] += [given];
269269
}
@@ -279,7 +279,7 @@ fn getopts(args: [str], opts: [opt]) -> result {
279279
if !option::is_none::<str>(i_arg) {
280280
vals[optid] += [val(option::get::<str>(i_arg))];
281281
} else if i + 1u == l {
282-
ret failure(argument_missing(name_str(nm)));
282+
ret err(argument_missing(name_str(nm)));
283283
} else { i += 1u; vals[optid] += [val(args[i])]; }
284284
}
285285
}
@@ -293,17 +293,17 @@ fn getopts(args: [str], opts: [opt]) -> result {
293293
let occ = opts[i].occur;
294294
if occ == req {
295295
if n == 0u {
296-
ret failure(option_missing(name_str(opts[i].name)));
296+
ret err(option_missing(name_str(opts[i].name)));
297297
}
298298
}
299299
if occ != multi {
300300
if n > 1u {
301-
ret failure(option_duplicated(name_str(opts[i].name)));
301+
ret err(option_duplicated(name_str(opts[i].name)));
302302
}
303303
}
304304
i += 1u;
305305
}
306-
ret success({opts: opts, vals: vals, free: free});
306+
ret ok({opts: opts, vals: vals, free: free});
307307
}
308308

309309
fn opt_vals(m: match, nm: str) -> [optval] {

‎src/libstd/test.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import task::task;
99
import core::option;
1010
import core::either;
1111
import core::vec;
12+
import core::result::{ok, err};
1213

1314
export test_name;
1415
export test_fn;
@@ -82,8 +83,8 @@ fn parse_opts(args: [str]) : vec::is_not_empty(args) -> opt_res {
8283
let opts = [getopts::optflag("ignored")];
8384
let match =
8485
alt getopts::getopts(args_, opts) {
85-
getopts::success(m) { m }
86-
getopts::failure(f) { ret either::right(getopts::fail_str(f)) }
86+
ok(m) { m }
87+
err(f) { ret either::right(getopts::fail_str(f)) }
8788
};
8889

8990
let filter =

‎src/test/bench/shootout-pfib.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ import comm::chan;
2727
import comm::send;
2828
import comm::recv;
2929

30+
import core::result;
31+
import result::{ok, err};
32+
3033
fn fib(n: int) -> int {
3134
fn pfib(args: (chan<int>, int)) {
3235
let (c, n) = args;
@@ -58,8 +61,8 @@ fn parse_opts(argv: [str]) -> config {
5861

5962

6063
alt getopts::getopts(opt_args, opts) {
61-
getopts::success(m) { ret {stress: getopts::opt_present(m, "stress")} }
62-
getopts::failure(_) { fail; }
64+
ok(m) { ret {stress: getopts::opt_present(m, "stress")} }
65+
err(_) { fail; }
6366
}
6467
}
6568

‎src/test/stdtest/getopts.rs

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import core::*;
22

33
use std;
44
import opt = std::getopts;
5+
import result::{err, ok};
56

67
tag fail_type {
78
argument_missing;
@@ -30,7 +31,7 @@ fn test_reqopt_long() {
3031
let opts = [opt::reqopt("test")];
3132
let rs = opt::getopts(args, opts);
3233
alt rs {
33-
opt::success(m) {
34+
ok(m) {
3435
assert (opt::opt_present(m, "test"));
3536
assert (opt::opt_str(m, "test") == "20");
3637
}
@@ -44,7 +45,7 @@ fn test_reqopt_long_missing() {
4445
let opts = [opt::reqopt("test")];
4546
let rs = opt::getopts(args, opts);
4647
alt rs {
47-
opt::failure(f) { check_fail_type(f, option_missing); }
48+
err(f) { check_fail_type(f, option_missing); }
4849
_ { fail; }
4950
}
5051
}
@@ -55,7 +56,7 @@ fn test_reqopt_long_no_arg() {
5556
let opts = [opt::reqopt("test")];
5657
let rs = opt::getopts(args, opts);
5758
alt rs {
58-
opt::failure(f) { check_fail_type(f, argument_missing); }
59+
err(f) { check_fail_type(f, argument_missing); }
5960
_ { fail; }
6061
}
6162
}
@@ -66,7 +67,7 @@ fn test_reqopt_long_multi() {
6667
let opts = [opt::reqopt("test")];
6768
let rs = opt::getopts(args, opts);
6869
alt rs {
69-
opt::failure(f) { check_fail_type(f, option_duplicated); }
70+
err(f) { check_fail_type(f, option_duplicated); }
7071
_ { fail; }
7172
}
7273
}
@@ -77,7 +78,7 @@ fn test_reqopt_short() {
7778
let opts = [opt::reqopt("t")];
7879
let rs = opt::getopts(args, opts);
7980
alt rs {
80-
opt::success(m) {
81+
ok(m) {
8182
assert (opt::opt_present(m, "t"));
8283
assert (opt::opt_str(m, "t") == "20");
8384
}
@@ -91,7 +92,7 @@ fn test_reqopt_short_missing() {
9192
let opts = [opt::reqopt("t")];
9293
let rs = opt::getopts(args, opts);
9394
alt rs {
94-
opt::failure(f) { check_fail_type(f, option_missing); }
95+
err(f) { check_fail_type(f, option_missing); }
9596
_ { fail; }
9697
}
9798
}
@@ -102,7 +103,7 @@ fn test_reqopt_short_no_arg() {
102103
let opts = [opt::reqopt("t")];
103104
let rs = opt::getopts(args, opts);
104105
alt rs {
105-
opt::failure(f) { check_fail_type(f, argument_missing); }
106+
err(f) { check_fail_type(f, argument_missing); }
106107
_ { fail; }
107108
}
108109
}
@@ -113,7 +114,7 @@ fn test_reqopt_short_multi() {
113114
let opts = [opt::reqopt("t")];
114115
let rs = opt::getopts(args, opts);
115116
alt rs {
116-
opt::failure(f) { check_fail_type(f, option_duplicated); }
117+
err(f) { check_fail_type(f, option_duplicated); }
117118
_ { fail; }
118119
}
119120
}
@@ -126,7 +127,7 @@ fn test_optopt_long() {
126127
let opts = [opt::optopt("test")];
127128
let rs = opt::getopts(args, opts);
128129
alt rs {
129-
opt::success(m) {
130+
ok(m) {
130131
assert (opt::opt_present(m, "test"));
131132
assert (opt::opt_str(m, "test") == "20");
132133
}
@@ -140,7 +141,7 @@ fn test_optopt_long_missing() {
140141
let opts = [opt::optopt("test")];
141142
let rs = opt::getopts(args, opts);
142143
alt rs {
143-
opt::success(m) { assert (!opt::opt_present(m, "test")); }
144+
ok(m) { assert (!opt::opt_present(m, "test")); }
144145
_ { fail; }
145146
}
146147
}
@@ -151,7 +152,7 @@ fn test_optopt_long_no_arg() {
151152
let opts = [opt::optopt("test")];
152153
let rs = opt::getopts(args, opts);
153154
alt rs {
154-
opt::failure(f) { check_fail_type(f, argument_missing); }
155+
err(f) { check_fail_type(f, argument_missing); }
155156
_ { fail; }
156157
}
157158
}
@@ -162,7 +163,7 @@ fn test_optopt_long_multi() {
162163
let opts = [opt::optopt("test")];
163164
let rs = opt::getopts(args, opts);
164165
alt rs {
165-
opt::failure(f) { check_fail_type(f, option_duplicated); }
166+
err(f) { check_fail_type(f, option_duplicated); }
166167
_ { fail; }
167168
}
168169
}
@@ -173,7 +174,7 @@ fn test_optopt_short() {
173174
let opts = [opt::optopt("t")];
174175
let rs = opt::getopts(args, opts);
175176
alt rs {
176-
opt::success(m) {
177+
ok(m) {
177178
assert (opt::opt_present(m, "t"));
178179
assert (opt::opt_str(m, "t") == "20");
179180
}
@@ -187,7 +188,7 @@ fn test_optopt_short_missing() {
187188
let opts = [opt::optopt("t")];
188189
let rs = opt::getopts(args, opts);
189190
alt rs {
190-
opt::success(m) { assert (!opt::opt_present(m, "t")); }
191+
ok(m) { assert (!opt::opt_present(m, "t")); }
191192
_ { fail; }
192193
}
193194
}
@@ -198,7 +199,7 @@ fn test_optopt_short_no_arg() {
198199
let opts = [opt::optopt("t")];
199200
let rs = opt::getopts(args, opts);
200201
alt rs {
201-
opt::failure(f) { check_fail_type(f, argument_missing); }
202+
err(f) { check_fail_type(f, argument_missing); }
202203
_ { fail; }
203204
}
204205
}
@@ -209,7 +210,7 @@ fn test_optopt_short_multi() {
209210
let opts = [opt::optopt("t")];
210211
let rs = opt::getopts(args, opts);
211212
alt rs {
212-
opt::failure(f) { check_fail_type(f, option_duplicated); }
213+
err(f) { check_fail_type(f, option_duplicated); }
213214
_ { fail; }
214215
}
215216
}
@@ -222,7 +223,7 @@ fn test_optflag_long() {
222223
let opts = [opt::optflag("test")];
223224
let rs = opt::getopts(args, opts);
224225
alt rs {
225-
opt::success(m) { assert (opt::opt_present(m, "test")); }
226+
ok(m) { assert (opt::opt_present(m, "test")); }
226227
_ { fail; }
227228
}
228229
}
@@ -233,7 +234,7 @@ fn test_optflag_long_missing() {
233234
let opts = [opt::optflag("test")];
234235
let rs = opt::getopts(args, opts);
235236
alt rs {
236-
opt::success(m) { assert (!opt::opt_present(m, "test")); }
237+
ok(m) { assert (!opt::opt_present(m, "test")); }
237238
_ { fail; }
238239
}
239240
}
@@ -244,7 +245,7 @@ fn test_optflag_long_arg() {
244245
let opts = [opt::optflag("test")];
245246
let rs = opt::getopts(args, opts);
246247
alt rs {
247-
opt::failure(f) {
248+
err(f) {
248249
log_err opt::fail_str(f);
249250
check_fail_type(f, unexpected_argument);
250251
}
@@ -258,7 +259,7 @@ fn test_optflag_long_multi() {
258259
let opts = [opt::optflag("test")];
259260
let rs = opt::getopts(args, opts);
260261
alt rs {
261-
opt::failure(f) { check_fail_type(f, option_duplicated); }
262+
err(f) { check_fail_type(f, option_duplicated); }
262263
_ { fail; }
263264
}
264265
}
@@ -269,7 +270,7 @@ fn test_optflag_short() {
269270
let opts = [opt::optflag("t")];
270271
let rs = opt::getopts(args, opts);
271272
alt rs {
272-
opt::success(m) { assert (opt::opt_present(m, "t")); }
273+
ok(m) { assert (opt::opt_present(m, "t")); }
273274
_ { fail; }
274275
}
275276
}
@@ -280,7 +281,7 @@ fn test_optflag_short_missing() {
280281
let opts = [opt::optflag("t")];
281282
let rs = opt::getopts(args, opts);
282283
alt rs {
283-
opt::success(m) { assert (!opt::opt_present(m, "t")); }
284+
ok(m) { assert (!opt::opt_present(m, "t")); }
284285
_ { fail; }
285286
}
286287
}
@@ -291,7 +292,7 @@ fn test_optflag_short_arg() {
291292
let opts = [opt::optflag("t")];
292293
let rs = opt::getopts(args, opts);
293294
alt rs {
294-
opt::success(m) {
295+
ok(m) {
295296
// The next variable after the flag is just a free argument
296297

297298
assert (m.free[0] == "20");
@@ -306,7 +307,7 @@ fn test_optflag_short_multi() {
306307
let opts = [opt::optflag("t")];
307308
let rs = opt::getopts(args, opts);
308309
alt rs {
309-
opt::failure(f) { check_fail_type(f, option_duplicated); }
310+
err(f) { check_fail_type(f, option_duplicated); }
310311
_ { fail; }
311312
}
312313
}
@@ -319,7 +320,7 @@ fn test_optmulti_long() {
319320
let opts = [opt::optmulti("test")];
320321
let rs = opt::getopts(args, opts);
321322
alt rs {
322-
opt::success(m) {
323+
ok(m) {
323324
assert (opt::opt_present(m, "test"));
324325
assert (opt::opt_str(m, "test") == "20");
325326
}
@@ -333,7 +334,7 @@ fn test_optmulti_long_missing() {
333334
let opts = [opt::optmulti("test")];
334335
let rs = opt::getopts(args, opts);
335336
alt rs {
336-
opt::success(m) { assert (!opt::opt_present(m, "test")); }
337+
ok(m) { assert (!opt::opt_present(m, "test")); }
337338
_ { fail; }
338339
}
339340
}
@@ -344,7 +345,7 @@ fn test_optmulti_long_no_arg() {
344345
let opts = [opt::optmulti("test")];
345346
let rs = opt::getopts(args, opts);
346347
alt rs {
347-
opt::failure(f) { check_fail_type(f, argument_missing); }
348+
err(f) { check_fail_type(f, argument_missing); }
348349
_ { fail; }
349350
}
350351
}
@@ -355,7 +356,7 @@ fn test_optmulti_long_multi() {
355356
let opts = [opt::optmulti("test")];
356357
let rs = opt::getopts(args, opts);
357358
alt rs {
358-
opt::success(m) {
359+
ok(m) {
359360
assert (opt::opt_present(m, "test"));
360361
assert (opt::opt_str(m, "test") == "20");
361362
assert (opt::opt_strs(m, "test")[0] == "20");
@@ -371,7 +372,7 @@ fn test_optmulti_short() {
371372
let opts = [opt::optmulti("t")];
372373
let rs = opt::getopts(args, opts);
373374
alt rs {
374-
opt::success(m) {
375+
ok(m) {
375376
assert (opt::opt_present(m, "t"));
376377
assert (opt::opt_str(m, "t") == "20");
377378
}
@@ -385,7 +386,7 @@ fn test_optmulti_short_missing() {
385386
let opts = [opt::optmulti("t")];
386387
let rs = opt::getopts(args, opts);
387388
alt rs {
388-
opt::success(m) { assert (!opt::opt_present(m, "t")); }
389+
ok(m) { assert (!opt::opt_present(m, "t")); }
389390
_ { fail; }
390391
}
391392
}
@@ -396,7 +397,7 @@ fn test_optmulti_short_no_arg() {
396397
let opts = [opt::optmulti("t")];
397398
let rs = opt::getopts(args, opts);
398399
alt rs {
399-
opt::failure(f) { check_fail_type(f, argument_missing); }
400+
err(f) { check_fail_type(f, argument_missing); }
400401
_ { fail; }
401402
}
402403
}
@@ -407,7 +408,7 @@ fn test_optmulti_short_multi() {
407408
let opts = [opt::optmulti("t")];
408409
let rs = opt::getopts(args, opts);
409410
alt rs {
410-
opt::success(m) {
411+
ok(m) {
411412
assert (opt::opt_present(m, "t"));
412413
assert (opt::opt_str(m, "t") == "20");
413414
assert (opt::opt_strs(m, "t")[0] == "20");
@@ -423,7 +424,7 @@ fn test_unrecognized_option_long() {
423424
let opts = [opt::optmulti("t")];
424425
let rs = opt::getopts(args, opts);
425426
alt rs {
426-
opt::failure(f) { check_fail_type(f, unrecognized_option); }
427+
err(f) { check_fail_type(f, unrecognized_option); }
427428
_ { fail; }
428429
}
429430
}
@@ -434,7 +435,7 @@ fn test_unrecognized_option_short() {
434435
let opts = [opt::optmulti("test")];
435436
let rs = opt::getopts(args, opts);
436437
alt rs {
437-
opt::failure(f) { check_fail_type(f, unrecognized_option); }
438+
err(f) { check_fail_type(f, unrecognized_option); }
438439
_ { fail; }
439440
}
440441
}
@@ -449,7 +450,7 @@ fn test_combined() {
449450
opt::optflag("f"), opt::optmulti("m"), opt::optopt("notpresent")];
450451
let rs = opt::getopts(args, opts);
451452
alt rs {
452-
opt::success(m) {
453+
ok(m) {
453454
assert (m.free[0] == "prog");
454455
assert (m.free[1] == "free1");
455456
assert (opt::opt_str(m, "s") == "20");

0 commit comments

Comments
 (0)
Please sign in to comment.