Skip to content

Commit

Permalink
std: getopts now uses result::t (fixes #1289)
Browse files Browse the repository at this point in the history
  • Loading branch information
boggle authored and brson committed Dec 19, 2011
1 parent bd6b80c commit fa27724
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 63 deletions.
9 changes: 5 additions & 4 deletions src/comp/driver/rustc.rs
Expand Up @@ -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};
Expand Down Expand Up @@ -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))
}
};
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
7 changes: 5 additions & 2 deletions src/compiletest/compiletest.rs
Expand Up @@ -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;
Expand Down Expand Up @@ -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"),
Expand Down
36 changes: 18 additions & 18 deletions src/libstd/getopts.rs
Expand Up @@ -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();
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -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];
}
Expand All @@ -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])]; }
}
}
Expand All @@ -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] {
Expand Down
5 changes: 3 additions & 2 deletions src/libstd/test.rs
Expand Up @@ -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;
Expand Down Expand Up @@ -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 =
Expand Down
7 changes: 5 additions & 2 deletions src/test/bench/shootout-pfib.rs
Expand Up @@ -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;
Expand Down Expand Up @@ -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; }
}
}

Expand Down

0 comments on commit fa27724

Please sign in to comment.