Skip to content

Commit fa27724

Browse files
bogglebrson
authored andcommitted
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

Diff for: src/comp/driver/rustc.rs

+5-4
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);

Diff for: src/compiletest/compiletest.rs

+5-2
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"),

Diff for: src/libstd/getopts.rs

+18-18
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] {

Diff for: src/libstd/test.rs

+3-2
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 =

Diff for: src/test/bench/shootout-pfib.rs

+5-2
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

0 commit comments

Comments
 (0)