rust/src/libstd/getopts.rs

964 lines
27 KiB
Rust
Raw Normal View History

/*!
* 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.
*
* 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>) {
* // ...
* }
*
* fn print_usage(program: str) {
* io::println("Usage: " + program + " [options]");
* io::println("-o\t\tOutput");
* io::println("-h --help\tUsage");
* }
*
* fn main(args: ~[str]) {
* check vec::is_not_empty(args);
*
* let program : str = vec::head(args);
*
* let opts = ~[
* optopt("o"),
* optflag("h"),
* optflag("help")
* ];
2012-08-06 14:34:08 -05:00
* let matches = match getopts(vec::tail(args), opts) {
* 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") {
* print_usage(program);
2012-08-01 19:30:05 -05:00
* return;
* }
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]
* } else {
* print_usage(program);
2012-08-01 19:30:05 -05:00
* return;
* };
* do_work(input, output);
* }
*/
#[forbid(deprecated_mode)];
#[forbid(deprecated_pattern)];
2012-09-04 13:23:53 -05:00
use core::cmp::Eq;
use core::result::{Err, Ok};
use core::option;
use core::option::{Some, None};
2012-08-11 09:08:42 -05:00
export Opt;
export reqopt;
export optopt;
export optflag;
export optflagopt;
export optmulti;
export getopts;
2012-08-11 09:08:42 -05:00
export Matches;
export Fail_;
export fail_str;
export opt_present;
export opts_present;
export opt_str;
export opts_str;
export opt_strs;
export opt_maybe_str;
export opt_default;
2012-08-11 09:08:42 -05:00
export Result; //NDM
2012-08-27 18:26:35 -05:00
enum Name {
Long(~str),
Short(char),
}
2012-08-11 09:08:42 -05:00
enum HasArg { Yes, No, Maybe, }
2012-08-11 09:08:42 -05:00
enum Occur { Req, Optional, Multi, }
2011-04-26 10:46:54 -05:00
/// A description of a possible option
2012-08-11 09:08:42 -05:00
type Opt = {name: Name, hasarg: HasArg, occur: Occur};
2011-04-26 10:46:54 -05:00
2012-08-11 09:08:42 -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-11 09:08:42 -05:00
Short(str::char_at(unm, 0u))
} else { Long(unm) };
2011-04-26 10:46:54 -05:00
}
2012-08-27 18:26:35 -05:00
impl Name : Eq {
pure fn eq(&&other: Name) -> bool {
match self {
Long(e0a) => {
match other {
Long(e0b) => e0a == e0b,
_ => false
}
}
Short(e0a) => {
match other {
Short(e0b) => e0a == e0b,
_ => false
}
}
}
}
pure fn ne(&&other: Name) -> bool { !self.eq(other) }
2012-08-27 18:26:35 -05:00
}
impl Occur : Eq {
pure fn eq(&&other: Occur) -> bool {
(self as uint) == (other as uint)
}
pure fn ne(&&other: Occur) -> bool { !self.eq(other) }
2012-08-27 18:26:35 -05:00
}
/// Create an option that is required and takes an argument
2012-08-11 09:08:42 -05:00
fn reqopt(name: &str) -> Opt {
return {name: mkname(name), hasarg: Yes, occur: Req};
2011-04-26 10:46:54 -05:00
}
/// Create an option that is optional and takes an argument
2012-08-11 09:08:42 -05:00
fn optopt(name: &str) -> Opt {
return {name: mkname(name), hasarg: Yes, occur: Optional};
2011-04-26 10:46:54 -05:00
}
/// Create an option that is optional and does not take an argument
2012-08-11 09:08:42 -05:00
fn optflag(name: &str) -> Opt {
return {name: mkname(name), hasarg: No, occur: Optional};
2011-04-26 10:46:54 -05:00
}
/// Create an option that is optional and takes an optional argument
2012-08-11 09:08:42 -05:00
fn optflagopt(name: &str) -> Opt {
return {name: mkname(name), hasarg: Maybe, occur: Optional};
}
/**
* Create an option that is optional, takes an argument, and may occur
* multiple times
*/
2012-08-11 09:08:42 -05:00
fn optmulti(name: &str) -> Opt {
return {name: mkname(name), hasarg: Yes, occur: Multi};
2011-04-26 10:46:54 -05:00
}
2012-08-11 09:08:42 -05:00
enum Optval { Val(~str), Given, }
2011-04-26 10:46:54 -05:00
/**
* The result of checking command line arguments. Contains a vector
* of matches and a vector of free strings.
*/
2012-08-11 09:08:42 -05:00
type Matches = {opts: ~[Opt], vals: ~[~[Optval]], free: ~[~str]};
2011-04-26 10:46:54 -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
}
2012-08-11 09:08:42 -05:00
fn name_str(nm: &Name) -> ~str {
return match *nm {
2012-08-11 09:08:42 -05:00
Short(ch) => str::from_char(ch),
Long(s) => s
2012-08-03 21:59:04 -05:00
};
2011-04-26 10:46:54 -05:00
}
2012-08-11 09:08:42 -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
}
/**
* 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-08-11 09:08:42 -05:00
enum Fail_ {
ArgumentMissing(~str),
UnrecognizedOption(~str),
OptionMissing(~str),
OptionDuplicated(~str),
UnexpectedArgument(~str),
2011-04-26 10:46:54 -05:00
}
/// Convert a `fail_` enum into an error string
2012-08-11 09:08:42 -05:00
fn fail_str(+f: Fail_) -> ~str {
2012-08-06 14:34:08 -05:00
return match f {
2012-08-11 09:08:42 -05:00
ArgumentMissing(nm) => ~"Argument to option '" + nm + ~"' missing.",
UnrecognizedOption(nm) => ~"Unrecognized option: '" + nm + ~"'.",
OptionMissing(nm) => ~"Required option '" + nm + ~"' missing.",
OptionDuplicated(nm) => ~"Option '" + nm + ~"' given more than once.",
UnexpectedArgument(nm) => {
2012-08-03 21:59:04 -05:00
~"Option " + nm + ~" does not take an argument."
}
};
2011-04-26 10:46:54 -05:00
}
/**
* The result of parsing a command line with a set of options
2012-08-11 09:08:42 -05:00
* (result::t<Matches, Fail_>)
*/
2012-08-11 09:08:42 -05:00
type Result = result::Result<Matches, Fail_>;
2011-04-26 10:46:54 -05:00
/**
* Parse command line arguments according to the provided options
*
2012-08-11 09:08:42 -05:00
* 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-11 09:08:42 -05:00
fn getopts(args: &[~str], opts: &[Opt]) -> Result unsafe {
let n_opts = vec::len::<Opt>(opts);
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));
let mut free: ~[~str] = ~[];
let l = vec::len(args);
let mut i = 0u;
2011-07-27 07:19:39 -05:00
while i < l {
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) {
vec::push(free, cur);
} else if cur == ~"--" {
let mut j = i + 1u;
while j < l { vec::push(free, args[j]); j += 1u; }
2011-04-26 10:46:54 -05:00
break;
} else {
let mut names;
2012-08-20 14:23:37 -05:00
let mut i_arg = option::None::<~str>;
if cur[1] == '-' as u8 {
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-08-11 09:08:42 -05:00
names = ~[Long(tail)];
2011-04-26 10:46:54 -05:00
} else {
names =
2012-08-11 09:08:42 -05:00
~[Long(tail_eq[0])];
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 {
let mut j = 1u;
2012-08-20 14:23:37 -05:00
let mut last_valid_opt_id = option::None;
names = ~[];
2011-07-27 07:19:39 -05:00
while j < curlen {
let range = str::char_range_at(cur, j);
2012-08-11 09:08:42 -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 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-11 09:08:42 -05:00
Yes | Maybe => true,
No => false
};
if arg_follows && j + 1 < curlen {
2012-08-20 14:23:37 -05:00
i_arg = option::Some(str::slice(cur, j, curlen));
break;
} else {
2012-08-20 14:23:37 -05:00
last_valid_opt_id = option::None;
}
}
}
vec::push(names, opt);
j = range.next;
2011-04-26 10:46:54 -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-11 09:08:42 -05:00
None => return Err(UnrecognizedOption(name_str(&nm)))
};
2012-08-06 14:34:08 -05:00
match opts[optid].hasarg {
2012-08-11 09:08:42 -05:00
No => {
if !option::is_none::<~str>(i_arg) {
2012-08-11 09:08:42 -05:00
return Err(UnexpectedArgument(name_str(&nm)));
2011-04-26 10:46:54 -05:00
}
2012-08-11 09:08:42 -05:00
vec::push(vals[optid], Given);
2011-07-27 07:19:39 -05:00
}
2012-08-11 09:08:42 -05:00
Maybe => {
if !option::is_none::<~str>(i_arg) {
2012-08-11 09:08:42 -05:00
vec::push(vals[optid], Val(option::get(i_arg)));
} else if name_pos < vec::len::<Name>(names) ||
i + 1u == l || is_arg(args[i + 1u]) {
2012-08-11 09:08:42 -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-11 09:08:42 -05:00
Yes => {
if !option::is_none::<~str>(i_arg) {
vec::push(vals[optid],
2012-08-11 09:08:42 -05:00
Val(option::get::<~str>(i_arg)));
} else if i + 1u == l {
2012-08-11 09:08:42 -05:00
return Err(ArgumentMissing(name_str(&nm)));
} 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 {
2012-08-11 09:08:42 -05:00
let n = vec::len::<Optval>(vals[i]);
let occ = opts[i].occur;
2012-08-11 09:08:42 -05:00
if occ == Req {
2011-07-27 07:19:39 -05:00
if n == 0u {
2012-08-11 09:08:42 -05:00
return Err(OptionMissing(name_str(&(opts[i].name))));
}
}
2012-08-11 09:08:42 -05:00
if occ != Multi {
2011-07-27 07:19:39 -05:00
if n > 1u {
2012-08-11 09:08:42 -05:00
return Err(OptionDuplicated(name_str(&(opts[i].name))));
}
}
2011-04-26 10:46:54 -05:00
i += 1u;
}
return Ok({opts: vec::from_slice(opts),
vals: vec::from_mut(vals),
free: free});
2011-04-26 10:46:54 -05:00
}
2012-08-11 09:08:42 -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
}
2012-08-11 09:08:42 -05:00
fn opt_val(+mm: Matches, nm: &str) -> Optval { return opt_vals(mm, nm)[0]; }
/// Returns true if an option was matched
2012-08-11 09:08:42 -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
}
/// Returns true if any of several options were matched
2012-08-11 09:08:42 -05:00
fn opts_present(+mm: Matches, names: &[~str]) -> bool {
2012-06-30 18:19:07 -05:00
for vec::each(names) |nm| {
match find_opt(mm.opts, mkname(nm)) {
2012-08-20 14:23:37 -05:00
Some(_) => return true,
None => ()
}
}
2012-08-01 19:30:05 -05:00
return false;
}
/**
* 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-11 09:08:42 -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
}
/**
* 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-11 09:08:42 -05:00
fn opts_str(+mm: Matches, names: &[~str]) -> ~str {
2012-06-30 18:19:07 -05:00
for vec::each(names) |nm| {
match opt_val(mm, nm) {
2012-08-11 09:08:42 -05:00
Val(s) => return s,
2012-08-03 21:59:04 -05:00
_ => ()
}
}
fail;
}
/**
* Returns a vector of the arguments provided to all matches of the given
* option.
*
* Used when an option accepts multiple values.
*/
2012-08-11 09:08:42 -05:00
fn opt_strs(+mm: Matches, nm: &str) -> ~[~str] {
let mut acc: ~[~str] = ~[];
for vec::each(opt_vals(mm, nm)) |v| {
2012-08-11 09:08:42 -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
}
/// Returns the string argument supplied to a matching option or none
2012-08-11 09:08:42 -05:00
fn opt_maybe_str(+mm: Matches, nm: &str) -> Option<~str> {
let vals = opt_vals(mm, nm);
2012-08-11 09:08:42 -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
}
/**
* 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-11 09:08:42 -05:00
fn opt_default(+mm: Matches, nm: &str, def: &str) -> Option<~str> {
let vals = opt_vals(mm, nm);
2012-08-11 09:08:42 -05:00
if vec::len::<Optval>(vals) == 0u { return None::<~str>; }
return match vals[0] { Val(s) => Some::<~str>(s),
_ => Some::<~str>(str::from_slice(def)) }
}
2012-01-17 21:05:07 -06:00
2012-08-27 18:26:35 -05:00
enum FailType {
ArgumentMissing_,
UnrecognizedOption_,
OptionMissing_,
OptionDuplicated_,
UnexpectedArgument_,
}
impl FailType : Eq {
pure fn eq(&&other: FailType) -> bool {
(self as uint) == (other as uint)
}
pure fn ne(&&other: FailType) -> bool { !self.eq(other) }
2012-08-27 18:26:35 -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-08-11 09:08:42 -05:00
fn check_fail_type(+f: Fail_, ft: FailType) {
2012-08-06 14:34:08 -05:00
match f {
2012-08-11 09:08:42 -05:00
ArgumentMissing(_) => assert ft == ArgumentMissing_,
UnrecognizedOption(_) => assert ft == UnrecognizedOption_,
OptionMissing(_) => assert ft == OptionMissing_,
OptionDuplicated(_) => assert ft == OptionDuplicated_,
UnexpectedArgument(_) => assert ft == UnexpectedArgument_
2012-01-17 21:05:07 -06:00
}
}
// Tests for reqopt
#[test]
fn test_reqopt_long() {
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) => {
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() {
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-11 09:08:42 -05:00
Err(f) => check_fail_type(f, OptionMissing_),
2012-08-03 21:59:04 -05:00
_ => fail
2012-01-17 21:05:07 -06:00
}
}
#[test]
fn test_reqopt_long_no_arg() {
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-11 09:08:42 -05:00
Err(f) => check_fail_type(f, ArgumentMissing_),
2012-08-03 21:59:04 -05:00
_ => fail
2012-01-17 21:05:07 -06:00
}
}
#[test]
fn test_reqopt_long_multi() {
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-11 09:08:42 -05:00
Err(f) => check_fail_type(f, OptionDuplicated_),
2012-08-03 21:59:04 -05:00
_ => fail
2012-01-17 21:05:07 -06:00
}
}
#[test]
fn test_reqopt_short() {
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) => {
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() {
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-11 09:08:42 -05:00
Err(f) => check_fail_type(f, OptionMissing_),
2012-08-03 21:59:04 -05:00
_ => fail
2012-01-17 21:05:07 -06:00
}
}
#[test]
fn test_reqopt_short_no_arg() {
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-11 09:08:42 -05:00
Err(f) => check_fail_type(f, ArgumentMissing_),
2012-08-03 21:59:04 -05:00
_ => fail
2012-01-17 21:05:07 -06:00
}
}
#[test]
fn test_reqopt_short_multi() {
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-11 09:08:42 -05:00
Err(f) => check_fail_type(f, OptionDuplicated_),
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() {
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) => {
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() {
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() {
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-11 09:08:42 -05:00
Err(f) => check_fail_type(f, ArgumentMissing_),
2012-08-03 21:59:04 -05:00
_ => fail
2012-01-17 21:05:07 -06:00
}
}
#[test]
fn test_optopt_long_multi() {
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-11 09:08:42 -05:00
Err(f) => check_fail_type(f, OptionDuplicated_),
2012-08-03 21:59:04 -05:00
_ => fail
2012-01-17 21:05:07 -06:00
}
}
#[test]
fn test_optopt_short() {
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) => {
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() {
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() {
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-11 09:08:42 -05:00
Err(f) => check_fail_type(f, ArgumentMissing_),
2012-08-03 21:59:04 -05:00
_ => fail
2012-01-17 21:05:07 -06:00
}
}
#[test]
fn test_optopt_short_multi() {
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-11 09:08:42 -05:00
Err(f) => check_fail_type(f, OptionDuplicated_),
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() {
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() {
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() {
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));
2012-08-11 09:08:42 -05:00
check_fail_type(f, UnexpectedArgument_);
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_long_multi() {
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-11 09:08:42 -05:00
Err(f) => check_fail_type(f, OptionDuplicated_),
2012-08-03 21:59:04 -05:00
_ => fail
2012-01-17 21:05:07 -06:00
}
}
#[test]
fn test_optflag_short() {
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() {
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() {
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
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() {
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-11 09:08:42 -05:00
Err(f) => check_fail_type(f, OptionDuplicated_),
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() {
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) => {
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() {
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() {
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-11 09:08:42 -05:00
Err(f) => check_fail_type(f, ArgumentMissing_),
2012-08-03 21:59:04 -05:00
_ => fail
2012-01-17 21:05:07 -06:00
}
}
#[test]
fn test_optmulti_long_multi() {
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) => {
assert (opt_present(m, ~"test"));
assert (opt_str(m, ~"test") == ~"20");
let pair = opt_strs(m, ~"test");
assert (pair[0] == ~"20");
assert (pair[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() {
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) => {
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() {
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() {
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-11 09:08:42 -05:00
Err(f) => check_fail_type(f, ArgumentMissing_),
2012-08-03 21:59:04 -05:00
_ => fail
2012-01-17 21:05:07 -06:00
}
}
#[test]
fn test_optmulti_short_multi() {
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) => {
assert (opt_present(m, ~"t"));
assert (opt_str(m, ~"t") == ~"20");
let pair = opt_strs(m, ~"t");
assert (pair[0] == ~"20");
assert (pair[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() {
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-11 09:08:42 -05:00
Err(f) => check_fail_type(f, UnrecognizedOption_),
2012-08-03 21:59:04 -05:00
_ => fail
2012-01-17 21:05:07 -06:00
}
}
#[test]
fn test_unrecognized_option_short() {
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-11 09:08:42 -05:00
Err(f) => check_fail_type(f, UnrecognizedOption_),
2012-08-03 21:59:04 -05:00
_ => fail
2012-01-17 21:05:07 -06:00
}
}
#[test]
fn test_combined() {
let args =
~[~"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 =
~[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) => {
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"));
let pair = opt_strs(m, ~"m");
assert (pair[0] == ~"40");
assert (pair[1] == ~"50");
let pair = opt_strs(m, ~"n");
assert (pair[0] == ~"-A B");
assert (pair[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
}
}
#[test]
fn test_multi() {
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,
result::Err(_f) => fail
};
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-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";
}
#[test]
fn test_nospace() {
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,
result::Err(_f) => fail
};
2012-07-31 15:18:46 -05:00
assert opts_present(matches, ~[~"L"]);
assert opts_str(matches, ~[~"L"]) == ~"foo";
}
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: