Permalink
Browse files

std: getopts now uses result::t (fixes #1289)

  • Loading branch information...
1 parent bd6b80c commit fa27724a4bb213f896d4003ed129bf7caff7f3a0 @boggle boggle committed with brson Dec 16, 2011
View
@@ -11,6 +11,7 @@ import syntax::print::{pp, pprust};
import util::{ppaux, filesearch};
import back::link;
import core::{option, str, vec, int, result};
+import result::{ok, err};
import std::{fs, io, getopts};
import option::{some, none};
import getopts::{optopt, optmulti, optflag, optflagopt, opt_present};
@@ -621,8 +622,8 @@ fn main(args: [str]) {
let args = args, binary = vec::shift(args);
let match =
alt getopts::getopts(args, opts()) {
- getopts::success(m) { m }
- getopts::failure(f) {
+ ok(m) { m }
+ err(f) {
early_error(getopts::fail_str(f))
}
};
@@ -670,7 +671,7 @@ mod test {
fn test_switch_implies_cfg_test() {
let match =
alt getopts::getopts(["--test"], opts()) {
- getopts::success(m) { m }
+ ok(m) { m }
};
let sessopts = build_session_options(match);
let sess = build_session(sessopts);
@@ -684,7 +685,7 @@ mod test {
fn test_switch_implies_cfg_test_unless_cfg_test() {
let match =
alt getopts::getopts(["--test", "--cfg=test"], opts()) {
- getopts::success(m) { m }
+ ok(m) { m }
};
let sessopts = build_session_options(match);
let sess = build_session(sessopts);
@@ -9,6 +9,9 @@ import str;
import vec;
import task;
+import core::result;
+import result::{ok, err};
+
import comm::port;
import comm::chan;
import comm::send;
@@ -42,8 +45,8 @@ fn parse_config(args: [str]) -> config {
let args_ = vec::tail(args);
let match =
alt getopts::getopts(args_, opts) {
- getopts::success(m) { m }
- getopts::failure(f) { fail getopts::fail_str(f) }
+ ok(m) { m }
+ err(f) { fail getopts::fail_str(f) }
};
ret {compile_lib_path: getopts::opt_str(match, "compile-lib-path"),
View
@@ -26,8 +26,8 @@ name following -o, and accepts both -h and --help as optional flags.
> optflag("help")
> ];
> let match = alt getopts(vec::shift(args), opts) {
-> success(m) { m }
-> failure(f) { fail fail_str(f) }
+> ok(m) { m }
+> err(f) { fail fail_str(f) }
> };
> if opt_present(match, "h") || opt_present(match, "help") {
> print_usage();
@@ -45,18 +45,17 @@ name following -o, and accepts both -h and --help as optional flags.
*/
+import core::result;
+import core::result::{err, ok};
import core::option;
-import option::{some, none};
+import core::option::{some, none};
export opt;
export reqopt;
export optopt;
export optflag;
export optflagopt;
export optmulti;
export getopts;
-export result;
-export success;
-export failure;
export match;
export fail_;
export fail_str;
@@ -193,13 +192,14 @@ fn fail_str(f: fail_) -> str {
Type: result
The result of parsing a command line with a set of options
+(result::t<match, fail_>)
Variants:
-success(match) - Returned from getopts on success
-failure(fail_) - Returned from getopts on failure
+ok(match) - Returned from getopts on success
+err(fail_) - Returned from getopts on failure
*/
-tag result { success(match); failure(fail_); }
+type result = result::t<match, fail_>;
/*
Function: getopts
@@ -208,9 +208,9 @@ Parse command line arguments according to the provided options
Returns:
-success(match) - On success. Use functions such as <opt_present>
- <opt_str>, etc. to interrogate results.
-failure(fail_) - On failure. Use <fail_str> to get an error message.
+ok(match) - On success. Use functions such as <opt_present>
+ <opt_str>, etc. to interrogate results.
+err(fail_) - On failure. Use <fail_str> to get an error message.
*/
fn getopts(args: [str], opts: [opt]) -> result {
let n_opts = vec::len::<opt>(opts);
@@ -258,12 +258,12 @@ fn getopts(args: [str], opts: [opt]) -> result {
let optid;
alt find_opt(opts, nm) {
some(id) { optid = id; }
- none. { ret failure(unrecognized_option(name_str(nm))); }
+ none. { ret err(unrecognized_option(name_str(nm))); }
}
alt opts[optid].hasarg {
no. {
if !option::is_none::<str>(i_arg) {
- ret failure(unexpected_argument(name_str(nm)));
+ ret err(unexpected_argument(name_str(nm)));
}
vals[optid] += [given];
}
@@ -279,7 +279,7 @@ fn getopts(args: [str], opts: [opt]) -> result {
if !option::is_none::<str>(i_arg) {
vals[optid] += [val(option::get::<str>(i_arg))];
} else if i + 1u == l {
- ret failure(argument_missing(name_str(nm)));
+ ret err(argument_missing(name_str(nm)));
} else { i += 1u; vals[optid] += [val(args[i])]; }
}
}
@@ -293,17 +293,17 @@ fn getopts(args: [str], opts: [opt]) -> result {
let occ = opts[i].occur;
if occ == req {
if n == 0u {
- ret failure(option_missing(name_str(opts[i].name)));
+ ret err(option_missing(name_str(opts[i].name)));
}
}
if occ != multi {
if n > 1u {
- ret failure(option_duplicated(name_str(opts[i].name)));
+ ret err(option_duplicated(name_str(opts[i].name)));
}
}
i += 1u;
}
- ret success({opts: opts, vals: vals, free: free});
+ ret ok({opts: opts, vals: vals, free: free});
}
fn opt_vals(m: match, nm: str) -> [optval] {
View
@@ -9,6 +9,7 @@ import task::task;
import core::option;
import core::either;
import core::vec;
+import core::result::{ok, err};
export test_name;
export test_fn;
@@ -82,8 +83,8 @@ fn parse_opts(args: [str]) : vec::is_not_empty(args) -> opt_res {
let opts = [getopts::optflag("ignored")];
let match =
alt getopts::getopts(args_, opts) {
- getopts::success(m) { m }
- getopts::failure(f) { ret either::right(getopts::fail_str(f)) }
+ ok(m) { m }
+ err(f) { ret either::right(getopts::fail_str(f)) }
};
let filter =
@@ -27,6 +27,9 @@ import comm::chan;
import comm::send;
import comm::recv;
+import core::result;
+import result::{ok, err};
+
fn fib(n: int) -> int {
fn pfib(args: (chan<int>, int)) {
let (c, n) = args;
@@ -58,8 +61,8 @@ fn parse_opts(argv: [str]) -> config {
alt getopts::getopts(opt_args, opts) {
- getopts::success(m) { ret {stress: getopts::opt_present(m, "stress")} }
- getopts::failure(_) { fail; }
+ ok(m) { ret {stress: getopts::opt_present(m, "stress")} }
+ err(_) { fail; }
}
}
Oops, something went wrong.

0 comments on commit fa27724

Please sign in to comment.