2011-06-15 13:19:50 -05:00
|
|
|
|
|
|
|
|
2011-04-26 10:46:54 -05:00
|
|
|
/* Simple getopt alternative. Construct a vector of options, either by using
|
|
|
|
* reqopt, optopt, and optflag or by building them from components yourself,
|
|
|
|
* and pass them to getopts, along with a vector of actual arguments (not
|
|
|
|
* including argv[0]). You'll either get a failure code back, or a match.
|
|
|
|
* You'll have to verify whether the amount of 'free' arguments in the match
|
|
|
|
* is what you expect. Use opt_* accessors (bottom of the file) to get
|
|
|
|
* argument values out of the match object.
|
2011-06-15 13:19:50 -05:00
|
|
|
*/
|
2011-09-12 18:13:28 -05:00
|
|
|
import option::{some, none};
|
2011-05-21 18:30:04 -05:00
|
|
|
export opt;
|
|
|
|
export reqopt;
|
|
|
|
export optopt;
|
|
|
|
export optflag;
|
2011-06-01 20:31:09 -05:00
|
|
|
export optflagopt;
|
2011-05-21 18:30:04 -05:00
|
|
|
export optmulti;
|
|
|
|
export getopts;
|
|
|
|
export result;
|
|
|
|
export success;
|
|
|
|
export failure;
|
2011-05-22 15:41:06 -05:00
|
|
|
export match;
|
2011-05-21 18:30:04 -05:00
|
|
|
export fail_;
|
|
|
|
export fail_str;
|
|
|
|
export opt_present;
|
|
|
|
export opt_str;
|
|
|
|
export opt_strs;
|
|
|
|
export opt_maybe_str;
|
2011-06-01 20:31:09 -05:00
|
|
|
export opt_default;
|
2011-05-21 18:30:04 -05:00
|
|
|
|
2011-09-02 17:34:58 -05:00
|
|
|
tag name { long(str); short(char); }
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2011-04-26 10:46:54 -05:00
|
|
|
tag hasarg { yes; no; maybe; }
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2011-04-26 10:46:54 -05:00
|
|
|
tag occur { req; optional; multi; }
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
type opt = {name: name, hasarg: hasarg, occur: occur};
|
2011-04-26 10:46:54 -05:00
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn mkname(nm: str) -> name {
|
2011-09-01 19:27:58 -05:00
|
|
|
ret if str::char_len(nm) == 1u {
|
|
|
|
short(str::char_at(nm, 0u))
|
2011-06-15 13:19:50 -05:00
|
|
|
} else { long(nm) };
|
2011-04-26 10:46:54 -05:00
|
|
|
}
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn reqopt(name: str) -> opt {
|
2011-07-27 07:19:39 -05:00
|
|
|
ret {name: mkname(name), hasarg: yes, occur: req};
|
2011-04-26 10:46:54 -05:00
|
|
|
}
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn optopt(name: str) -> opt {
|
2011-07-27 07:19:39 -05:00
|
|
|
ret {name: mkname(name), hasarg: yes, occur: optional};
|
2011-04-26 10:46:54 -05:00
|
|
|
}
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn optflag(name: str) -> opt {
|
2011-07-27 07:19:39 -05:00
|
|
|
ret {name: mkname(name), hasarg: no, occur: optional};
|
2011-04-26 10:46:54 -05:00
|
|
|
}
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn optflagopt(name: str) -> opt {
|
2011-07-27 07:19:39 -05:00
|
|
|
ret {name: mkname(name), hasarg: maybe, occur: optional};
|
2011-06-01 20:31:09 -05:00
|
|
|
}
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn optmulti(name: str) -> opt {
|
2011-07-27 07:19:39 -05:00
|
|
|
ret {name: mkname(name), hasarg: yes, occur: multi};
|
2011-04-26 10:46:54 -05:00
|
|
|
}
|
|
|
|
|
2011-09-02 17:34:58 -05:00
|
|
|
tag optval { val(str); given; }
|
2011-04-26 10:46:54 -05:00
|
|
|
|
2011-09-02 17:34:58 -05:00
|
|
|
type match = {opts: [opt], vals: [mutable [optval]], free: [str]};
|
2011-04-26 10:46:54 -05:00
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn is_arg(arg: str) -> bool {
|
2011-09-01 19:27:58 -05:00
|
|
|
ret str::byte_len(arg) > 1u && arg[0] == '-' as u8;
|
2011-04-26 10:46:54 -05:00
|
|
|
}
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn name_str(nm: name) -> str {
|
2011-09-01 19:27:58 -05:00
|
|
|
ret alt nm { short(ch) { str::from_char(ch) } long(s) { s } };
|
2011-04-26 10:46:54 -05:00
|
|
|
}
|
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn find_opt(opts: [opt], nm: name) -> option::t<uint> {
|
2011-07-27 07:19:39 -05:00
|
|
|
let i = 0u;
|
2011-08-12 12:56:57 -05:00
|
|
|
let l = vec::len::<opt>(opts);
|
2011-08-19 17:16:48 -05:00
|
|
|
while i < l { if opts[i].name == nm { ret some::<uint>(i); } i += 1u; }
|
2011-08-12 12:56:57 -05:00
|
|
|
ret none::<uint>;
|
2011-04-26 10:46:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
tag fail_ {
|
2011-09-02 17:34:58 -05:00
|
|
|
argument_missing(str);
|
|
|
|
unrecognized_option(str);
|
|
|
|
option_missing(str);
|
|
|
|
option_duplicated(str);
|
|
|
|
unexpected_argument(str);
|
2011-04-26 10:46:54 -05:00
|
|
|
}
|
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn fail_str(f: fail_) -> str {
|
2011-07-27 07:19:39 -05:00
|
|
|
ret alt f {
|
2011-09-02 17:34:58 -05:00
|
|
|
argument_missing(nm) { "Argument to option '" + nm + "' missing." }
|
|
|
|
unrecognized_option(nm) { "Unrecognized option: '" + nm + "'." }
|
|
|
|
option_missing(nm) { "Required option '" + nm + "' missing." }
|
2011-07-27 07:19:39 -05:00
|
|
|
option_duplicated(nm) {
|
2011-09-02 17:34:58 -05:00
|
|
|
"Option '" + nm + "' given more than once."
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
|
|
|
unexpected_argument(nm) {
|
2011-09-02 17:34:58 -05:00
|
|
|
"Option " + nm + " does not take an argument."
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
2011-06-15 13:19:50 -05:00
|
|
|
};
|
2011-04-26 10:46:54 -05:00
|
|
|
}
|
|
|
|
|
2011-06-15 13:19:50 -05:00
|
|
|
tag result { success(match); failure(fail_); }
|
2011-04-26 10:46:54 -05:00
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn getopts(args: [str], opts: [opt]) -> result {
|
2011-08-12 12:56:57 -05:00
|
|
|
let n_opts = vec::len::<opt>(opts);
|
2011-08-19 17:16:48 -05:00
|
|
|
fn f(_x: uint) -> [optval] { ret []; }
|
2011-08-12 12:56:57 -05:00
|
|
|
let vals = vec::init_fn_mut::<[optval]>(f, n_opts);
|
2011-09-02 17:34:58 -05:00
|
|
|
let free: [str] = [];
|
2011-08-24 20:13:27 -05:00
|
|
|
let l = vec::len(args);
|
2011-07-27 07:19:39 -05:00
|
|
|
let i = 0u;
|
|
|
|
while i < l {
|
2011-08-19 17:16:48 -05:00
|
|
|
let cur = args[i];
|
2011-09-01 19:27:58 -05:00
|
|
|
let curlen = str::byte_len(cur);
|
2011-07-27 07:19:39 -05:00
|
|
|
if !is_arg(cur) {
|
2011-08-19 17:16:48 -05:00
|
|
|
free += [cur];
|
2011-09-02 17:34:58 -05:00
|
|
|
} else if str::eq(cur, "--") {
|
2011-07-27 07:19:39 -05:00
|
|
|
let j = i + 1u;
|
2011-08-19 17:16:48 -05:00
|
|
|
while j < l { free += [args[j]]; j += 1u; }
|
2011-04-26 10:46:54 -05:00
|
|
|
break;
|
|
|
|
} else {
|
2011-07-27 07:19:39 -05:00
|
|
|
let names;
|
2011-09-02 17:34:58 -05:00
|
|
|
let i_arg = option::none::<str>;
|
2011-08-19 17:16:48 -05:00
|
|
|
if cur[1] == '-' as u8 {
|
2011-09-01 19:27:58 -05:00
|
|
|
let tail = str::slice(cur, 2u, curlen);
|
|
|
|
let eq = str::index(tail, '=' as u8);
|
2011-07-27 07:19:39 -05:00
|
|
|
if eq == -1 {
|
2011-08-19 17:16:48 -05:00
|
|
|
names = [long(tail)];
|
2011-04-26 10:46:54 -05:00
|
|
|
} else {
|
2011-09-01 19:27:58 -05:00
|
|
|
names = [long(str::slice(tail, 0u, eq as uint))];
|
2011-06-15 13:19:50 -05:00
|
|
|
i_arg =
|
2011-09-02 17:34:58 -05:00
|
|
|
option::some::<str>(str::slice(tail,
|
|
|
|
(eq as uint) + 1u,
|
|
|
|
curlen - 2u));
|
2011-04-26 10:46:54 -05:00
|
|
|
}
|
|
|
|
} else {
|
2011-07-27 07:19:39 -05:00
|
|
|
let j = 1u;
|
2011-08-19 17:16:48 -05:00
|
|
|
names = [];
|
2011-07-27 07:19:39 -05:00
|
|
|
while j < curlen {
|
2011-09-01 19:27:58 -05:00
|
|
|
let range = str::char_range_at(cur, j);
|
2011-08-19 17:16:48 -05:00
|
|
|
names += [short(range.ch)];
|
2011-07-26 07:06:02 -05:00
|
|
|
j = range.next;
|
2011-04-26 10:46:54 -05:00
|
|
|
}
|
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
let name_pos = 0u;
|
2011-08-15 23:54:52 -05:00
|
|
|
for nm: name in names {
|
2011-04-26 10:46:54 -05:00
|
|
|
name_pos += 1u;
|
2011-07-27 07:19:39 -05:00
|
|
|
let optid;
|
|
|
|
alt find_opt(opts, nm) {
|
|
|
|
some(id) { optid = id; }
|
|
|
|
none. { ret failure(unrecognized_option(name_str(nm))); }
|
2011-04-26 10:46:54 -05:00
|
|
|
}
|
2011-08-19 17:16:48 -05:00
|
|
|
alt opts[optid].hasarg {
|
2011-07-27 07:19:39 -05:00
|
|
|
no. {
|
2011-09-02 17:34:58 -05:00
|
|
|
if !option::is_none::<str>(i_arg) {
|
2011-07-27 07:19:39 -05:00
|
|
|
ret failure(unexpected_argument(name_str(nm)));
|
2011-04-26 10:46:54 -05:00
|
|
|
}
|
2011-08-19 17:16:48 -05:00
|
|
|
vals[optid] += [given];
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
|
|
|
maybe. {
|
2011-09-02 17:34:58 -05:00
|
|
|
if !option::is_none::<str>(i_arg) {
|
2011-08-19 17:16:48 -05:00
|
|
|
vals[optid] += [val(option::get(i_arg))];
|
|
|
|
} else if name_pos < vec::len::<name>(names) ||
|
|
|
|
i + 1u == l || is_arg(args[i + 1u]) {
|
|
|
|
vals[optid] += [given];
|
|
|
|
} else { i += 1u; vals[optid] += [val(args[i])]; }
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
|
|
|
yes. {
|
2011-09-02 17:34:58 -05:00
|
|
|
if !option::is_none::<str>(i_arg) {
|
|
|
|
vals[optid] += [val(option::get::<str>(i_arg))];
|
2011-08-19 17:16:48 -05:00
|
|
|
} else if i + 1u == l {
|
2011-07-27 07:19:39 -05:00
|
|
|
ret failure(argument_missing(name_str(nm)));
|
2011-08-19 17:16:48 -05:00
|
|
|
} else { i += 1u; vals[optid] += [val(args[i])]; }
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
2011-04-26 10:46:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
i += 1u;
|
|
|
|
}
|
|
|
|
i = 0u;
|
2011-07-27 07:19:39 -05:00
|
|
|
while i < n_opts {
|
2011-08-19 17:16:48 -05:00
|
|
|
let n = vec::len::<optval>(vals[i]);
|
|
|
|
let occ = opts[i].occur;
|
2011-07-27 07:19:39 -05:00
|
|
|
if occ == req {
|
|
|
|
if n == 0u {
|
2011-08-19 17:16:48 -05:00
|
|
|
ret failure(option_missing(name_str(opts[i].name)));
|
2011-06-15 13:19:50 -05:00
|
|
|
}
|
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
if occ != multi {
|
|
|
|
if n > 1u {
|
2011-08-19 17:16:48 -05:00
|
|
|
ret failure(option_duplicated(name_str(opts[i].name)));
|
2011-06-15 13:19:50 -05:00
|
|
|
}
|
|
|
|
}
|
2011-04-26 10:46:54 -05:00
|
|
|
i += 1u;
|
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
ret success({opts: opts, vals: vals, free: free});
|
2011-04-26 10:46:54 -05:00
|
|
|
}
|
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn opt_vals(m: match, nm: str) -> [optval] {
|
2011-07-27 07:19:39 -05:00
|
|
|
ret alt find_opt(m.opts, mkname(nm)) {
|
2011-08-19 17:16:48 -05:00
|
|
|
some(id) { m.vals[id] }
|
2011-09-02 17:34:58 -05:00
|
|
|
none. { log_err "No option '" + nm + "' defined."; fail }
|
2011-06-15 13:19:50 -05:00
|
|
|
};
|
2011-04-26 10:46:54 -05:00
|
|
|
}
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn opt_val(m: match, nm: str) -> optval { ret opt_vals(m, nm)[0]; }
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn opt_present(m: match, nm: str) -> bool {
|
2011-08-12 12:56:57 -05:00
|
|
|
ret vec::len::<optval>(opt_vals(m, nm)) > 0u;
|
2011-04-26 10:46:54 -05:00
|
|
|
}
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn opt_str(m: match, nm: str) -> str {
|
2011-07-27 07:19:39 -05:00
|
|
|
ret alt opt_val(m, nm) { val(s) { s } _ { fail } };
|
2011-04-26 10:46:54 -05:00
|
|
|
}
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn opt_strs(m: match, nm: str) -> [str] {
|
2011-09-02 17:34:58 -05:00
|
|
|
let acc: [str] = [];
|
2011-08-15 23:54:52 -05:00
|
|
|
for v: optval in opt_vals(m, nm) {
|
2011-08-19 17:16:48 -05:00
|
|
|
alt v { val(s) { acc += [s]; } _ { } }
|
2011-04-26 10:46:54 -05:00
|
|
|
}
|
|
|
|
ret acc;
|
|
|
|
}
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn opt_maybe_str(m: match, nm: str) -> option::t<str> {
|
2011-07-27 07:19:39 -05:00
|
|
|
let vals = opt_vals(m, nm);
|
2011-09-02 17:34:58 -05:00
|
|
|
if vec::len::<optval>(vals) == 0u { ret none::<str>; }
|
|
|
|
ret alt vals[0] { val(s) { some::<str>(s) } _ { none::<str> } };
|
2011-04-26 10:46:54 -05:00
|
|
|
}
|
|
|
|
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2011-06-01 20:31:09 -05:00
|
|
|
/// 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.
|
2011-09-12 04:27:30 -05:00
|
|
|
fn opt_default(m: match, nm: str, def: str) -> option::t<str> {
|
2011-07-27 07:19:39 -05:00
|
|
|
let vals = opt_vals(m, nm);
|
2011-09-02 17:34:58 -05:00
|
|
|
if vec::len::<optval>(vals) == 0u { ret none::<str>; }
|
|
|
|
ret alt vals[0] { val(s) { some::<str>(s) } _ { some::<str>(def) } }
|
2011-06-01 20:31:09 -05:00
|
|
|
}
|
2011-04-26 10:46:54 -05:00
|
|
|
// Local Variables:
|
|
|
|
// mode: rust;
|
|
|
|
// fill-column: 78;
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
// c-basic-offset: 4
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
2011-06-15 14:01:19 -05:00
|
|
|
// compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
|
2011-04-26 10:46:54 -05:00
|
|
|
// End:
|