Convert std::getopts to istrs. Issue #855

This commit is contained in:
Brian Anderson 2011-08-24 18:13:27 -07:00
parent 3a5f4e7a74
commit 4cf2e510e0
7 changed files with 287 additions and 259 deletions

View File

@ -330,45 +330,47 @@ fn build_target_config() -> @session::config {
ret target_cfg;
}
fn build_session_options(binary: str, match: getopts::match, binary_dir: str)
fn build_session_options(binary: str, match: &getopts::match, binary_dir: str)
-> @session::options {
let library = opt_present(match, "lib");
let static = opt_present(match, "static");
let library = opt_present(match, ~"lib");
let static = opt_present(match, ~"static");
let library_search_paths = [binary_dir + "/lib"];
let lsp_vec = getopts::opt_strs(match, "L");
for lsp: str in lsp_vec { library_search_paths += [lsp]; }
let lsp_vec = getopts::opt_strs(match, ~"L");
for lsp: istr in lsp_vec {
library_search_paths += [istr::to_estr(lsp)];
}
let parse_only = opt_present(match, "parse-only");
let no_trans = opt_present(match, "no-trans");
let parse_only = opt_present(match, ~"parse-only");
let no_trans = opt_present(match, ~"no-trans");
let output_type =
if parse_only || no_trans {
link::output_type_none
} else if opt_present(match, "S") {
} else if opt_present(match, ~"S") {
link::output_type_assembly
} else if opt_present(match, "c") {
} else if opt_present(match, ~"c") {
link::output_type_object
} else if opt_present(match, "emit-llvm") {
} else if opt_present(match, ~"emit-llvm") {
link::output_type_bitcode
} else { link::output_type_exe };
let verify = !opt_present(match, "noverify");
let save_temps = opt_present(match, "save-temps");
let debuginfo = opt_present(match, "g");
let stats = opt_present(match, "stats");
let time_passes = opt_present(match, "time-passes");
let time_llvm_passes = opt_present(match, "time-llvm-passes");
let run_typestate = !opt_present(match, "no-typestate");
let sysroot_opt = getopts::opt_maybe_str(match, "sysroot");
let verify = !opt_present(match, ~"noverify");
let save_temps = opt_present(match, ~"save-temps");
let debuginfo = opt_present(match, ~"g");
let stats = opt_present(match, ~"stats");
let time_passes = opt_present(match, ~"time-passes");
let time_llvm_passes = opt_present(match, ~"time-llvm-passes");
let run_typestate = !opt_present(match, ~"no-typestate");
let sysroot_opt = getopts::opt_maybe_str(match, ~"sysroot");
let opt_level: uint =
if opt_present(match, "O") {
if opt_present(match, "OptLevel") {
if opt_present(match, ~"O") {
if opt_present(match, ~"OptLevel") {
log_err "error: -O and --OptLevel both provided";
fail;
}
2u
} else if opt_present(match, "OptLevel") {
alt getopts::opt_str(match, "OptLevel") {
} else if opt_present(match, ~"OptLevel") {
alt istr::to_estr(getopts::opt_str(match, ~"OptLevel")) {
"0" { 0u }
"1" { 1u }
"2" { 2u }
@ -383,11 +385,12 @@ fn build_session_options(binary: str, match: getopts::match, binary_dir: str)
let sysroot =
alt sysroot_opt {
none. { get_default_sysroot(binary) }
some(s) { s }
some(s) { istr::to_estr(s) }
};
let cfg = parse_cfgspecs(getopts::opt_strs(match, "cfg"));
let test = opt_present(match, "test");
let do_gc = opt_present(match, "gc");
let cfg = parse_cfgspecs(
istr::to_estrs(getopts::opt_strs(match, ~"cfg")));
let test = opt_present(match, ~"test");
let do_gc = opt_present(match, ~"gc");
let sopts: @session::options =
@{library: library,
static: static,
@ -418,28 +421,29 @@ fn build_session(sopts: @session::options) -> session::session {
none, 0u);
}
fn parse_pretty(sess: session::session, name: &str) -> pp_mode {
if str::eq(name, "normal") {
fn parse_pretty(sess: session::session, name: &istr) -> pp_mode {
if istr::eq(name, ~"normal") {
ret ppm_normal;
} else if str::eq(name, "expanded") {
} else if istr::eq(name, ~"expanded") {
ret ppm_expanded;
} else if str::eq(name, "typed") {
} else if istr::eq(name, ~"typed") {
ret ppm_typed;
} else if str::eq(name, "identified") { ret ppm_identified; }
} else if istr::eq(name, ~"identified") { ret ppm_identified; }
sess.fatal("argument to `pretty` must be one of `normal`, `typed`, or "
+ "`identified`");
}
fn opts() -> [getopts::opt] {
ret [optflag("h"), optflag("help"), optflag("v"), optflag("version"),
optflag("glue"), optflag("emit-llvm"), optflagopt("pretty"),
optflag("ls"), optflag("parse-only"), optflag("no-trans"),
optflag("O"), optopt("OptLevel"), optmulti("L"),
optflag("S"), optflag("c"), optopt("o"), optflag("g"),
optflag("save-temps"), optopt("sysroot"), optflag("stats"),
optflag("time-passes"), optflag("time-llvm-passes"),
optflag("no-typestate"), optflag("noverify"), optmulti("cfg"),
optflag("test"), optflag("lib"), optflag("static"), optflag("gc")];
ret [optflag(~"h"), optflag(~"help"), optflag(~"v"), optflag(~"version"),
optflag(~"glue"), optflag(~"emit-llvm"), optflagopt(~"pretty"),
optflag(~"ls"), optflag(~"parse-only"), optflag(~"no-trans"),
optflag(~"O"), optopt(~"OptLevel"), optmulti(~"L"),
optflag(~"S"), optflag(~"c"), optopt(~"o"), optflag(~"g"),
optflag(~"save-temps"), optopt(~"sysroot"), optflag(~"stats"),
optflag(~"time-passes"), optflag(~"time-llvm-passes"),
optflag(~"no-typestate"), optflag(~"noverify"), optmulti(~"cfg"),
optflag(~"test"), optflag(~"lib"), optflag(~"static"),
optflag(~"gc")];
}
fn main(args: [str]) {
@ -447,31 +451,32 @@ fn main(args: [str]) {
let binary_dir = istr::to_estr(
fs::dirname(istr::from_estr(binary)));
let match =
alt getopts::getopts(args, opts()) {
alt getopts::getopts(istr::from_estrs(args), opts()) {
getopts::success(m) { m }
getopts::failure(f) {
log_err #fmt["error: %s", getopts::fail_str(f)];
log_err #fmt["error: %s", istr::to_estr(getopts::fail_str(f))];
fail
}
};
if opt_present(match, "h") || opt_present(match, "help") {
if opt_present(match, ~"h") || opt_present(match, ~"help") {
usage(binary);
ret;
}
if opt_present(match, "v") || opt_present(match, "version") {
if opt_present(match, ~"v") || opt_present(match, ~"version") {
version(binary);
ret;
}
let sopts = build_session_options(binary, match, binary_dir);
let sess = build_session(sopts);
let n_inputs = vec::len::<str>(match.free);
let output_file = getopts::opt_maybe_str(match, "o");
let glue = opt_present(match, "glue");
let n_inputs = vec::len::<istr>(match.free);
let output_file = getopts::opt_maybe_str(match, ~"o");
let glue = opt_present(match, ~"glue");
if glue {
if n_inputs > 0u {
sess.fatal("No input files allowed with --glue.");
}
let out = option::from_maybe::<str>("glue.bc", output_file);
let out = option::from_maybe::<istr>(~"glue.bc", output_file);
let out = istr::to_estr(out);
middle::trans::make_common_glue(sess, out);
ret;
}
@ -480,14 +485,14 @@ fn main(args: [str]) {
} else if n_inputs > 1u {
sess.fatal("Multiple input filenames provided.");
}
let ifile = match.free[0];
let ifile = istr::to_estr(match.free[0]);
let saved_out_filename: str = "";
let cfg = build_configuration(sess, binary, ifile);
let pretty =
option::map::<str,
option::map::<istr,
pp_mode>(bind parse_pretty(sess, _),
getopts::opt_default(match, "pretty",
"normal"));
getopts::opt_default(match, ~"pretty",
~"normal"));
alt pretty {
some::<pp_mode>(ppm) {
pretty_print_input(sess, cfg, ifile, ppm);
@ -495,7 +500,7 @@ fn main(args: [str]) {
}
none::<pp_mode>. {/* continue */ }
}
let ls = opt_present(match, "ls");
let ls = opt_present(match, ~"ls");
if ls { metadata::creader::list_file_metadata(ifile, io::stdout()); ret; }
let stop_after_codegen =
@ -528,6 +533,7 @@ fn main(args: [str]) {
compile_input(sess, cfg, ifile, ofile);
}
some(ofile) {
let ofile = istr::to_estr(ofile);
// FIXME: what about windows? This will create a foo.exe.o.
saved_out_filename = ofile;
let temp_filename =
@ -634,7 +640,7 @@ mod test {
#[test]
fn test_switch_implies_cfg_test() {
let match =
alt getopts::getopts(["--test"], opts()) {
alt getopts::getopts([~"--test"], opts()) {
getopts::success(m) { m }
};
let sessopts = build_session_options("whatever", match, "whatever");
@ -648,7 +654,7 @@ mod test {
#[test]
fn test_switch_implies_cfg_test_unless_cfg_test() {
let match =
alt getopts::getopts(["--test", "--cfg=test"], opts()) {
alt getopts::getopts([~"--test", ~"--cfg=test"], opts()) {
getopts::success(m) { m }
};
let sessopts = build_session_options("whatever", match, "whatever");

View File

@ -29,7 +29,7 @@ export opt_strs;
export opt_maybe_str;
export opt_default;
tag name { long(str); short(char); }
tag name { long(istr); short(char); }
tag hasarg { yes; no; maybe; }
@ -37,45 +37,45 @@ tag occur { req; optional; multi; }
type opt = {name: name, hasarg: hasarg, occur: occur};
fn mkname(nm: str) -> name {
ret if str::char_len(nm) == 1u {
short(str::char_at(nm, 0u))
fn mkname(nm: &istr) -> name {
ret if istr::char_len(nm) == 1u {
short(istr::char_at(nm, 0u))
} else { long(nm) };
}
fn reqopt(name: str) -> opt {
fn reqopt(name: &istr) -> opt {
ret {name: mkname(name), hasarg: yes, occur: req};
}
fn optopt(name: str) -> opt {
fn optopt(name: &istr) -> opt {
ret {name: mkname(name), hasarg: yes, occur: optional};
}
fn optflag(name: str) -> opt {
fn optflag(name: &istr) -> opt {
ret {name: mkname(name), hasarg: no, occur: optional};
}
fn optflagopt(name: str) -> opt {
fn optflagopt(name: &istr) -> opt {
ret {name: mkname(name), hasarg: maybe, occur: optional};
}
fn optmulti(name: str) -> opt {
fn optmulti(name: &istr) -> opt {
ret {name: mkname(name), hasarg: yes, occur: multi};
}
tag optval { val(str); given; }
tag optval { val(istr); given; }
type match = {opts: [opt], vals: [mutable [optval]], free: [str]};
type match = {opts: [opt], vals: [mutable [optval]], free: [istr]};
fn is_arg(arg: str) -> bool {
ret str::byte_len(arg) > 1u && arg[0] == '-' as u8;
fn is_arg(arg: &istr) -> bool {
ret istr::byte_len(arg) > 1u && arg[0] == '-' as u8;
}
fn name_str(nm: name) -> str {
ret alt nm { short(ch) { str::from_char(ch) } long(s) { s } };
fn name_str(nm: &name) -> istr {
ret alt nm { short(ch) { istr::from_char(ch) } long(s) { s } };
}
fn find_opt(opts: &[opt], nm: name) -> option::t<uint> {
fn find_opt(opts: &[opt], nm: &name) -> option::t<uint> {
let i = 0u;
let l = vec::len::<opt>(opts);
while i < l { if opts[i].name == nm { ret some::<uint>(i); } i += 1u; }
@ -83,57 +83,59 @@ fn find_opt(opts: &[opt], nm: name) -> option::t<uint> {
}
tag fail_ {
argument_missing(str);
unrecognized_option(str);
option_missing(str);
option_duplicated(str);
unexpected_argument(str);
argument_missing(istr);
unrecognized_option(istr);
option_missing(istr);
option_duplicated(istr);
unexpected_argument(istr);
}
fn fail_str(f: fail_) -> str {
fn fail_str(f: &fail_) -> istr {
ret alt f {
argument_missing(nm) { "Argument to option '" + nm + "' missing." }
unrecognized_option(nm) { "Unrecognized option: '" + nm + "'." }
option_missing(nm) { "Required option '" + nm + "' missing." }
argument_missing(nm) {
~"Argument to option '" + nm + ~"' missing." }
unrecognized_option(nm) {
~"Unrecognized option: '" + nm + ~"'." }
option_missing(nm) { ~"Required option '" + nm + ~"' missing." }
option_duplicated(nm) {
"Option '" + nm + "' given more than once."
~"Option '" + nm + ~"' given more than once."
}
unexpected_argument(nm) {
"Option " + nm + " does not take an argument."
~"Option " + nm + ~" does not take an argument."
}
};
}
tag result { success(match); failure(fail_); }
fn getopts(args: &[str], opts: &[opt]) -> result {
fn getopts(args: &[istr], opts: &[opt]) -> result {
let n_opts = vec::len::<opt>(opts);
fn f(_x: uint) -> [optval] { ret []; }
let vals = vec::init_fn_mut::<[optval]>(f, n_opts);
let free: [str] = [];
let l = vec::len::<str>(args);
let free: [istr] = [];
let l = vec::len(args);
let i = 0u;
while i < l {
let cur = args[i];
let curlen = str::byte_len(cur);
let curlen = istr::byte_len(cur);
if !is_arg(cur) {
free += [cur];
} else if str::eq(cur, "--") {
} else if istr::eq(cur, ~"--") {
let j = i + 1u;
while j < l { free += [args[j]]; j += 1u; }
break;
} else {
let names;
let i_arg = option::none::<str>;
let i_arg = option::none::<istr>;
if cur[1] == '-' as u8 {
let tail = str::slice(cur, 2u, curlen);
let eq = str::index(tail, '=' as u8);
let tail = istr::slice(cur, 2u, curlen);
let eq = istr::index(tail, '=' as u8);
if eq == -1 {
names = [long(tail)];
} else {
names = [long(str::slice(tail, 0u, eq as uint))];
names = [long(istr::slice(tail, 0u, eq as uint))];
i_arg =
option::some::<str>(str::slice(tail,
option::some::<istr>(istr::slice(tail,
(eq as uint) + 1u,
curlen - 2u));
}
@ -141,7 +143,7 @@ fn getopts(args: &[str], opts: &[opt]) -> result {
let j = 1u;
names = [];
while j < curlen {
let range = str::char_range_at(cur, j);
let range = istr::char_range_at(cur, j);
names += [short(range.ch)];
j = range.next;
}
@ -156,13 +158,13 @@ fn getopts(args: &[str], opts: &[opt]) -> result {
}
alt opts[optid].hasarg {
no. {
if !option::is_none::<str>(i_arg) {
if !option::is_none::<istr>(i_arg) {
ret failure(unexpected_argument(name_str(nm)));
}
vals[optid] += [given];
}
maybe. {
if !option::is_none::<str>(i_arg) {
if !option::is_none::<istr>(i_arg) {
vals[optid] += [val(option::get(i_arg))];
} else if name_pos < vec::len::<name>(names) ||
i + 1u == l || is_arg(args[i + 1u]) {
@ -170,8 +172,8 @@ fn getopts(args: &[str], opts: &[opt]) -> result {
} else { i += 1u; vals[optid] += [val(args[i])]; }
}
yes. {
if !option::is_none::<str>(i_arg) {
vals[optid] += [val(option::get::<str>(i_arg))];
if !option::is_none::<istr>(i_arg) {
vals[optid] += [val(option::get::<istr>(i_arg))];
} else if i + 1u == l {
ret failure(argument_missing(name_str(nm)));
} else { i += 1u; vals[optid] += [val(args[i])]; }
@ -200,45 +202,45 @@ fn getopts(args: &[str], opts: &[opt]) -> result {
ret success({opts: opts, vals: vals, free: free});
}
fn opt_vals(m: &match, nm: str) -> [optval] {
fn opt_vals(m: &match, nm: &istr) -> [optval] {
ret alt find_opt(m.opts, mkname(nm)) {
some(id) { m.vals[id] }
none. { log_err "No option '" + nm + "' defined."; fail }
none. { log_err ~"No option '" + nm + ~"' defined."; fail }
};
}
fn opt_val(m: &match, nm: str) -> optval { ret opt_vals(m, nm)[0]; }
fn opt_val(m: &match, nm: &istr) -> optval { ret opt_vals(m, nm)[0]; }
fn opt_present(m: &match, nm: str) -> bool {
fn opt_present(m: &match, nm: &istr) -> bool {
ret vec::len::<optval>(opt_vals(m, nm)) > 0u;
}
fn opt_str(m: &match, nm: str) -> str {
fn opt_str(m: &match, nm: &istr) -> istr {
ret alt opt_val(m, nm) { val(s) { s } _ { fail } };
}
fn opt_strs(m: &match, nm: str) -> [str] {
let acc: [str] = [];
fn opt_strs(m: &match, nm: &istr) -> [istr] {
let acc: [istr] = [];
for v: optval in opt_vals(m, nm) {
alt v { val(s) { acc += [s]; } _ { } }
}
ret acc;
}
fn opt_maybe_str(m: &match, nm: str) -> option::t<str> {
fn opt_maybe_str(m: &match, nm: &istr) -> option::t<istr> {
let vals = opt_vals(m, nm);
if vec::len::<optval>(vals) == 0u { ret none::<str>; }
ret alt vals[0] { val(s) { some::<str>(s) } _ { none::<str> } };
if vec::len::<optval>(vals) == 0u { ret none::<istr>; }
ret alt vals[0] { val(s) { some::<istr>(s) } _ { none::<istr> } };
}
/// Returns none if the option was not present, `def` if the option was
/// present but no argument was provided, and the argument if the option was
/// present and an argument was provided.
fn opt_default(m: &match, nm: str, def: str) -> option::t<str> {
fn opt_default(m: &match, nm: &istr, def: &istr) -> option::t<istr> {
let vals = opt_vals(m, nm);
if vec::len::<optval>(vals) == 0u { ret none::<str>; }
ret alt vals[0] { val(s) { some::<str>(s) } _ { some::<str>(def) } }
if vec::len::<optval>(vals) == 0u { ret none::<istr>; }
ret alt vals[0] { val(s) { some::<istr>(s) } _ { some::<istr>(def) } }
}
// Local Variables:
// mode: rust;

View File

@ -3,9 +3,9 @@ index, rindex, find, starts_with, ends_with, substr, slice, split,
concat, connect, to_upper, replace, char_slice, trim_left, trim_right, trim,
unshift_char, shift_char, pop_char, push_char, is_utf8, from_chars, to_chars,
char_len, char_at, bytes, is_ascii, shift_byte, pop_byte, unsafe_from_byte,
unsafe_from_bytes, from_char;
unsafe_from_bytes, from_char, char_range_at;
export from_estr, to_estr;
export from_estr, to_estr, from_estrs, to_estrs;
fn from_estr(s: &str) -> istr {
let s2 = ~"";
@ -23,6 +23,22 @@ fn to_estr(s: &istr) -> str {
ret s2;
}
fn from_estrs(ss: &[str]) -> [istr] {
let ss2 = [];
for s in ss {
ss2 += [from_estr(s)];
}
ret ss2;
}
fn to_estrs(ss: &[istr]) -> [str] {
let ss2 = [];
for s in ss {
ss2 += [to_estr(s)];
}
ret ss2;
}
fn eq(a: &istr, b: &istr) -> bool { a == b }
fn lteq(a: &istr, b: &istr) -> bool { a <= b }

View File

@ -66,20 +66,22 @@ type opt_res = either::t<test_opts, str>;
// Parses command line arguments into test options
fn parse_opts(args: &[str]) : vec::is_not_empty(args) -> opt_res {
let args_ = vec::tail(args);
let opts = [getopts::optflag("ignored")];
let args_ = istr::from_estrs(vec::tail(args));
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)) }
getopts::failure(f) {
ret either::right(istr::to_estr(getopts::fail_str(f)))
}
};
let filter =
if vec::len(match.free) > 0u {
option::some(match.free[0])
option::some(istr::to_estr(match.free[0]))
} else { option::none };
let run_ignored = getopts::opt_present(match, "ignored");
let run_ignored = getopts::opt_present(match, ~"ignored");
let test_opts = {filter: filter, run_ignored: run_ignored};

View File

@ -51,13 +51,14 @@ fn fib(n: int) -> int {
type config = {stress: bool};
fn parse_opts(argv: [str]) -> config {
let opts = [getopts::optflag("stress")];
let argv = istr::from_estrs(argv);
let opts = [getopts::optflag(~"stress")];
let opt_args = vec::slice(argv, 1u, vec::len(argv));
alt getopts::getopts(opt_args, opts) {
getopts::success(m) { ret {stress: getopts::opt_present(m, "stress")} }
getopts::success(m) { ret {stress: getopts::opt_present(m, ~"stress")} }
getopts::failure(_) { fail; }
}
}

View File

@ -30,45 +30,45 @@ fn main(args: [str]) {
}
fn parse_config(args: &[str]) -> config {
let args = istr::from_estrs(args);
let opts =
[getopts::reqopt("compile-lib-path"), getopts::reqopt("run-lib-path"),
getopts::reqopt("rustc-path"), getopts::reqopt("src-base"),
getopts::reqopt("build-base"), getopts::reqopt("stage-id"),
getopts::reqopt("mode"), getopts::optflag("ignored"),
getopts::optopt("runtool"), getopts::optopt("rustcflags"),
getopts::optflag("verbose")];
[getopts::reqopt(~"compile-lib-path"),
getopts::reqopt(~"run-lib-path"),
getopts::reqopt(~"rustc-path"),
getopts::reqopt(~"src-base"),
getopts::reqopt(~"build-base"),
getopts::reqopt(~"stage-id"),
getopts::reqopt(~"mode"),
getopts::optflag(~"ignored"),
getopts::optopt(~"runtool"),
getopts::optopt(~"rustcflags"),
getopts::optflag(~"verbose")];
check (vec::is_not_empty(args));
let args_ = vec::tail(args);
let match =
alt getopts::getopts(args_, opts) {
getopts::success(m) { m }
getopts::failure(f) { fail getopts::fail_str(f) }
};
let cnv = istr::from_estr;
let cnvo = fn(o: &option::t<str>) -> option::t<istr> {
alt o {
option::some(s) { option::some(istr::from_estr(s)) }
option::none. { option::none }
getopts::failure(f) {
fail istr::to_estr(getopts::fail_str(f))
}
};
ret {compile_lib_path: cnv(getopts::opt_str(match, "compile-lib-path")),
run_lib_path: cnv(getopts::opt_str(match, "run-lib-path")),
rustc_path: cnv(getopts::opt_str(match, "rustc-path")),
src_base: cnv(getopts::opt_str(match, "src-base")),
build_base: cnv(getopts::opt_str(match, "build-base")),
stage_id: cnv(getopts::opt_str(match, "stage-id")),
mode: str_mode(getopts::opt_str(match, "mode")),
run_ignored: getopts::opt_present(match, "ignored"),
ret {compile_lib_path: getopts::opt_str(match, ~"compile-lib-path"),
run_lib_path: getopts::opt_str(match, ~"run-lib-path"),
rustc_path: getopts::opt_str(match, ~"rustc-path"),
src_base: getopts::opt_str(match, ~"src-base"),
build_base: getopts::opt_str(match, ~"build-base"),
stage_id: getopts::opt_str(match, ~"stage-id"),
mode: str_mode(istr::to_estr(getopts::opt_str(match, ~"mode"))),
run_ignored: getopts::opt_present(match, ~"ignored"),
filter:
if vec::len(match.free) > 0u {
option::some(cnv(match.free[0]))
option::some(match.free[0])
} else { option::none },
runtool: cnvo(getopts::opt_maybe_str(match, "runtool")),
rustcflags: cnvo(getopts::opt_maybe_str(match, "rustcflags")),
verbose: getopts::opt_present(match, "verbose")};
runtool: getopts::opt_maybe_str(match, ~"runtool"),
rustcflags: getopts::opt_maybe_str(match, ~"rustcflags"),
verbose: getopts::opt_present(match, ~"verbose")};
}
fn log_config(config: &config) {

View File

@ -27,13 +27,13 @@ fn check_fail_type(f: opt::fail_, ft: fail_type) {
// Tests for reqopt
#[test]
fn test_reqopt_long() {
let args = ["--test=20"];
let opts = [opt::reqopt("test")];
let args = [~"--test=20"];
let opts = [opt::reqopt(~"test")];
let rs = opt::getopts(args, opts);
alt rs {
opt::success(m) {
assert (opt::opt_present(m, "test"));
assert (opt::opt_str(m, "test") == "20");
assert (opt::opt_present(m, ~"test"));
assert (opt::opt_str(m, ~"test") == ~"20");
}
_ { fail; }
}
@ -41,8 +41,8 @@ fn test_reqopt_long() {
#[test]
fn test_reqopt_long_missing() {
let args = ["blah"];
let opts = [opt::reqopt("test")];
let args = [~"blah"];
let opts = [opt::reqopt(~"test")];
let rs = opt::getopts(args, opts);
alt rs {
opt::failure(f) { check_fail_type(f, option_missing); }
@ -52,8 +52,8 @@ fn test_reqopt_long_missing() {
#[test]
fn test_reqopt_long_no_arg() {
let args = ["--test"];
let opts = [opt::reqopt("test")];
let args = [~"--test"];
let opts = [opt::reqopt(~"test")];
let rs = opt::getopts(args, opts);
alt rs {
opt::failure(f) { check_fail_type(f, argument_missing); }
@ -63,8 +63,8 @@ fn test_reqopt_long_no_arg() {
#[test]
fn test_reqopt_long_multi() {
let args = ["--test=20", "--test=30"];
let opts = [opt::reqopt("test")];
let args = [~"--test=20", ~"--test=30"];
let opts = [opt::reqopt(~"test")];
let rs = opt::getopts(args, opts);
alt rs {
opt::failure(f) { check_fail_type(f, option_duplicated); }
@ -74,13 +74,13 @@ fn test_reqopt_long_multi() {
#[test]
fn test_reqopt_short() {
let args = ["-t", "20"];
let opts = [opt::reqopt("t")];
let args = [~"-t", ~"20"];
let opts = [opt::reqopt(~"t")];
let rs = opt::getopts(args, opts);
alt rs {
opt::success(m) {
assert (opt::opt_present(m, "t"));
assert (opt::opt_str(m, "t") == "20");
assert (opt::opt_present(m, ~"t"));
assert (opt::opt_str(m, ~"t") == ~"20");
}
_ { fail; }
}
@ -88,8 +88,8 @@ fn test_reqopt_short() {
#[test]
fn test_reqopt_short_missing() {
let args = ["blah"];
let opts = [opt::reqopt("t")];
let args = [~"blah"];
let opts = [opt::reqopt(~"t")];
let rs = opt::getopts(args, opts);
alt rs {
opt::failure(f) { check_fail_type(f, option_missing); }
@ -99,8 +99,8 @@ fn test_reqopt_short_missing() {
#[test]
fn test_reqopt_short_no_arg() {
let args = ["-t"];
let opts = [opt::reqopt("t")];
let args = [~"-t"];
let opts = [opt::reqopt(~"t")];
let rs = opt::getopts(args, opts);
alt rs {
opt::failure(f) { check_fail_type(f, argument_missing); }
@ -110,8 +110,8 @@ fn test_reqopt_short_no_arg() {
#[test]
fn test_reqopt_short_multi() {
let args = ["-t", "20", "-t", "30"];
let opts = [opt::reqopt("t")];
let args = [~"-t", ~"20", ~"-t", ~"30"];
let opts = [opt::reqopt(~"t")];
let rs = opt::getopts(args, opts);
alt rs {
opt::failure(f) { check_fail_type(f, option_duplicated); }
@ -123,13 +123,13 @@ fn test_reqopt_short_multi() {
// Tests for optopt
#[test]
fn test_optopt_long() {
let args = ["--test=20"];
let opts = [opt::optopt("test")];
let args = [~"--test=20"];
let opts = [opt::optopt(~"test")];
let rs = opt::getopts(args, opts);
alt rs {
opt::success(m) {
assert (opt::opt_present(m, "test"));
assert (opt::opt_str(m, "test") == "20");
assert (opt::opt_present(m, ~"test"));
assert (opt::opt_str(m, ~"test") == ~"20");
}
_ { fail; }
}
@ -137,19 +137,19 @@ fn test_optopt_long() {
#[test]
fn test_optopt_long_missing() {
let args = ["blah"];
let opts = [opt::optopt("test")];
let args = [~"blah"];
let opts = [opt::optopt(~"test")];
let rs = opt::getopts(args, opts);
alt rs {
opt::success(m) { assert (!opt::opt_present(m, "test")); }
opt::success(m) { assert (!opt::opt_present(m, ~"test")); }
_ { fail; }
}
}
#[test]
fn test_optopt_long_no_arg() {
let args = ["--test"];
let opts = [opt::optopt("test")];
let args = [~"--test"];
let opts = [opt::optopt(~"test")];
let rs = opt::getopts(args, opts);
alt rs {
opt::failure(f) { check_fail_type(f, argument_missing); }
@ -159,8 +159,8 @@ fn test_optopt_long_no_arg() {
#[test]
fn test_optopt_long_multi() {
let args = ["--test=20", "--test=30"];
let opts = [opt::optopt("test")];
let args = [~"--test=20", ~"--test=30"];
let opts = [opt::optopt(~"test")];
let rs = opt::getopts(args, opts);
alt rs {
opt::failure(f) { check_fail_type(f, option_duplicated); }
@ -170,13 +170,13 @@ fn test_optopt_long_multi() {
#[test]
fn test_optopt_short() {
let args = ["-t", "20"];
let opts = [opt::optopt("t")];
let args = [~"-t", ~"20"];
let opts = [opt::optopt(~"t")];
let rs = opt::getopts(args, opts);
alt rs {
opt::success(m) {
assert (opt::opt_present(m, "t"));
assert (opt::opt_str(m, "t") == "20");
assert (opt::opt_present(m, ~"t"));
assert (opt::opt_str(m, ~"t") == ~"20");
}
_ { fail; }
}
@ -184,19 +184,19 @@ fn test_optopt_short() {
#[test]
fn test_optopt_short_missing() {
let args = ["blah"];
let opts = [opt::optopt("t")];
let args = [~"blah"];
let opts = [opt::optopt(~"t")];
let rs = opt::getopts(args, opts);
alt rs {
opt::success(m) { assert (!opt::opt_present(m, "t")); }
opt::success(m) { assert (!opt::opt_present(m, ~"t")); }
_ { fail; }
}
}
#[test]
fn test_optopt_short_no_arg() {
let args = ["-t"];
let opts = [opt::optopt("t")];
let args = [~"-t"];
let opts = [opt::optopt(~"t")];
let rs = opt::getopts(args, opts);
alt rs {
opt::failure(f) { check_fail_type(f, argument_missing); }
@ -206,8 +206,8 @@ fn test_optopt_short_no_arg() {
#[test]
fn test_optopt_short_multi() {
let args = ["-t", "20", "-t", "30"];
let opts = [opt::optopt("t")];
let args = [~"-t", ~"20", ~"-t", ~"30"];
let opts = [opt::optopt(~"t")];
let rs = opt::getopts(args, opts);
alt rs {
opt::failure(f) { check_fail_type(f, option_duplicated); }
@ -219,30 +219,30 @@ fn test_optopt_short_multi() {
// Tests for optflag
#[test]
fn test_optflag_long() {
let args = ["--test"];
let opts = [opt::optflag("test")];
let args = [~"--test"];
let opts = [opt::optflag(~"test")];
let rs = opt::getopts(args, opts);
alt rs {
opt::success(m) { assert (opt::opt_present(m, "test")); }
opt::success(m) { assert (opt::opt_present(m, ~"test")); }
_ { fail; }
}
}
#[test]
fn test_optflag_long_missing() {
let args = ["blah"];
let opts = [opt::optflag("test")];
let args = [~"blah"];
let opts = [opt::optflag(~"test")];
let rs = opt::getopts(args, opts);
alt rs {
opt::success(m) { assert (!opt::opt_present(m, "test")); }
opt::success(m) { assert (!opt::opt_present(m, ~"test")); }
_ { fail; }
}
}
#[test]
fn test_optflag_long_arg() {
let args = ["--test=20"];
let opts = [opt::optflag("test")];
let args = [~"--test=20"];
let opts = [opt::optflag(~"test")];
let rs = opt::getopts(args, opts);
alt rs {
opt::failure(f) {
@ -255,8 +255,8 @@ fn test_optflag_long_arg() {
#[test]
fn test_optflag_long_multi() {
let args = ["--test", "--test"];
let opts = [opt::optflag("test")];
let args = [~"--test", ~"--test"];
let opts = [opt::optflag(~"test")];
let rs = opt::getopts(args, opts);
alt rs {
opt::failure(f) { check_fail_type(f, option_duplicated); }
@ -266,36 +266,36 @@ fn test_optflag_long_multi() {
#[test]
fn test_optflag_short() {
let args = ["-t"];
let opts = [opt::optflag("t")];
let args = [~"-t"];
let opts = [opt::optflag(~"t")];
let rs = opt::getopts(args, opts);
alt rs {
opt::success(m) { assert (opt::opt_present(m, "t")); }
opt::success(m) { assert (opt::opt_present(m, ~"t")); }
_ { fail; }
}
}
#[test]
fn test_optflag_short_missing() {
let args = ["blah"];
let opts = [opt::optflag("t")];
let args = [~"blah"];
let opts = [opt::optflag(~"t")];
let rs = opt::getopts(args, opts);
alt rs {
opt::success(m) { assert (!opt::opt_present(m, "t")); }
opt::success(m) { assert (!opt::opt_present(m, ~"t")); }
_ { fail; }
}
}
#[test]
fn test_optflag_short_arg() {
let args = ["-t", "20"];
let opts = [opt::optflag("t")];
let args = [~"-t", ~"20"];
let opts = [opt::optflag(~"t")];
let rs = opt::getopts(args, opts);
alt rs {
opt::success(m) {
// The next variable after the flag is just a free argument
assert (m.free[0] == "20");
assert (m.free[0] == ~"20");
}
_ { fail; }
}
@ -303,8 +303,8 @@ fn test_optflag_short_arg() {
#[test]
fn test_optflag_short_multi() {
let args = ["-t", "-t"];
let opts = [opt::optflag("t")];
let args = [~"-t", ~"-t"];
let opts = [opt::optflag(~"t")];
let rs = opt::getopts(args, opts);
alt rs {
opt::failure(f) { check_fail_type(f, option_duplicated); }
@ -316,13 +316,13 @@ fn test_optflag_short_multi() {
// Tests for optmulti
#[test]
fn test_optmulti_long() {
let args = ["--test=20"];
let opts = [opt::optmulti("test")];
let args = [~"--test=20"];
let opts = [opt::optmulti(~"test")];
let rs = opt::getopts(args, opts);
alt rs {
opt::success(m) {
assert (opt::opt_present(m, "test"));
assert (opt::opt_str(m, "test") == "20");
assert (opt::opt_present(m, ~"test"));
assert (opt::opt_str(m, ~"test") == ~"20");
}
_ { fail; }
}
@ -330,19 +330,19 @@ fn test_optmulti_long() {
#[test]
fn test_optmulti_long_missing() {
let args = ["blah"];
let opts = [opt::optmulti("test")];
let args = [~"blah"];
let opts = [opt::optmulti(~"test")];
let rs = opt::getopts(args, opts);
alt rs {
opt::success(m) { assert (!opt::opt_present(m, "test")); }
opt::success(m) { assert (!opt::opt_present(m, ~"test")); }
_ { fail; }
}
}
#[test]
fn test_optmulti_long_no_arg() {
let args = ["--test"];
let opts = [opt::optmulti("test")];
let args = [~"--test"];
let opts = [opt::optmulti(~"test")];
let rs = opt::getopts(args, opts);
alt rs {
opt::failure(f) { check_fail_type(f, argument_missing); }
@ -352,15 +352,15 @@ fn test_optmulti_long_no_arg() {
#[test]
fn test_optmulti_long_multi() {
let args = ["--test=20", "--test=30"];
let opts = [opt::optmulti("test")];
let args = [~"--test=20", ~"--test=30"];
let opts = [opt::optmulti(~"test")];
let rs = opt::getopts(args, opts);
alt rs {
opt::success(m) {
assert (opt::opt_present(m, "test"));
assert (opt::opt_str(m, "test") == "20");
assert (opt::opt_strs(m, "test")[0] == "20");
assert (opt::opt_strs(m, "test")[1] == "30");
assert (opt::opt_present(m, ~"test"));
assert (opt::opt_str(m, ~"test") == ~"20");
assert (opt::opt_strs(m, ~"test")[0] == ~"20");
assert (opt::opt_strs(m, ~"test")[1] == ~"30");
}
_ { fail; }
}
@ -368,13 +368,13 @@ fn test_optmulti_long_multi() {
#[test]
fn test_optmulti_short() {
let args = ["-t", "20"];
let opts = [opt::optmulti("t")];
let args = [~"-t", ~"20"];
let opts = [opt::optmulti(~"t")];
let rs = opt::getopts(args, opts);
alt rs {
opt::success(m) {
assert (opt::opt_present(m, "t"));
assert (opt::opt_str(m, "t") == "20");
assert (opt::opt_present(m, ~"t"));
assert (opt::opt_str(m, ~"t") == ~"20");
}
_ { fail; }
}
@ -382,19 +382,19 @@ fn test_optmulti_short() {
#[test]
fn test_optmulti_short_missing() {
let args = ["blah"];
let opts = [opt::optmulti("t")];
let args = [~"blah"];
let opts = [opt::optmulti(~"t")];
let rs = opt::getopts(args, opts);
alt rs {
opt::success(m) { assert (!opt::opt_present(m, "t")); }
opt::success(m) { assert (!opt::opt_present(m, ~"t")); }
_ { fail; }
}
}
#[test]
fn test_optmulti_short_no_arg() {
let args = ["-t"];
let opts = [opt::optmulti("t")];
let args = [~"-t"];
let opts = [opt::optmulti(~"t")];
let rs = opt::getopts(args, opts);
alt rs {
opt::failure(f) { check_fail_type(f, argument_missing); }
@ -404,15 +404,15 @@ fn test_optmulti_short_no_arg() {
#[test]
fn test_optmulti_short_multi() {
let args = ["-t", "20", "-t", "30"];
let opts = [opt::optmulti("t")];
let args = [~"-t", ~"20", ~"-t", ~"30"];
let opts = [opt::optmulti(~"t")];
let rs = opt::getopts(args, opts);
alt rs {
opt::success(m) {
assert (opt::opt_present(m, "t"));
assert (opt::opt_str(m, "t") == "20");
assert (opt::opt_strs(m, "t")[0] == "20");
assert (opt::opt_strs(m, "t")[1] == "30");
assert (opt::opt_present(m, ~"t"));
assert (opt::opt_str(m, ~"t") == ~"20");
assert (opt::opt_strs(m, ~"t")[0] == ~"20");
assert (opt::opt_strs(m, ~"t")[1] == ~"30");
}
_ { fail; }
}
@ -420,8 +420,8 @@ fn test_optmulti_short_multi() {
#[test]
fn test_unrecognized_option_long() {
let args = ["--untest"];
let opts = [opt::optmulti("t")];
let args = [~"--untest"];
let opts = [opt::optmulti(~"t")];
let rs = opt::getopts(args, opts);
alt rs {
opt::failure(f) { check_fail_type(f, unrecognized_option); }
@ -431,8 +431,8 @@ fn test_unrecognized_option_long() {
#[test]
fn test_unrecognized_option_short() {
let args = ["-t"];
let opts = [opt::optmulti("test")];
let args = [~"-t"];
let opts = [opt::optmulti(~"test")];
let rs = opt::getopts(args, opts);
alt rs {
opt::failure(f) { check_fail_type(f, unrecognized_option); }
@ -443,24 +443,25 @@ fn test_unrecognized_option_short() {
#[test]
fn test_combined() {
let args =
["prog", "free1", "-s", "20", "free2", "--flag", "--long=30", "-f",
"-m", "40", "-m", "50"];
[~"prog", ~"free1", ~"-s", ~"20", ~"free2", ~"--flag",
~"--long=30", ~"-f", ~"-m", ~"40", ~"-m", ~"50"];
let opts =
[opt::optopt("s"), opt::optflag("flag"), opt::reqopt("long"),
opt::optflag("f"), opt::optmulti("m"), opt::optopt("notpresent")];
[opt::optopt(~"s"), opt::optflag(~"flag"), opt::reqopt(~"long"),
opt::optflag(~"f"), opt::optmulti(~"m"),
opt::optopt(~"notpresent")];
let rs = opt::getopts(args, opts);
alt rs {
opt::success(m) {
assert (m.free[0] == "prog");
assert (m.free[1] == "free1");
assert (opt::opt_str(m, "s") == "20");
assert (m.free[2] == "free2");
assert (opt::opt_present(m, "flag"));
assert (opt::opt_str(m, "long") == "30");
assert (opt::opt_present(m, "f"));
assert (opt::opt_strs(m, "m")[0] == "40");
assert (opt::opt_strs(m, "m")[1] == "50");
assert (!opt::opt_present(m, "notpresent"));
assert (m.free[0] == ~"prog");
assert (m.free[1] == ~"free1");
assert (opt::opt_str(m, ~"s") == ~"20");
assert (m.free[2] == ~"free2");
assert (opt::opt_present(m, ~"flag"));
assert (opt::opt_str(m, ~"long") == ~"30");
assert (opt::opt_present(m, ~"f"));
assert (opt::opt_strs(m, ~"m")[0] == ~"40");
assert (opt::opt_strs(m, ~"m")[1] == ~"50");
assert (!opt::opt_present(m, ~"notpresent"));
}
_ { fail; }
}