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

This commit is contained in:
Stefan Plantikow 2011-12-16 01:11:29 +01:00 committed by Brian Anderson
parent bd6b80c972
commit fa27724a4b
6 changed files with 72 additions and 63 deletions

View file

@ -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);

View file

@ -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 file

@ -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,8 +45,10 @@ 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;
@ -54,9 +56,6 @@ 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 file

@ -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 =

View file

@ -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; }
}
}

View file

@ -2,6 +2,7 @@ import core::*;
use std;
import opt = std::getopts;
import result::{err, ok};
tag fail_type {
argument_missing;
@ -30,7 +31,7 @@ fn test_reqopt_long() {
let opts = [opt::reqopt("test")];
let rs = opt::getopts(args, opts);
alt rs {
opt::success(m) {
ok(m) {
assert (opt::opt_present(m, "test"));
assert (opt::opt_str(m, "test") == "20");
}
@ -44,7 +45,7 @@ fn test_reqopt_long_missing() {
let opts = [opt::reqopt("test")];
let rs = opt::getopts(args, opts);
alt rs {
opt::failure(f) { check_fail_type(f, option_missing); }
err(f) { check_fail_type(f, option_missing); }
_ { fail; }
}
}
@ -55,7 +56,7 @@ fn test_reqopt_long_no_arg() {
let opts = [opt::reqopt("test")];
let rs = opt::getopts(args, opts);
alt rs {
opt::failure(f) { check_fail_type(f, argument_missing); }
err(f) { check_fail_type(f, argument_missing); }
_ { fail; }
}
}
@ -66,7 +67,7 @@ fn test_reqopt_long_multi() {
let opts = [opt::reqopt("test")];
let rs = opt::getopts(args, opts);
alt rs {
opt::failure(f) { check_fail_type(f, option_duplicated); }
err(f) { check_fail_type(f, option_duplicated); }
_ { fail; }
}
}
@ -77,7 +78,7 @@ fn test_reqopt_short() {
let opts = [opt::reqopt("t")];
let rs = opt::getopts(args, opts);
alt rs {
opt::success(m) {
ok(m) {
assert (opt::opt_present(m, "t"));
assert (opt::opt_str(m, "t") == "20");
}
@ -91,7 +92,7 @@ fn test_reqopt_short_missing() {
let opts = [opt::reqopt("t")];
let rs = opt::getopts(args, opts);
alt rs {
opt::failure(f) { check_fail_type(f, option_missing); }
err(f) { check_fail_type(f, option_missing); }
_ { fail; }
}
}
@ -102,7 +103,7 @@ fn test_reqopt_short_no_arg() {
let opts = [opt::reqopt("t")];
let rs = opt::getopts(args, opts);
alt rs {
opt::failure(f) { check_fail_type(f, argument_missing); }
err(f) { check_fail_type(f, argument_missing); }
_ { fail; }
}
}
@ -113,7 +114,7 @@ fn test_reqopt_short_multi() {
let opts = [opt::reqopt("t")];
let rs = opt::getopts(args, opts);
alt rs {
opt::failure(f) { check_fail_type(f, option_duplicated); }
err(f) { check_fail_type(f, option_duplicated); }
_ { fail; }
}
}
@ -126,7 +127,7 @@ fn test_optopt_long() {
let opts = [opt::optopt("test")];
let rs = opt::getopts(args, opts);
alt rs {
opt::success(m) {
ok(m) {
assert (opt::opt_present(m, "test"));
assert (opt::opt_str(m, "test") == "20");
}
@ -140,7 +141,7 @@ fn test_optopt_long_missing() {
let opts = [opt::optopt("test")];
let rs = opt::getopts(args, opts);
alt rs {
opt::success(m) { assert (!opt::opt_present(m, "test")); }
ok(m) { assert (!opt::opt_present(m, "test")); }
_ { fail; }
}
}
@ -151,7 +152,7 @@ fn test_optopt_long_no_arg() {
let opts = [opt::optopt("test")];
let rs = opt::getopts(args, opts);
alt rs {
opt::failure(f) { check_fail_type(f, argument_missing); }
err(f) { check_fail_type(f, argument_missing); }
_ { fail; }
}
}
@ -162,7 +163,7 @@ fn test_optopt_long_multi() {
let opts = [opt::optopt("test")];
let rs = opt::getopts(args, opts);
alt rs {
opt::failure(f) { check_fail_type(f, option_duplicated); }
err(f) { check_fail_type(f, option_duplicated); }
_ { fail; }
}
}
@ -173,7 +174,7 @@ fn test_optopt_short() {
let opts = [opt::optopt("t")];
let rs = opt::getopts(args, opts);
alt rs {
opt::success(m) {
ok(m) {
assert (opt::opt_present(m, "t"));
assert (opt::opt_str(m, "t") == "20");
}
@ -187,7 +188,7 @@ fn test_optopt_short_missing() {
let opts = [opt::optopt("t")];
let rs = opt::getopts(args, opts);
alt rs {
opt::success(m) { assert (!opt::opt_present(m, "t")); }
ok(m) { assert (!opt::opt_present(m, "t")); }
_ { fail; }
}
}
@ -198,7 +199,7 @@ fn test_optopt_short_no_arg() {
let opts = [opt::optopt("t")];
let rs = opt::getopts(args, opts);
alt rs {
opt::failure(f) { check_fail_type(f, argument_missing); }
err(f) { check_fail_type(f, argument_missing); }
_ { fail; }
}
}
@ -209,7 +210,7 @@ fn test_optopt_short_multi() {
let opts = [opt::optopt("t")];
let rs = opt::getopts(args, opts);
alt rs {
opt::failure(f) { check_fail_type(f, option_duplicated); }
err(f) { check_fail_type(f, option_duplicated); }
_ { fail; }
}
}
@ -222,7 +223,7 @@ fn test_optflag_long() {
let opts = [opt::optflag("test")];
let rs = opt::getopts(args, opts);
alt rs {
opt::success(m) { assert (opt::opt_present(m, "test")); }
ok(m) { assert (opt::opt_present(m, "test")); }
_ { fail; }
}
}
@ -233,7 +234,7 @@ fn test_optflag_long_missing() {
let opts = [opt::optflag("test")];
let rs = opt::getopts(args, opts);
alt rs {
opt::success(m) { assert (!opt::opt_present(m, "test")); }
ok(m) { assert (!opt::opt_present(m, "test")); }
_ { fail; }
}
}
@ -244,7 +245,7 @@ fn test_optflag_long_arg() {
let opts = [opt::optflag("test")];
let rs = opt::getopts(args, opts);
alt rs {
opt::failure(f) {
err(f) {
log_err opt::fail_str(f);
check_fail_type(f, unexpected_argument);
}
@ -258,7 +259,7 @@ fn test_optflag_long_multi() {
let opts = [opt::optflag("test")];
let rs = opt::getopts(args, opts);
alt rs {
opt::failure(f) { check_fail_type(f, option_duplicated); }
err(f) { check_fail_type(f, option_duplicated); }
_ { fail; }
}
}
@ -269,7 +270,7 @@ fn test_optflag_short() {
let opts = [opt::optflag("t")];
let rs = opt::getopts(args, opts);
alt rs {
opt::success(m) { assert (opt::opt_present(m, "t")); }
ok(m) { assert (opt::opt_present(m, "t")); }
_ { fail; }
}
}
@ -280,7 +281,7 @@ fn test_optflag_short_missing() {
let opts = [opt::optflag("t")];
let rs = opt::getopts(args, opts);
alt rs {
opt::success(m) { assert (!opt::opt_present(m, "t")); }
ok(m) { assert (!opt::opt_present(m, "t")); }
_ { fail; }
}
}
@ -291,7 +292,7 @@ fn test_optflag_short_arg() {
let opts = [opt::optflag("t")];
let rs = opt::getopts(args, opts);
alt rs {
opt::success(m) {
ok(m) {
// The next variable after the flag is just a free argument
assert (m.free[0] == "20");
@ -306,7 +307,7 @@ fn test_optflag_short_multi() {
let opts = [opt::optflag("t")];
let rs = opt::getopts(args, opts);
alt rs {
opt::failure(f) { check_fail_type(f, option_duplicated); }
err(f) { check_fail_type(f, option_duplicated); }
_ { fail; }
}
}
@ -319,7 +320,7 @@ fn test_optmulti_long() {
let opts = [opt::optmulti("test")];
let rs = opt::getopts(args, opts);
alt rs {
opt::success(m) {
ok(m) {
assert (opt::opt_present(m, "test"));
assert (opt::opt_str(m, "test") == "20");
}
@ -333,7 +334,7 @@ fn test_optmulti_long_missing() {
let opts = [opt::optmulti("test")];
let rs = opt::getopts(args, opts);
alt rs {
opt::success(m) { assert (!opt::opt_present(m, "test")); }
ok(m) { assert (!opt::opt_present(m, "test")); }
_ { fail; }
}
}
@ -344,7 +345,7 @@ fn test_optmulti_long_no_arg() {
let opts = [opt::optmulti("test")];
let rs = opt::getopts(args, opts);
alt rs {
opt::failure(f) { check_fail_type(f, argument_missing); }
err(f) { check_fail_type(f, argument_missing); }
_ { fail; }
}
}
@ -355,7 +356,7 @@ fn test_optmulti_long_multi() {
let opts = [opt::optmulti("test")];
let rs = opt::getopts(args, opts);
alt rs {
opt::success(m) {
ok(m) {
assert (opt::opt_present(m, "test"));
assert (opt::opt_str(m, "test") == "20");
assert (opt::opt_strs(m, "test")[0] == "20");
@ -371,7 +372,7 @@ fn test_optmulti_short() {
let opts = [opt::optmulti("t")];
let rs = opt::getopts(args, opts);
alt rs {
opt::success(m) {
ok(m) {
assert (opt::opt_present(m, "t"));
assert (opt::opt_str(m, "t") == "20");
}
@ -385,7 +386,7 @@ fn test_optmulti_short_missing() {
let opts = [opt::optmulti("t")];
let rs = opt::getopts(args, opts);
alt rs {
opt::success(m) { assert (!opt::opt_present(m, "t")); }
ok(m) { assert (!opt::opt_present(m, "t")); }
_ { fail; }
}
}
@ -396,7 +397,7 @@ fn test_optmulti_short_no_arg() {
let opts = [opt::optmulti("t")];
let rs = opt::getopts(args, opts);
alt rs {
opt::failure(f) { check_fail_type(f, argument_missing); }
err(f) { check_fail_type(f, argument_missing); }
_ { fail; }
}
}
@ -407,7 +408,7 @@ fn test_optmulti_short_multi() {
let opts = [opt::optmulti("t")];
let rs = opt::getopts(args, opts);
alt rs {
opt::success(m) {
ok(m) {
assert (opt::opt_present(m, "t"));
assert (opt::opt_str(m, "t") == "20");
assert (opt::opt_strs(m, "t")[0] == "20");
@ -423,7 +424,7 @@ fn test_unrecognized_option_long() {
let opts = [opt::optmulti("t")];
let rs = opt::getopts(args, opts);
alt rs {
opt::failure(f) { check_fail_type(f, unrecognized_option); }
err(f) { check_fail_type(f, unrecognized_option); }
_ { fail; }
}
}
@ -434,7 +435,7 @@ fn test_unrecognized_option_short() {
let opts = [opt::optmulti("test")];
let rs = opt::getopts(args, opts);
alt rs {
opt::failure(f) { check_fail_type(f, unrecognized_option); }
err(f) { check_fail_type(f, unrecognized_option); }
_ { fail; }
}
}
@ -449,7 +450,7 @@ fn test_combined() {
opt::optflag("f"), opt::optmulti("m"), opt::optopt("notpresent")];
let rs = opt::getopts(args, opts);
alt rs {
opt::success(m) {
ok(m) {
assert (m.free[0] == "prog");
assert (m.free[1] == "free1");
assert (opt::opt_str(m, "s") == "20");