2012-07-04 16:53:12 -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_*
|
2012-07-31 15:18:46 -05:00
|
|
|
* accessors to get argument values out of the matches object.
|
2012-07-04 16:53:12 -05:00
|
|
|
*
|
|
|
|
* Single-character options are expected to appear on the command line with a
|
|
|
|
* single preceding dash; multiple-character options are expected to be
|
|
|
|
* proceeded by two dashes. Options that expect an argument accept their
|
|
|
|
* argument following either a space or an equals sign.
|
|
|
|
*
|
|
|
|
* # Example
|
|
|
|
*
|
|
|
|
* The following example shows simple command line parsing for an application
|
|
|
|
* that requires an input file to be specified, accepts an optional output
|
|
|
|
* file name following -o, and accepts both -h and --help as optional flags.
|
|
|
|
*
|
|
|
|
* use std;
|
|
|
|
* import std::getopts::{optopt,optflag,getopts,opt_present,opt_maybe_str,
|
|
|
|
* fail_str};
|
|
|
|
*
|
2012-08-20 14:23:37 -05:00
|
|
|
* fn do_work(in: str, out: Option<str>) {
|
2012-07-04 16:53:12 -05:00
|
|
|
* // ...
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* fn print_usage(program: str) {
|
2012-07-11 18:49:02 -05:00
|
|
|
* io::println("Usage: " + program + " [options]");
|
2012-07-04 16:53:12 -05:00
|
|
|
* io::println("-o\t\tOutput");
|
|
|
|
* io::println("-h --help\tUsage");
|
|
|
|
* }
|
|
|
|
*
|
2012-07-11 18:49:02 -05:00
|
|
|
* fn main(args: ~[str]) {
|
2012-07-04 16:53:12 -05:00
|
|
|
* check vec::is_not_empty(args);
|
|
|
|
*
|
|
|
|
* let program : str = vec::head(args);
|
|
|
|
*
|
2012-07-11 18:49:02 -05:00
|
|
|
* let opts = ~[
|
2012-07-04 16:53:12 -05:00
|
|
|
* optopt("o"),
|
|
|
|
* optflag("h"),
|
|
|
|
* optflag("help")
|
2012-07-11 18:49:02 -05:00
|
|
|
* ];
|
2012-08-06 14:34:08 -05:00
|
|
|
* let matches = match getopts(vec::tail(args), opts) {
|
2012-07-04 16:53:12 -05:00
|
|
|
* result::ok(m) { m }
|
|
|
|
* result::err(f) { fail fail_str(f) }
|
|
|
|
* };
|
2012-07-31 15:18:46 -05:00
|
|
|
* if opt_present(matches, "h") || opt_present(matches, "help") {
|
2012-07-04 16:53:12 -05:00
|
|
|
* print_usage(program);
|
2012-08-01 19:30:05 -05:00
|
|
|
* return;
|
2012-07-04 16:53:12 -05:00
|
|
|
* }
|
2012-07-31 15:18:46 -05:00
|
|
|
* let output = opt_maybe_str(matches, "o");
|
|
|
|
* let input = if vec::is_not_empty(matches.free) {
|
|
|
|
* matches.free[0]
|
2012-07-04 16:53:12 -05:00
|
|
|
* } else {
|
|
|
|
* print_usage(program);
|
2012-08-01 19:30:05 -05:00
|
|
|
* return;
|
2012-07-04 16:53:12 -05:00
|
|
|
* };
|
|
|
|
* do_work(input, output);
|
|
|
|
* }
|
|
|
|
*/
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2012-08-26 18:41:18 -05:00
|
|
|
#[forbid(deprecated_mode)];
|
|
|
|
#[forbid(deprecated_pattern)];
|
|
|
|
|
2012-08-26 18:54:31 -05:00
|
|
|
import core::result::{Err, Ok};
|
2011-12-13 18:25:51 -06:00
|
|
|
import core::option;
|
2012-08-20 14:23:37 -05:00
|
|
|
import core::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;
|
2012-07-31 15:18:46 -05:00
|
|
|
export matches;
|
2011-05-21 18:30:04 -05:00
|
|
|
export fail_;
|
|
|
|
export fail_str;
|
|
|
|
export opt_present;
|
2012-05-31 19:02:03 -05:00
|
|
|
export opts_present;
|
2011-05-21 18:30:04 -05:00
|
|
|
export opt_str;
|
2012-05-31 19:02:03 -05:00
|
|
|
export opts_str;
|
2011-05-21 18:30:04 -05:00
|
|
|
export opt_strs;
|
|
|
|
export opt_maybe_str;
|
2011-06-01 20:31:09 -05:00
|
|
|
export opt_default;
|
2012-03-22 22:06:01 -05:00
|
|
|
export result; //NDM
|
2011-05-21 18:30:04 -05:00
|
|
|
|
2012-07-14 00:57:48 -05:00
|
|
|
enum name { long(~str), short(char), }
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2012-01-19 19:55:34 -06:00
|
|
|
enum hasarg { yes, no, maybe, }
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2012-01-19 19:55:34 -06:00
|
|
|
enum occur { req, optional, multi, }
|
2011-04-26 10:46:54 -05:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// A description of a possible option
|
2011-07-27 07:19:39 -05:00
|
|
|
type opt = {name: name, hasarg: hasarg, occur: occur};
|
2011-04-26 10:46:54 -05:00
|
|
|
|
2012-08-26 18:41:18 -05:00
|
|
|
fn mkname(nm: &str) -> name {
|
|
|
|
let unm = str::from_slice(nm);
|
2012-08-01 19:30:05 -05:00
|
|
|
return if str::len(nm) == 1u {
|
2012-08-26 18:41:18 -05:00
|
|
|
short(str::char_at(unm, 0u))
|
|
|
|
} else { long(unm) };
|
2011-04-26 10:46:54 -05:00
|
|
|
}
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Create an option that is required and takes an argument
|
2012-08-26 18:41:18 -05:00
|
|
|
fn reqopt(name: &str) -> opt {
|
2012-08-01 19:30:05 -05:00
|
|
|
return {name: mkname(name), hasarg: yes, occur: req};
|
2011-04-26 10:46:54 -05:00
|
|
|
}
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Create an option that is optional and takes an argument
|
2012-08-26 18:41:18 -05:00
|
|
|
fn optopt(name: &str) -> opt {
|
2012-08-01 19:30:05 -05:00
|
|
|
return {name: mkname(name), hasarg: yes, occur: optional};
|
2011-04-26 10:46:54 -05:00
|
|
|
}
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Create an option that is optional and does not take an argument
|
2012-08-26 18:41:18 -05:00
|
|
|
fn optflag(name: &str) -> opt {
|
2012-08-01 19:30:05 -05:00
|
|
|
return {name: mkname(name), hasarg: no, occur: optional};
|
2011-04-26 10:46:54 -05:00
|
|
|
}
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Create an option that is optional and takes an optional argument
|
2012-08-26 18:41:18 -05:00
|
|
|
fn optflagopt(name: &str) -> opt {
|
2012-08-01 19:30:05 -05:00
|
|
|
return {name: mkname(name), hasarg: maybe, occur: optional};
|
2011-06-01 20:31:09 -05:00
|
|
|
}
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Create an option that is optional, takes an argument, and may occur
|
|
|
|
* multiple times
|
|
|
|
*/
|
2012-08-26 18:41:18 -05:00
|
|
|
fn optmulti(name: &str) -> opt {
|
2012-08-01 19:30:05 -05:00
|
|
|
return {name: mkname(name), hasarg: yes, occur: multi};
|
2011-04-26 10:46:54 -05:00
|
|
|
}
|
|
|
|
|
2012-07-14 00:57:48 -05:00
|
|
|
enum optval { val(~str), given, }
|
2011-04-26 10:46:54 -05:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* The result of checking command line arguments. Contains a vector
|
|
|
|
* of matches and a vector of free strings.
|
|
|
|
*/
|
2012-07-31 15:18:46 -05:00
|
|
|
type matches = {opts: ~[opt], vals: ~[~[optval]], free: ~[~str]};
|
2011-04-26 10:46:54 -05:00
|
|
|
|
2012-08-26 18:41:18 -05:00
|
|
|
fn is_arg(arg: &str) -> bool {
|
2012-08-01 19:30:05 -05:00
|
|
|
return str::len(arg) > 1u && arg[0] == '-' as u8;
|
2011-04-26 10:46:54 -05:00
|
|
|
}
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2012-08-26 18:41:18 -05:00
|
|
|
fn name_str(nm: &name) -> ~str {
|
|
|
|
return match *nm {
|
2012-08-03 21:59:04 -05:00
|
|
|
short(ch) => str::from_char(ch),
|
|
|
|
long(s) => s
|
|
|
|
};
|
2011-04-26 10:46:54 -05:00
|
|
|
}
|
|
|
|
|
2012-08-26 18:41:18 -05:00
|
|
|
fn find_opt(opts: &[opt], +nm: name) -> Option<uint> {
|
2012-06-30 18:19:07 -05:00
|
|
|
vec::position(opts, |opt| opt.name == nm)
|
2011-04-26 10:46:54 -05:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* The type returned when the command line does not conform to the
|
|
|
|
* expected format. Pass this value to <fail_str> to get an error message.
|
|
|
|
*/
|
2012-01-19 17:20:57 -06:00
|
|
|
enum fail_ {
|
2012-07-14 00:57:48 -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
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Convert a `fail_` enum into an error string
|
2012-08-26 18:41:18 -05:00
|
|
|
fn fail_str(+f: fail_) -> ~str {
|
2012-08-06 14:34:08 -05:00
|
|
|
return match f {
|
2012-08-03 21:59:04 -05:00
|
|
|
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.",
|
|
|
|
unexpected_argument(nm) => {
|
|
|
|
~"Option " + nm + ~" does not take an argument."
|
|
|
|
}
|
|
|
|
};
|
2011-04-26 10:46:54 -05:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* The result of parsing a command line with a set of options
|
2012-07-31 15:18:46 -05:00
|
|
|
* (result::t<matches, fail_>)
|
2012-07-04 16:53:12 -05:00
|
|
|
*/
|
2012-08-26 18:54:31 -05:00
|
|
|
type result = result::Result<matches, fail_>;
|
2011-04-26 10:46:54 -05:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Parse command line arguments according to the provided options
|
|
|
|
*
|
|
|
|
* On success returns `ok(opt)`. Use functions such as `opt_present`
|
|
|
|
* `opt_str`, etc. to interrogate results. Returns `err(fail_)` on failure.
|
|
|
|
* Use <fail_str> to get an error message.
|
|
|
|
*/
|
2012-08-26 18:41:18 -05:00
|
|
|
fn getopts(args: &[~str], opts: &[opt]) -> result unsafe {
|
2011-08-12 12:56:57 -05:00
|
|
|
let n_opts = vec::len::<opt>(opts);
|
2012-08-01 19:30:05 -05:00
|
|
|
fn f(_x: uint) -> ~[optval] { return ~[]; }
|
2012-03-12 17:52:30 -05:00
|
|
|
let vals = vec::to_mut(vec::from_fn(n_opts, f));
|
2012-07-14 00:57:48 -05:00
|
|
|
let mut free: ~[~str] = ~[];
|
2011-08-24 20:13:27 -05:00
|
|
|
let l = vec::len(args);
|
2012-03-14 13:03:56 -05:00
|
|
|
let mut i = 0u;
|
2011-07-27 07:19:39 -05:00
|
|
|
while i < l {
|
2011-08-19 17:16:48 -05:00
|
|
|
let cur = args[i];
|
2012-02-23 03:44:04 -06:00
|
|
|
let curlen = str::len(cur);
|
2011-07-27 07:19:39 -05:00
|
|
|
if !is_arg(cur) {
|
2012-06-28 00:49:05 -05:00
|
|
|
vec::push(free, cur);
|
2012-08-02 17:42:56 -05:00
|
|
|
} else if cur == ~"--" {
|
2012-03-14 13:03:56 -05:00
|
|
|
let mut j = i + 1u;
|
2012-06-25 22:00:46 -05:00
|
|
|
while j < l { vec::push(free, args[j]); j += 1u; }
|
2011-04-26 10:46:54 -05:00
|
|
|
break;
|
|
|
|
} else {
|
2012-03-14 13:03:56 -05:00
|
|
|
let mut names;
|
2012-08-20 14:23:37 -05:00
|
|
|
let mut i_arg = option::None::<~str>;
|
2011-08-19 17:16:48 -05:00
|
|
|
if cur[1] == '-' as u8 {
|
2012-02-23 02:45:25 -06:00
|
|
|
let tail = str::slice(cur, 2u, curlen);
|
2012-02-11 05:20:45 -06:00
|
|
|
let tail_eq = str::splitn_char(tail, '=', 1u);
|
|
|
|
if vec::len(tail_eq) <= 1u {
|
2012-06-29 18:26:56 -05:00
|
|
|
names = ~[long(tail)];
|
2011-04-26 10:46:54 -05:00
|
|
|
} else {
|
2012-02-01 06:20:21 -06:00
|
|
|
names =
|
2012-06-29 18:26:56 -05:00
|
|
|
~[long(tail_eq[0])];
|
2011-06-15 13:19:50 -05:00
|
|
|
i_arg =
|
2012-08-20 14:23:37 -05:00
|
|
|
option::Some::<~str>(tail_eq[1]);
|
2011-04-26 10:46:54 -05:00
|
|
|
}
|
|
|
|
} else {
|
2012-03-14 13:03:56 -05:00
|
|
|
let mut j = 1u;
|
2012-08-20 14:23:37 -05:00
|
|
|
let mut last_valid_opt_id = option::None;
|
2012-06-29 18:26:56 -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);
|
2012-07-04 10:36:31 -05:00
|
|
|
let opt = short(range.ch);
|
|
|
|
|
|
|
|
/* In a series of potential options (eg. -aheJ), if we see
|
|
|
|
one which takes an argument, we assume all subsequent
|
|
|
|
characters make up the argument. This allows options
|
2012-07-04 17:01:24 -05:00
|
|
|
such as -L/usr/local/lib/foo to be interpreted
|
|
|
|
correctly
|
2012-07-04 10:36:31 -05:00
|
|
|
*/
|
2012-07-04 17:01:24 -05:00
|
|
|
|
2012-08-06 14:34:08 -05:00
|
|
|
match find_opt(opts, opt) {
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(id) => last_valid_opt_id = option::Some(id),
|
|
|
|
None => {
|
2012-07-04 17:01:24 -05:00
|
|
|
let arg_follows =
|
|
|
|
option::is_some(last_valid_opt_id) &&
|
2012-08-06 14:34:08 -05:00
|
|
|
match opts[option::get(last_valid_opt_id)]
|
|
|
|
.hasarg {
|
|
|
|
|
2012-08-03 21:59:04 -05:00
|
|
|
yes | maybe => true,
|
|
|
|
no => false
|
2012-07-04 10:36:31 -05:00
|
|
|
};
|
|
|
|
if arg_follows && j + 1 < curlen {
|
2012-08-20 14:23:37 -05:00
|
|
|
i_arg = option::Some(str::slice(cur, j, curlen));
|
2012-07-04 10:36:31 -05:00
|
|
|
break;
|
|
|
|
} else {
|
2012-08-20 14:23:37 -05:00
|
|
|
last_valid_opt_id = option::None;
|
2012-07-04 10:36:31 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
vec::push(names, opt);
|
2011-07-26 07:06:02 -05:00
|
|
|
j = range.next;
|
2011-04-26 10:46:54 -05:00
|
|
|
}
|
|
|
|
}
|
2012-03-14 13:03:56 -05:00
|
|
|
let mut name_pos = 0u;
|
2012-06-30 18:19:07 -05:00
|
|
|
for vec::each(names) |nm| {
|
2011-04-26 10:46:54 -05:00
|
|
|
name_pos += 1u;
|
2012-08-06 14:34:08 -05:00
|
|
|
let optid = match find_opt(opts, nm) {
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(id) => id,
|
2012-08-26 18:41:18 -05:00
|
|
|
None => return Err(unrecognized_option(name_str(&nm)))
|
2012-03-14 13:03:56 -05:00
|
|
|
};
|
2012-08-06 14:34:08 -05:00
|
|
|
match opts[optid].hasarg {
|
2012-08-03 21:59:04 -05:00
|
|
|
no => {
|
2012-07-14 00:57:48 -05:00
|
|
|
if !option::is_none::<~str>(i_arg) {
|
2012-08-26 18:41:18 -05:00
|
|
|
return Err(unexpected_argument(name_str(&nm)));
|
2011-04-26 10:46:54 -05:00
|
|
|
}
|
2012-06-25 22:00:46 -05:00
|
|
|
vec::push(vals[optid], given);
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
maybe => {
|
2012-07-14 00:57:48 -05:00
|
|
|
if !option::is_none::<~str>(i_arg) {
|
2012-06-25 22:00:46 -05:00
|
|
|
vec::push(vals[optid], val(option::get(i_arg)));
|
2011-08-19 17:16:48 -05:00
|
|
|
} else if name_pos < vec::len::<name>(names) ||
|
|
|
|
i + 1u == l || is_arg(args[i + 1u]) {
|
2012-06-25 22:00:46 -05:00
|
|
|
vec::push(vals[optid], given);
|
|
|
|
} else { i += 1u; vec::push(vals[optid], val(args[i])); }
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
yes => {
|
2012-07-14 00:57:48 -05:00
|
|
|
if !option::is_none::<~str>(i_arg) {
|
2012-06-25 22:00:46 -05:00
|
|
|
vec::push(vals[optid],
|
2012-07-14 00:57:48 -05:00
|
|
|
val(option::get::<~str>(i_arg)));
|
2011-08-19 17:16:48 -05:00
|
|
|
} else if i + 1u == l {
|
2012-08-26 18:41:18 -05:00
|
|
|
return Err(argument_missing(name_str(&nm)));
|
2012-06-25 22:00:46 -05:00
|
|
|
} else { i += 1u; vec::push(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 {
|
2012-08-26 18:41:18 -05:00
|
|
|
return Err(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 {
|
2012-08-26 18:41:18 -05:00
|
|
|
return Err(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;
|
|
|
|
}
|
2012-08-26 18:41:18 -05:00
|
|
|
return Ok({opts: vec::from_slice(opts),
|
|
|
|
vals: vec::from_mut(vals),
|
|
|
|
free: free});
|
2011-04-26 10:46:54 -05:00
|
|
|
}
|
|
|
|
|
2012-08-26 18:41:18 -05:00
|
|
|
fn opt_vals(+mm: matches, nm: &str) -> ~[optval] {
|
|
|
|
return match find_opt(mm.opts, mkname(nm)) {
|
|
|
|
Some(id) => mm.vals[id],
|
2012-08-20 14:23:37 -05:00
|
|
|
None => {
|
2012-08-22 19:24:52 -05:00
|
|
|
error!("No option '%s' defined", nm);
|
2012-08-03 21:59:04 -05:00
|
|
|
fail
|
|
|
|
}
|
|
|
|
};
|
2011-04-26 10:46:54 -05:00
|
|
|
}
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2012-08-26 18:41:18 -05:00
|
|
|
fn opt_val(+mm: matches, nm: &str) -> optval { return opt_vals(mm, nm)[0]; }
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Returns true if an option was matched
|
2012-08-26 18:41:18 -05:00
|
|
|
fn opt_present(+mm: matches, nm: &str) -> bool {
|
|
|
|
return vec::len::<optval>(opt_vals(mm, nm)) > 0u;
|
2011-04-26 10:46:54 -05:00
|
|
|
}
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Returns true if any of several options were matched
|
2012-08-26 18:41:18 -05:00
|
|
|
fn opts_present(+mm: matches, names: &[~str]) -> bool {
|
2012-06-30 18:19:07 -05:00
|
|
|
for vec::each(names) |nm| {
|
2012-08-26 18:41:18 -05:00
|
|
|
match find_opt(mm.opts, mkname(nm)) {
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(_) => return true,
|
2012-08-26 18:41:18 -05:00
|
|
|
None => ()
|
2012-05-31 19:02:03 -05:00
|
|
|
}
|
|
|
|
}
|
2012-08-01 19:30:05 -05:00
|
|
|
return false;
|
2012-05-31 19:02:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Returns the string argument supplied to a matching option
|
|
|
|
*
|
|
|
|
* Fails if the option was not matched or if the match did not take an
|
|
|
|
* argument
|
|
|
|
*/
|
2012-08-26 18:41:18 -05:00
|
|
|
fn opt_str(+mm: matches, nm: &str) -> ~str {
|
|
|
|
return match opt_val(mm, nm) { val(s) => s, _ => fail };
|
2011-04-26 10:46:54 -05:00
|
|
|
}
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Returns the string argument supplied to one of several matching options
|
|
|
|
*
|
|
|
|
* Fails if the no option was provided from the given list, or if the no such
|
|
|
|
* option took an argument
|
|
|
|
*/
|
2012-08-26 18:41:18 -05:00
|
|
|
fn opts_str(+mm: matches, names: &[~str]) -> ~str {
|
2012-06-30 18:19:07 -05:00
|
|
|
for vec::each(names) |nm| {
|
2012-08-26 18:41:18 -05:00
|
|
|
match opt_val(mm, nm) {
|
2012-08-03 21:59:04 -05:00
|
|
|
val(s) => return s,
|
|
|
|
_ => ()
|
2012-05-31 19:02:03 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Returns a vector of the arguments provided to all matches of the given
|
|
|
|
* option.
|
|
|
|
*
|
|
|
|
* Used when an option accepts multiple values.
|
|
|
|
*/
|
2012-08-26 18:41:18 -05:00
|
|
|
fn opt_strs(+mm: matches, nm: &str) -> ~[~str] {
|
2012-07-14 00:57:48 -05:00
|
|
|
let mut acc: ~[~str] = ~[];
|
2012-08-26 18:41:18 -05:00
|
|
|
for vec::each(opt_vals(mm, nm)) |v| {
|
2012-08-06 14:34:08 -05:00
|
|
|
match v { val(s) => vec::push(acc, s), _ => () }
|
2011-04-26 10:46:54 -05:00
|
|
|
}
|
2012-08-01 19:30:05 -05:00
|
|
|
return acc;
|
2011-04-26 10:46:54 -05:00
|
|
|
}
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Returns the string argument supplied to a matching option or none
|
2012-08-26 18:41:18 -05:00
|
|
|
fn opt_maybe_str(+mm: matches, nm: &str) -> Option<~str> {
|
|
|
|
let vals = opt_vals(mm, nm);
|
2012-08-20 14:23:37 -05:00
|
|
|
if vec::len::<optval>(vals) == 0u { return None::<~str>; }
|
|
|
|
return match 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
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Returns the matching string, a default, or none
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2012-08-26 18:41:18 -05:00
|
|
|
fn opt_default(+mm: matches, nm: &str, def: &str) -> Option<~str> {
|
|
|
|
let vals = opt_vals(mm, nm);
|
2012-08-20 14:23:37 -05:00
|
|
|
if vec::len::<optval>(vals) == 0u { return None::<~str>; }
|
2012-08-26 18:41:18 -05:00
|
|
|
return match vals[0] { val(s) => Some::<~str>(s),
|
|
|
|
_ => Some::<~str>(str::from_slice(def)) }
|
2011-06-01 20:31:09 -05:00
|
|
|
}
|
2012-01-17 21:05:07 -06:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
import opt = getopts;
|
2012-08-26 18:54:31 -05:00
|
|
|
import result::{Err, Ok};
|
2012-01-17 21:05:07 -06:00
|
|
|
|
2012-01-19 17:20:57 -06:00
|
|
|
enum fail_type {
|
2012-01-19 19:55:34 -06:00
|
|
|
argument_missing_,
|
|
|
|
unrecognized_option_,
|
|
|
|
option_missing_,
|
|
|
|
option_duplicated_,
|
|
|
|
unexpected_argument_,
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
|
2012-08-26 18:41:18 -05:00
|
|
|
fn check_fail_type(+f: fail_, ft: fail_type) {
|
2012-08-06 14:34:08 -05:00
|
|
|
match f {
|
2012-08-03 21:59:04 -05:00
|
|
|
argument_missing(_) => assert ft == argument_missing_,
|
|
|
|
unrecognized_option(_) => assert ft == unrecognized_option_,
|
|
|
|
option_missing(_) => assert ft == option_missing_,
|
|
|
|
option_duplicated(_) => assert ft == option_duplicated_,
|
|
|
|
unexpected_argument(_) => assert ft == unexpected_argument_
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Tests for reqopt
|
|
|
|
#[test]
|
|
|
|
fn test_reqopt_long() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let args = ~[~"--test=20"];
|
|
|
|
let opts = ~[reqopt(~"test")];
|
2012-01-17 21:05:07 -06:00
|
|
|
let rs = getopts(args, opts);
|
2012-08-15 13:55:17 -05:00
|
|
|
match rs {
|
2012-08-26 18:54:31 -05:00
|
|
|
Ok(m) => {
|
2012-07-14 00:57:48 -05:00
|
|
|
assert (opt_present(m, ~"test"));
|
|
|
|
assert (opt_str(m, ~"test") == ~"20");
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
2012-08-15 13:55:17 -05:00
|
|
|
_ => { fail ~"test_reqopt_long failed"; }
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_reqopt_long_missing() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let args = ~[~"blah"];
|
|
|
|
let opts = ~[reqopt(~"test")];
|
2012-01-17 21:05:07 -06:00
|
|
|
let rs = getopts(args, opts);
|
2012-08-06 14:34:08 -05:00
|
|
|
match rs {
|
2012-08-26 18:54:31 -05:00
|
|
|
Err(f) => check_fail_type(f, option_missing_),
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => fail
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_reqopt_long_no_arg() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let args = ~[~"--test"];
|
|
|
|
let opts = ~[reqopt(~"test")];
|
2012-01-17 21:05:07 -06:00
|
|
|
let rs = getopts(args, opts);
|
2012-08-06 14:34:08 -05:00
|
|
|
match rs {
|
2012-08-26 18:54:31 -05:00
|
|
|
Err(f) => check_fail_type(f, argument_missing_),
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => fail
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_reqopt_long_multi() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let args = ~[~"--test=20", ~"--test=30"];
|
|
|
|
let opts = ~[reqopt(~"test")];
|
2012-01-17 21:05:07 -06:00
|
|
|
let rs = getopts(args, opts);
|
2012-08-06 14:34:08 -05:00
|
|
|
match rs {
|
2012-08-26 18:54:31 -05:00
|
|
|
Err(f) => check_fail_type(f, option_duplicated_),
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => fail
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_reqopt_short() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let args = ~[~"-t", ~"20"];
|
|
|
|
let opts = ~[reqopt(~"t")];
|
2012-01-17 21:05:07 -06:00
|
|
|
let rs = getopts(args, opts);
|
2012-08-06 14:34:08 -05:00
|
|
|
match rs {
|
2012-08-26 18:54:31 -05:00
|
|
|
Ok(m) => {
|
2012-07-14 00:57:48 -05:00
|
|
|
assert (opt_present(m, ~"t"));
|
|
|
|
assert (opt_str(m, ~"t") == ~"20");
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => fail
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_reqopt_short_missing() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let args = ~[~"blah"];
|
|
|
|
let opts = ~[reqopt(~"t")];
|
2012-01-17 21:05:07 -06:00
|
|
|
let rs = getopts(args, opts);
|
2012-08-06 14:34:08 -05:00
|
|
|
match rs {
|
2012-08-26 18:54:31 -05:00
|
|
|
Err(f) => check_fail_type(f, option_missing_),
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => fail
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_reqopt_short_no_arg() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let args = ~[~"-t"];
|
|
|
|
let opts = ~[reqopt(~"t")];
|
2012-01-17 21:05:07 -06:00
|
|
|
let rs = getopts(args, opts);
|
2012-08-06 14:34:08 -05:00
|
|
|
match rs {
|
2012-08-26 18:54:31 -05:00
|
|
|
Err(f) => check_fail_type(f, argument_missing_),
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => fail
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_reqopt_short_multi() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let args = ~[~"-t", ~"20", ~"-t", ~"30"];
|
|
|
|
let opts = ~[reqopt(~"t")];
|
2012-01-17 21:05:07 -06:00
|
|
|
let rs = getopts(args, opts);
|
2012-08-06 14:34:08 -05:00
|
|
|
match rs {
|
2012-08-26 18:54:31 -05:00
|
|
|
Err(f) => check_fail_type(f, option_duplicated_),
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => fail
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Tests for optopt
|
|
|
|
#[test]
|
|
|
|
fn test_optopt_long() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let args = ~[~"--test=20"];
|
|
|
|
let opts = ~[optopt(~"test")];
|
2012-01-17 21:05:07 -06:00
|
|
|
let rs = getopts(args, opts);
|
2012-08-06 14:34:08 -05:00
|
|
|
match rs {
|
2012-08-26 18:54:31 -05:00
|
|
|
Ok(m) => {
|
2012-07-14 00:57:48 -05:00
|
|
|
assert (opt_present(m, ~"test"));
|
|
|
|
assert (opt_str(m, ~"test") == ~"20");
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => fail
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_optopt_long_missing() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let args = ~[~"blah"];
|
|
|
|
let opts = ~[optopt(~"test")];
|
2012-01-17 21:05:07 -06:00
|
|
|
let rs = getopts(args, opts);
|
2012-08-06 14:34:08 -05:00
|
|
|
match rs {
|
2012-08-26 18:54:31 -05:00
|
|
|
Ok(m) => assert (!opt_present(m, ~"test")),
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => fail
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_optopt_long_no_arg() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let args = ~[~"--test"];
|
|
|
|
let opts = ~[optopt(~"test")];
|
2012-01-17 21:05:07 -06:00
|
|
|
let rs = getopts(args, opts);
|
2012-08-06 14:34:08 -05:00
|
|
|
match rs {
|
2012-08-26 18:54:31 -05:00
|
|
|
Err(f) => check_fail_type(f, argument_missing_),
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => fail
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_optopt_long_multi() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let args = ~[~"--test=20", ~"--test=30"];
|
|
|
|
let opts = ~[optopt(~"test")];
|
2012-01-17 21:05:07 -06:00
|
|
|
let rs = getopts(args, opts);
|
2012-08-06 14:34:08 -05:00
|
|
|
match rs {
|
2012-08-26 18:54:31 -05:00
|
|
|
Err(f) => check_fail_type(f, option_duplicated_),
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => fail
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_optopt_short() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let args = ~[~"-t", ~"20"];
|
|
|
|
let opts = ~[optopt(~"t")];
|
2012-01-17 21:05:07 -06:00
|
|
|
let rs = getopts(args, opts);
|
2012-08-06 14:34:08 -05:00
|
|
|
match rs {
|
2012-08-26 18:54:31 -05:00
|
|
|
Ok(m) => {
|
2012-07-14 00:57:48 -05:00
|
|
|
assert (opt_present(m, ~"t"));
|
|
|
|
assert (opt_str(m, ~"t") == ~"20");
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => fail
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_optopt_short_missing() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let args = ~[~"blah"];
|
|
|
|
let opts = ~[optopt(~"t")];
|
2012-01-17 21:05:07 -06:00
|
|
|
let rs = getopts(args, opts);
|
2012-08-06 14:34:08 -05:00
|
|
|
match rs {
|
2012-08-26 18:54:31 -05:00
|
|
|
Ok(m) => assert (!opt_present(m, ~"t")),
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => fail
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_optopt_short_no_arg() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let args = ~[~"-t"];
|
|
|
|
let opts = ~[optopt(~"t")];
|
2012-01-17 21:05:07 -06:00
|
|
|
let rs = getopts(args, opts);
|
2012-08-06 14:34:08 -05:00
|
|
|
match rs {
|
2012-08-26 18:54:31 -05:00
|
|
|
Err(f) => check_fail_type(f, argument_missing_),
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => fail
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_optopt_short_multi() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let args = ~[~"-t", ~"20", ~"-t", ~"30"];
|
|
|
|
let opts = ~[optopt(~"t")];
|
2012-01-17 21:05:07 -06:00
|
|
|
let rs = getopts(args, opts);
|
2012-08-06 14:34:08 -05:00
|
|
|
match rs {
|
2012-08-26 18:54:31 -05:00
|
|
|
Err(f) => check_fail_type(f, option_duplicated_),
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => fail
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Tests for optflag
|
|
|
|
#[test]
|
|
|
|
fn test_optflag_long() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let args = ~[~"--test"];
|
|
|
|
let opts = ~[optflag(~"test")];
|
2012-01-17 21:05:07 -06:00
|
|
|
let rs = getopts(args, opts);
|
2012-08-06 14:34:08 -05:00
|
|
|
match rs {
|
2012-08-26 18:54:31 -05:00
|
|
|
Ok(m) => assert (opt_present(m, ~"test")),
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => fail
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_optflag_long_missing() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let args = ~[~"blah"];
|
|
|
|
let opts = ~[optflag(~"test")];
|
2012-01-17 21:05:07 -06:00
|
|
|
let rs = getopts(args, opts);
|
2012-08-06 14:34:08 -05:00
|
|
|
match rs {
|
2012-08-26 18:54:31 -05:00
|
|
|
Ok(m) => assert (!opt_present(m, ~"test")),
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => fail
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_optflag_long_arg() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let args = ~[~"--test=20"];
|
|
|
|
let opts = ~[optflag(~"test")];
|
2012-01-17 21:05:07 -06:00
|
|
|
let rs = getopts(args, opts);
|
2012-08-06 14:34:08 -05:00
|
|
|
match rs {
|
2012-08-26 18:54:31 -05:00
|
|
|
Err(f) => {
|
2012-01-17 21:05:07 -06:00
|
|
|
log(error, fail_str(f));
|
|
|
|
check_fail_type(f, unexpected_argument_);
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => fail
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_optflag_long_multi() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let args = ~[~"--test", ~"--test"];
|
|
|
|
let opts = ~[optflag(~"test")];
|
2012-01-17 21:05:07 -06:00
|
|
|
let rs = getopts(args, opts);
|
2012-08-06 14:34:08 -05:00
|
|
|
match rs {
|
2012-08-26 18:54:31 -05:00
|
|
|
Err(f) => check_fail_type(f, option_duplicated_),
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => fail
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_optflag_short() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let args = ~[~"-t"];
|
|
|
|
let opts = ~[optflag(~"t")];
|
2012-01-17 21:05:07 -06:00
|
|
|
let rs = getopts(args, opts);
|
2012-08-06 14:34:08 -05:00
|
|
|
match rs {
|
2012-08-26 18:54:31 -05:00
|
|
|
Ok(m) => assert (opt_present(m, ~"t")),
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => fail
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_optflag_short_missing() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let args = ~[~"blah"];
|
|
|
|
let opts = ~[optflag(~"t")];
|
2012-01-17 21:05:07 -06:00
|
|
|
let rs = getopts(args, opts);
|
2012-08-06 14:34:08 -05:00
|
|
|
match rs {
|
2012-08-26 18:54:31 -05:00
|
|
|
Ok(m) => assert (!opt_present(m, ~"t")),
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => fail
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_optflag_short_arg() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let args = ~[~"-t", ~"20"];
|
|
|
|
let opts = ~[optflag(~"t")];
|
2012-01-17 21:05:07 -06:00
|
|
|
let rs = getopts(args, opts);
|
2012-08-06 14:34:08 -05:00
|
|
|
match rs {
|
2012-08-26 18:54:31 -05:00
|
|
|
Ok(m) => {
|
2012-01-17 21:05:07 -06:00
|
|
|
// The next variable after the flag is just a free argument
|
|
|
|
|
2012-07-14 00:57:48 -05:00
|
|
|
assert (m.free[0] == ~"20");
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => fail
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_optflag_short_multi() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let args = ~[~"-t", ~"-t"];
|
|
|
|
let opts = ~[optflag(~"t")];
|
2012-01-17 21:05:07 -06:00
|
|
|
let rs = getopts(args, opts);
|
2012-08-06 14:34:08 -05:00
|
|
|
match rs {
|
2012-08-26 18:54:31 -05:00
|
|
|
Err(f) => check_fail_type(f, option_duplicated_),
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => fail
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Tests for optmulti
|
|
|
|
#[test]
|
|
|
|
fn test_optmulti_long() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let args = ~[~"--test=20"];
|
|
|
|
let opts = ~[optmulti(~"test")];
|
2012-01-17 21:05:07 -06:00
|
|
|
let rs = getopts(args, opts);
|
2012-08-06 14:34:08 -05:00
|
|
|
match rs {
|
2012-08-26 18:54:31 -05:00
|
|
|
Ok(m) => {
|
2012-07-14 00:57:48 -05:00
|
|
|
assert (opt_present(m, ~"test"));
|
|
|
|
assert (opt_str(m, ~"test") == ~"20");
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => fail
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_optmulti_long_missing() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let args = ~[~"blah"];
|
|
|
|
let opts = ~[optmulti(~"test")];
|
2012-01-17 21:05:07 -06:00
|
|
|
let rs = getopts(args, opts);
|
2012-08-06 14:34:08 -05:00
|
|
|
match rs {
|
2012-08-26 18:54:31 -05:00
|
|
|
Ok(m) => assert (!opt_present(m, ~"test")),
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => fail
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_optmulti_long_no_arg() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let args = ~[~"--test"];
|
|
|
|
let opts = ~[optmulti(~"test")];
|
2012-01-17 21:05:07 -06:00
|
|
|
let rs = getopts(args, opts);
|
2012-08-06 14:34:08 -05:00
|
|
|
match rs {
|
2012-08-26 18:54:31 -05:00
|
|
|
Err(f) => check_fail_type(f, argument_missing_),
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => fail
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_optmulti_long_multi() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let args = ~[~"--test=20", ~"--test=30"];
|
|
|
|
let opts = ~[optmulti(~"test")];
|
2012-01-17 21:05:07 -06:00
|
|
|
let rs = getopts(args, opts);
|
2012-08-06 14:34:08 -05:00
|
|
|
match rs {
|
2012-08-26 18:54:31 -05:00
|
|
|
Ok(m) => {
|
2012-07-14 00:57:48 -05:00
|
|
|
assert (opt_present(m, ~"test"));
|
|
|
|
assert (opt_str(m, ~"test") == ~"20");
|
|
|
|
assert (opt_strs(m, ~"test")[0] == ~"20");
|
|
|
|
assert (opt_strs(m, ~"test")[1] == ~"30");
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => fail
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_optmulti_short() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let args = ~[~"-t", ~"20"];
|
|
|
|
let opts = ~[optmulti(~"t")];
|
2012-01-17 21:05:07 -06:00
|
|
|
let rs = getopts(args, opts);
|
2012-08-06 14:34:08 -05:00
|
|
|
match rs {
|
2012-08-26 18:54:31 -05:00
|
|
|
Ok(m) => {
|
2012-07-14 00:57:48 -05:00
|
|
|
assert (opt_present(m, ~"t"));
|
|
|
|
assert (opt_str(m, ~"t") == ~"20");
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => fail
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_optmulti_short_missing() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let args = ~[~"blah"];
|
|
|
|
let opts = ~[optmulti(~"t")];
|
2012-01-17 21:05:07 -06:00
|
|
|
let rs = getopts(args, opts);
|
2012-08-06 14:34:08 -05:00
|
|
|
match rs {
|
2012-08-26 18:54:31 -05:00
|
|
|
Ok(m) => assert (!opt_present(m, ~"t")),
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => fail
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_optmulti_short_no_arg() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let args = ~[~"-t"];
|
|
|
|
let opts = ~[optmulti(~"t")];
|
2012-01-17 21:05:07 -06:00
|
|
|
let rs = getopts(args, opts);
|
2012-08-06 14:34:08 -05:00
|
|
|
match rs {
|
2012-08-26 18:54:31 -05:00
|
|
|
Err(f) => check_fail_type(f, argument_missing_),
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => fail
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_optmulti_short_multi() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let args = ~[~"-t", ~"20", ~"-t", ~"30"];
|
|
|
|
let opts = ~[optmulti(~"t")];
|
2012-01-17 21:05:07 -06:00
|
|
|
let rs = getopts(args, opts);
|
2012-08-06 14:34:08 -05:00
|
|
|
match rs {
|
2012-08-26 18:54:31 -05:00
|
|
|
Ok(m) => {
|
2012-07-14 00:57:48 -05:00
|
|
|
assert (opt_present(m, ~"t"));
|
|
|
|
assert (opt_str(m, ~"t") == ~"20");
|
|
|
|
assert (opt_strs(m, ~"t")[0] == ~"20");
|
|
|
|
assert (opt_strs(m, ~"t")[1] == ~"30");
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => fail
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_unrecognized_option_long() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let args = ~[~"--untest"];
|
|
|
|
let opts = ~[optmulti(~"t")];
|
2012-01-17 21:05:07 -06:00
|
|
|
let rs = getopts(args, opts);
|
2012-08-06 14:34:08 -05:00
|
|
|
match rs {
|
2012-08-26 18:54:31 -05:00
|
|
|
Err(f) => check_fail_type(f, unrecognized_option_),
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => fail
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_unrecognized_option_short() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let args = ~[~"-t"];
|
|
|
|
let opts = ~[optmulti(~"test")];
|
2012-01-17 21:05:07 -06:00
|
|
|
let rs = getopts(args, opts);
|
2012-08-06 14:34:08 -05:00
|
|
|
match rs {
|
2012-08-26 18:54:31 -05:00
|
|
|
Err(f) => check_fail_type(f, unrecognized_option_),
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => fail
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_combined() {
|
|
|
|
let args =
|
2012-07-14 00:57:48 -05:00
|
|
|
~[~"prog", ~"free1", ~"-s", ~"20", ~"free2",
|
|
|
|
~"--flag", ~"--long=30", ~"-f", ~"-m", ~"40",
|
|
|
|
~"-m", ~"50", ~"-n", ~"-A B", ~"-n", ~"-60 70"];
|
2012-01-17 21:05:07 -06:00
|
|
|
let opts =
|
2012-07-14 00:57:48 -05:00
|
|
|
~[optopt(~"s"), optflag(~"flag"), reqopt(~"long"),
|
|
|
|
optflag(~"f"), optmulti(~"m"), optmulti(~"n"),
|
|
|
|
optopt(~"notpresent")];
|
2012-01-17 21:05:07 -06:00
|
|
|
let rs = getopts(args, opts);
|
2012-08-06 14:34:08 -05:00
|
|
|
match rs {
|
2012-08-26 18:54:31 -05:00
|
|
|
Ok(m) => {
|
2012-07-14 00:57:48 -05:00
|
|
|
assert (m.free[0] == ~"prog");
|
|
|
|
assert (m.free[1] == ~"free1");
|
|
|
|
assert (opt_str(m, ~"s") == ~"20");
|
|
|
|
assert (m.free[2] == ~"free2");
|
|
|
|
assert (opt_present(m, ~"flag"));
|
|
|
|
assert (opt_str(m, ~"long") == ~"30");
|
|
|
|
assert (opt_present(m, ~"f"));
|
|
|
|
assert (opt_strs(m, ~"m")[0] == ~"40");
|
|
|
|
assert (opt_strs(m, ~"m")[1] == ~"50");
|
|
|
|
assert (opt_strs(m, ~"n")[0] == ~"-A B");
|
|
|
|
assert (opt_strs(m, ~"n")[1] == ~"-60 70");
|
|
|
|
assert (!opt_present(m, ~"notpresent"));
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => fail
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-31 19:02:03 -05:00
|
|
|
#[test]
|
|
|
|
fn test_multi() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let args = ~[~"-e", ~"foo", ~"--encrypt", ~"foo"];
|
|
|
|
let opts = ~[optopt(~"e"), optopt(~"encrypt")];
|
2012-08-06 14:34:08 -05:00
|
|
|
let matches = match getopts(args, opts) {
|
2012-08-26 18:54:31 -05:00
|
|
|
result::Ok(m) => m,
|
2012-08-26 18:41:18 -05:00
|
|
|
result::Err(_f) => fail
|
2012-05-31 19:02:03 -05:00
|
|
|
};
|
2012-07-31 15:18:46 -05:00
|
|
|
assert opts_present(matches, ~[~"e"]);
|
|
|
|
assert opts_present(matches, ~[~"encrypt"]);
|
|
|
|
assert opts_present(matches, ~[~"encrypt", ~"e"]);
|
|
|
|
assert opts_present(matches, ~[~"e", ~"encrypt"]);
|
|
|
|
assert !opts_present(matches, ~[~"thing"]);
|
|
|
|
assert !opts_present(matches, ~[]);
|
2012-06-29 18:26:56 -05:00
|
|
|
|
2012-07-31 15:18:46 -05:00
|
|
|
assert opts_str(matches, ~[~"e"]) == ~"foo";
|
|
|
|
assert opts_str(matches, ~[~"encrypt"]) == ~"foo";
|
|
|
|
assert opts_str(matches, ~[~"e", ~"encrypt"]) == ~"foo";
|
|
|
|
assert opts_str(matches, ~[~"encrypt", ~"e"]) == ~"foo";
|
2012-05-31 19:02:03 -05:00
|
|
|
}
|
2012-07-04 10:36:31 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_nospace() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let args = ~[~"-Lfoo"];
|
|
|
|
let opts = ~[optmulti(~"L")];
|
2012-08-06 14:34:08 -05:00
|
|
|
let matches = match getopts(args, opts) {
|
2012-08-26 18:54:31 -05:00
|
|
|
result::Ok(m) => m,
|
2012-08-26 18:41:18 -05:00
|
|
|
result::Err(_f) => fail
|
2012-07-04 10:36:31 -05:00
|
|
|
};
|
2012-07-31 15:18:46 -05:00
|
|
|
assert opts_present(matches, ~[~"L"]);
|
|
|
|
assert opts_str(matches, ~[~"L"]) == ~"foo";
|
2012-07-04 10:36:31 -05:00
|
|
|
}
|
2012-01-17 21:05:07 -06: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
|
|
|
|
// End:
|