2012-12-03 18:48:01 -06:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
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
|
2012-12-07 21:53:45 -06:00
|
|
|
* argument following either a space or an equals sign. Single-character
|
|
|
|
* options don't require the space.
|
2012-07-04 16:53:12 -05:00
|
|
|
*
|
|
|
|
* # 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);
|
|
|
|
* }
|
|
|
|
*/
|
2012-10-04 21:58:31 -05:00
|
|
|
#[forbid(deprecated_mode)];
|
2012-08-26 18:41:18 -05:00
|
|
|
|
2012-09-04 13:23:53 -05:00
|
|
|
use core::cmp::Eq;
|
2013-01-08 21:37:25 -06:00
|
|
|
use core::prelude::*;
|
2012-09-04 13:23:53 -05:00
|
|
|
use core::result::{Err, Ok};
|
2012-12-23 16:41:37 -06:00
|
|
|
use core::result;
|
2012-09-04 13:23:53 -05:00
|
|
|
use core::option;
|
|
|
|
use core::option::{Some, None};
|
2012-12-23 16:41:37 -06:00
|
|
|
use core::str;
|
|
|
|
use core::vec;
|
2011-05-21 18:30:04 -05:00
|
|
|
|
2012-12-11 17:16:36 -06:00
|
|
|
#[deriving_eq]
|
2013-01-08 21:37:25 -06:00
|
|
|
pub enum Name {
|
2012-08-27 18:26:35 -05:00
|
|
|
Long(~str),
|
|
|
|
Short(char),
|
|
|
|
}
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2012-12-11 17:16:36 -06:00
|
|
|
#[deriving_eq]
|
2013-01-08 21:37:25 -06:00
|
|
|
pub enum HasArg { Yes, No, Maybe, }
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2012-12-11 17:16:36 -06:00
|
|
|
#[deriving_eq]
|
2013-01-08 21:37:25 -06:00
|
|
|
pub 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
|
2012-12-11 17:16:36 -06:00
|
|
|
#[deriving_eq]
|
2012-12-11 19:03:22 -06:00
|
|
|
pub struct 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 {
|
2012-08-26 18:41:18 -05:00
|
|
|
let unm = str::from_slice(nm);
|
2012-10-11 18:54:31 -05:00
|
|
|
return if nm.len() == 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
|
|
|
}
|
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-10-03 15:37:51 -05:00
|
|
|
pub fn reqopt(name: &str) -> Opt {
|
2012-12-11 19:03:22 -06:00
|
|
|
return Opt {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-10-03 15:37:51 -05:00
|
|
|
pub fn optopt(name: &str) -> Opt {
|
2012-12-11 19:03:22 -06:00
|
|
|
return Opt {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-10-03 15:37:51 -05:00
|
|
|
pub fn optflag(name: &str) -> Opt {
|
2012-12-11 19:03:22 -06:00
|
|
|
return Opt {name: mkname(name), hasarg: No, occur: Optional};
|
2011-04-26 10:46:54 -05:00
|
|
|
}
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2012-11-17 21:43:39 -06:00
|
|
|
/// Create an option that is optional and does not take an argument
|
|
|
|
pub fn optflagmulti(name: &str) -> Opt {
|
2012-12-11 19:03:22 -06:00
|
|
|
return Opt {name: mkname(name), hasarg: No, occur: Multi};
|
2012-11-17 21:43:39 -06:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Create an option that is optional and takes an optional argument
|
2012-10-03 15:37:51 -05:00
|
|
|
pub fn optflagopt(name: &str) -> Opt {
|
2012-12-11 19:03:22 -06:00
|
|
|
return Opt {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-10-03 15:37:51 -05:00
|
|
|
pub fn optmulti(name: &str) -> Opt {
|
2012-12-11 19:03:22 -06:00
|
|
|
return Opt {name: mkname(name), hasarg: Yes, occur: Multi};
|
2011-04-26 10:46:54 -05:00
|
|
|
}
|
|
|
|
|
2012-12-11 17:16:36 -06:00
|
|
|
#[deriving_eq]
|
2012-08-11 09:08:42 -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-12-11 17:16:36 -06:00
|
|
|
#[deriving_eq]
|
2012-12-11 19:03:22 -06:00
|
|
|
pub struct 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-12-07 21:53:45 -06:00
|
|
|
return arg.len() > 1 && arg[0] == '-' as u8;
|
2011-04-26 10:46:54 -05:00
|
|
|
}
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2012-08-11 09:08:42 -05:00
|
|
|
fn name_str(nm: &Name) -> ~str {
|
2012-08-26 18:41:18 -05:00
|
|
|
return match *nm {
|
2012-08-11 09:08:42 -05:00
|
|
|
Short(ch) => str::from_char(ch),
|
2012-09-28 02:22:18 -05:00
|
|
|
Long(copy s) => s
|
2012-08-03 21:59:04 -05:00
|
|
|
};
|
2011-04-26 10:46:54 -05:00
|
|
|
}
|
|
|
|
|
2012-10-04 21:58:31 -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-12-11 17:16:36 -06:00
|
|
|
#[deriving_eq]
|
2012-10-03 15:37:51 -05:00
|
|
|
pub enum Fail_ {
|
2012-08-11 09:08:42 -05:00
|
|
|
ArgumentMissing(~str),
|
|
|
|
UnrecognizedOption(~str),
|
|
|
|
OptionMissing(~str),
|
|
|
|
OptionDuplicated(~str),
|
|
|
|
UnexpectedArgument(~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-10-03 14:21:48 -05:00
|
|
|
pub fn fail_str(f: Fail_) -> ~str {
|
2012-08-06 14:34:08 -05:00
|
|
|
return match f {
|
2012-09-28 04:26:20 -05:00
|
|
|
ArgumentMissing(ref nm) => {
|
|
|
|
~"Argument to option '" + *nm + ~"' missing."
|
|
|
|
}
|
|
|
|
UnrecognizedOption(ref nm) => {
|
|
|
|
~"Unrecognized option: '" + *nm + ~"'."
|
|
|
|
}
|
|
|
|
OptionMissing(ref nm) => {
|
|
|
|
~"Required option '" + *nm + ~"' missing."
|
|
|
|
}
|
|
|
|
OptionDuplicated(ref nm) => {
|
|
|
|
~"Option '" + *nm + ~"' given more than once."
|
|
|
|
}
|
|
|
|
UnexpectedArgument(ref nm) => {
|
|
|
|
~"Option " + *nm + ~" does not take an argument."
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
};
|
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-08-11 09:08:42 -05:00
|
|
|
* (result::t<Matches, Fail_>)
|
2012-07-04 16:53:12 -05:00
|
|
|
*/
|
2012-10-03 15:37:51 -05:00
|
|
|
pub 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
|
|
|
|
*
|
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.
|
2012-07-04 16:53:12 -05:00
|
|
|
* Use <fail_str> to get an error message.
|
|
|
|
*/
|
2013-01-23 13:43:58 -06:00
|
|
|
pub fn getopts(args: &[~str], opts: &[Opt]) -> Result {
|
|
|
|
unsafe {
|
|
|
|
let n_opts = opts.len();
|
|
|
|
fn f(_x: uint) -> ~[Optval] { return ~[]; }
|
|
|
|
let vals = vec::cast_to_mut(vec::from_fn(n_opts, f));
|
|
|
|
let mut free: ~[~str] = ~[];
|
|
|
|
let l = args.len();
|
|
|
|
let mut i = 0;
|
|
|
|
while i < l {
|
|
|
|
let cur = args[i];
|
|
|
|
let curlen = cur.len();
|
|
|
|
if !is_arg(cur) {
|
|
|
|
free.push(cur);
|
|
|
|
} else if cur == ~"--" {
|
|
|
|
let mut j = i + 1;
|
|
|
|
while j < l { free.push(args[j]); j += 1; }
|
|
|
|
break;
|
2011-04-26 10:46:54 -05:00
|
|
|
} else {
|
2013-01-23 13:43:58 -06:00
|
|
|
let mut names;
|
|
|
|
let mut i_arg = None;
|
|
|
|
if cur[1] == '-' as u8 {
|
|
|
|
let tail = str::slice(cur, 2, curlen);
|
|
|
|
let tail_eq = str::splitn_char(tail, '=', 1);
|
|
|
|
if tail_eq.len() <= 1 {
|
|
|
|
names = ~[Long(tail)];
|
|
|
|
} else {
|
|
|
|
names =
|
|
|
|
~[Long(tail_eq[0])];
|
|
|
|
i_arg = Some(tail_eq[1]);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let mut j = 1;
|
|
|
|
let mut last_valid_opt_id = None;
|
|
|
|
names = ~[];
|
|
|
|
while j < curlen {
|
|
|
|
let range = str::char_range_at(cur, j);
|
|
|
|
let opt = Short(range.ch);
|
2012-07-04 10:36:31 -05:00
|
|
|
|
2013-01-23 13:43:58 -06:00
|
|
|
/* 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 such as -L/usr/local/lib/foo to be
|
|
|
|
interpreted correctly
|
|
|
|
*/
|
2012-07-04 17:01:24 -05:00
|
|
|
|
2013-01-23 13:43:58 -06:00
|
|
|
match find_opt(opts, opt) {
|
|
|
|
Some(id) => last_valid_opt_id = Some(id),
|
|
|
|
None => {
|
|
|
|
let arg_follows =
|
|
|
|
last_valid_opt_id.is_some() &&
|
|
|
|
match opts[last_valid_opt_id.get()]
|
|
|
|
.hasarg {
|
2012-08-06 14:34:08 -05:00
|
|
|
|
2013-01-23 13:43:58 -06:00
|
|
|
Yes | Maybe => true,
|
|
|
|
No => false
|
|
|
|
};
|
|
|
|
if arg_follows && j < curlen {
|
|
|
|
i_arg = Some(cur.slice(j, curlen));
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
last_valid_opt_id = None;
|
|
|
|
}
|
|
|
|
}
|
2012-07-04 10:36:31 -05:00
|
|
|
}
|
2013-01-23 13:43:58 -06:00
|
|
|
names.push(opt);
|
|
|
|
j = range.next;
|
2012-07-04 10:36:31 -05:00
|
|
|
}
|
2011-04-26 10:46:54 -05:00
|
|
|
}
|
2013-01-23 13:43:58 -06:00
|
|
|
let mut name_pos = 0;
|
|
|
|
for names.each() |nm| {
|
|
|
|
name_pos += 1;
|
|
|
|
let optid = match find_opt(opts, *nm) {
|
|
|
|
Some(id) => id,
|
|
|
|
None => return Err(UnrecognizedOption(name_str(nm)))
|
|
|
|
};
|
|
|
|
match opts[optid].hasarg {
|
|
|
|
No => {
|
|
|
|
if !i_arg.is_none() {
|
|
|
|
return Err(UnexpectedArgument(name_str(nm)));
|
|
|
|
}
|
2012-09-26 19:33:34 -05:00
|
|
|
vals[optid].push(Given);
|
2013-01-23 13:43:58 -06:00
|
|
|
}
|
|
|
|
Maybe => {
|
|
|
|
if !i_arg.is_none() {
|
|
|
|
vals[optid].push(Val(i_arg.get()));
|
|
|
|
} else if name_pos < names.len() ||
|
|
|
|
i + 1 == l || is_arg(args[i + 1]) {
|
|
|
|
vals[optid].push(Given);
|
|
|
|
} else { i += 1; vals[optid].push(Val(args[i])); }
|
|
|
|
}
|
|
|
|
Yes => {
|
|
|
|
if !i_arg.is_none() {
|
|
|
|
vals[optid].push(Val(i_arg.get()));
|
|
|
|
} else if i + 1 == l {
|
|
|
|
return Err(ArgumentMissing(name_str(nm)));
|
|
|
|
} else { i += 1; vals[optid].push(Val(args[i])); }
|
|
|
|
}
|
|
|
|
}
|
2011-04-26 10:46:54 -05:00
|
|
|
}
|
|
|
|
}
|
2013-01-23 13:43:58 -06:00
|
|
|
i += 1;
|
2011-04-26 10:46:54 -05:00
|
|
|
}
|
2013-01-23 13:43:58 -06:00
|
|
|
i = 0u;
|
|
|
|
while i < n_opts {
|
|
|
|
let n = vals[i].len();
|
|
|
|
let occ = opts[i].occur;
|
|
|
|
if occ == Req {
|
|
|
|
if n == 0 {
|
|
|
|
return Err(OptionMissing(name_str(&(opts[i].name))));
|
|
|
|
}
|
2011-06-15 13:19:50 -05:00
|
|
|
}
|
2013-01-23 13:43:58 -06:00
|
|
|
if occ != Multi {
|
|
|
|
if n > 1 {
|
|
|
|
return Err(OptionDuplicated(name_str(&(opts[i].name))));
|
|
|
|
}
|
2011-06-15 13:19:50 -05:00
|
|
|
}
|
2013-01-23 13:43:58 -06:00
|
|
|
i += 1;
|
2011-06-15 13:19:50 -05:00
|
|
|
}
|
2013-01-23 13:43:58 -06:00
|
|
|
return Ok(Matches {opts: vec::from_slice(opts),
|
|
|
|
vals: vec::cast_from_mut(move vals),
|
|
|
|
free: free});
|
2011-04-26 10:46:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-24 14:49:31 -06:00
|
|
|
fn opt_vals(mm: &Matches, nm: &str) -> ~[Optval] {
|
2012-08-26 18:41:18 -05:00
|
|
|
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-11-24 14:49:31 -06: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-11-24 14:49:31 -06:00
|
|
|
pub fn opt_present(mm: &Matches, nm: &str) -> bool {
|
|
|
|
opt_vals(mm, nm).is_not_empty()
|
2011-04-26 10:46:54 -05:00
|
|
|
}
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2012-11-17 21:43:39 -06:00
|
|
|
/// Returns the number of times an option was matched
|
2012-11-24 14:49:31 -06:00
|
|
|
pub fn opt_count(mm: &Matches, nm: &str) -> uint {
|
|
|
|
opt_vals(mm, nm).len()
|
2012-11-17 21:43:39 -06:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Returns true if any of several options were matched
|
2012-11-24 14:49:31 -06:00
|
|
|
pub fn opts_present(mm: &Matches, names: &[~str]) -> bool {
|
2012-09-18 23:41:13 -05:00
|
|
|
for vec::each(names) |nm| {
|
2012-09-18 23:41:37 -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-11-24 14:49:31 -06:00
|
|
|
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-11-24 14:49:31 -06:00
|
|
|
pub fn opt_str(mm: &Matches, nm: &str) -> ~str {
|
2012-09-28 02:22:18 -05:00
|
|
|
return match opt_val(mm, nm) { Val(copy 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-11-24 14:49:31 -06:00
|
|
|
pub fn opts_str(mm: &Matches, names: &[~str]) -> ~str {
|
2012-09-18 23:41:13 -05:00
|
|
|
for vec::each(names) |nm| {
|
2012-09-18 23:41:37 -05:00
|
|
|
match opt_val(mm, *nm) {
|
2012-09-28 02:22:18 -05:00
|
|
|
Val(copy s) => return s,
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => ()
|
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-11-24 14:49:31 -06:00
|
|
|
pub fn opt_strs(mm: &Matches, nm: &str) -> ~[~str] {
|
2012-07-14 00:57:48 -05:00
|
|
|
let mut acc: ~[~str] = ~[];
|
2012-09-18 23:41:13 -05:00
|
|
|
for vec::each(opt_vals(mm, nm)) |v| {
|
2012-09-28 02:22:18 -05:00
|
|
|
match *v { Val(copy s) => acc.push(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-11-24 14:49:31 -06:00
|
|
|
pub fn opt_maybe_str(mm: &Matches, nm: &str) -> Option<~str> {
|
2012-08-26 18:41:18 -05:00
|
|
|
let vals = opt_vals(mm, nm);
|
2012-08-11 09:08:42 -05:00
|
|
|
if vec::len::<Optval>(vals) == 0u { return None::<~str>; }
|
2012-09-28 13:36:09 -05:00
|
|
|
return match vals[0] {
|
2012-09-28 00:20:47 -05:00
|
|
|
Val(copy s) => Some(s),
|
|
|
|
_ => None
|
2012-09-28 13:36:09 -05:00
|
|
|
};
|
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-11-24 14:49:31 -06:00
|
|
|
pub fn opt_default(mm: &Matches, nm: &str, def: &str) -> Option<~str> {
|
2012-08-26 18:41:18 -05:00
|
|
|
let vals = opt_vals(mm, nm);
|
2012-08-11 09:08:42 -05:00
|
|
|
if vec::len::<Optval>(vals) == 0u { return None::<~str>; }
|
2012-09-28 02:22:18 -05:00
|
|
|
return match vals[0] { Val(copy s) => Some::<~str>(s),
|
2012-08-26 18:41:18 -05:00
|
|
|
_ => Some::<~str>(str::from_slice(def)) }
|
2011-06-01 20:31:09 -05:00
|
|
|
}
|
2012-01-17 21:05:07 -06:00
|
|
|
|
2012-12-11 17:16:36 -06:00
|
|
|
#[deriving_eq]
|
2012-08-27 18:26:35 -05:00
|
|
|
enum FailType {
|
|
|
|
ArgumentMissing_,
|
|
|
|
UnrecognizedOption_,
|
|
|
|
OptionMissing_,
|
|
|
|
OptionDuplicated_,
|
|
|
|
UnexpectedArgument_,
|
|
|
|
}
|
|
|
|
|
2012-10-11 18:54:31 -05:00
|
|
|
/** A module which provides a way to specify descriptions and
|
|
|
|
* groups of short and long option names, together.
|
|
|
|
*/
|
|
|
|
pub mod groups {
|
2013-01-08 21:37:25 -06:00
|
|
|
use getopts::{HasArg, Long, Maybe, Multi, No, Occur, Opt, Optional, Req};
|
|
|
|
use getopts::{Result, Short, Yes};
|
2012-12-23 16:41:37 -06:00
|
|
|
|
2013-01-08 21:37:25 -06:00
|
|
|
use core::prelude::*;
|
2012-12-23 16:41:37 -06:00
|
|
|
use core::str;
|
|
|
|
use core::vec;
|
2012-10-11 18:54:31 -05:00
|
|
|
|
|
|
|
/** one group of options, e.g., both -h and --help, along with
|
|
|
|
* their shared description and properties
|
|
|
|
*/
|
2012-12-11 17:16:36 -06:00
|
|
|
#[deriving_eq]
|
2012-12-11 19:03:22 -06:00
|
|
|
pub struct OptGroup {
|
2012-10-11 18:54:31 -05:00
|
|
|
short_name: ~str,
|
|
|
|
long_name: ~str,
|
|
|
|
hint: ~str,
|
|
|
|
desc: ~str,
|
|
|
|
hasarg: HasArg,
|
|
|
|
occur: Occur
|
2012-12-11 19:03:22 -06:00
|
|
|
}
|
2012-10-11 18:54:31 -05:00
|
|
|
|
|
|
|
/// Create a long option that is required and takes an argument
|
|
|
|
pub fn reqopt(short_name: &str, long_name: &str,
|
|
|
|
desc: &str, hint: &str) -> OptGroup {
|
|
|
|
let len = short_name.len();
|
|
|
|
assert len == 1 || len == 0;
|
2012-12-11 19:03:22 -06:00
|
|
|
return OptGroup { short_name: str::from_slice(short_name),
|
2012-10-11 18:54:31 -05:00
|
|
|
long_name: str::from_slice(long_name),
|
|
|
|
hint: str::from_slice(hint),
|
|
|
|
desc: str::from_slice(desc),
|
|
|
|
hasarg: Yes,
|
|
|
|
occur: Req};
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create a long option that is optional and takes an argument
|
|
|
|
pub fn optopt(short_name: &str, long_name: &str,
|
|
|
|
desc: &str, hint: &str) -> OptGroup {
|
|
|
|
let len = short_name.len();
|
|
|
|
assert len == 1 || len == 0;
|
2012-12-11 19:03:22 -06:00
|
|
|
return OptGroup {short_name: str::from_slice(short_name),
|
2012-10-11 18:54:31 -05:00
|
|
|
long_name: str::from_slice(long_name),
|
|
|
|
hint: str::from_slice(hint),
|
|
|
|
desc: str::from_slice(desc),
|
|
|
|
hasarg: Yes,
|
|
|
|
occur: Optional};
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create a long option that is optional and does not take an argument
|
|
|
|
pub fn optflag(short_name: &str, long_name: &str,
|
|
|
|
desc: &str) -> OptGroup {
|
|
|
|
let len = short_name.len();
|
|
|
|
assert len == 1 || len == 0;
|
2012-12-11 19:03:22 -06:00
|
|
|
return OptGroup {short_name: str::from_slice(short_name),
|
2012-10-11 18:54:31 -05:00
|
|
|
long_name: str::from_slice(long_name),
|
|
|
|
hint: ~"",
|
|
|
|
desc: str::from_slice(desc),
|
|
|
|
hasarg: No,
|
|
|
|
occur: Optional};
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create a long option that is optional and takes an optional argument
|
|
|
|
pub fn optflagopt(short_name: &str, long_name: &str,
|
|
|
|
desc: &str, hint: &str) -> OptGroup {
|
|
|
|
let len = short_name.len();
|
|
|
|
assert len == 1 || len == 0;
|
2012-12-11 19:03:22 -06:00
|
|
|
return OptGroup {short_name: str::from_slice(short_name),
|
2012-10-11 18:54:31 -05:00
|
|
|
long_name: str::from_slice(long_name),
|
|
|
|
hint: str::from_slice(hint),
|
|
|
|
desc: str::from_slice(desc),
|
|
|
|
hasarg: Maybe,
|
|
|
|
occur: Optional};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a long option that is optional, takes an argument, and may occur
|
|
|
|
* multiple times
|
|
|
|
*/
|
|
|
|
pub fn optmulti(short_name: &str, long_name: &str,
|
|
|
|
desc: &str, hint: &str) -> OptGroup {
|
|
|
|
let len = short_name.len();
|
|
|
|
assert len == 1 || len == 0;
|
2012-12-11 19:03:22 -06:00
|
|
|
return OptGroup {short_name: str::from_slice(short_name),
|
2012-10-11 18:54:31 -05:00
|
|
|
long_name: str::from_slice(long_name),
|
|
|
|
hint: str::from_slice(hint),
|
|
|
|
desc: str::from_slice(desc),
|
|
|
|
hasarg: Yes,
|
|
|
|
occur: Multi};
|
|
|
|
}
|
|
|
|
|
|
|
|
// translate OptGroup into Opt
|
|
|
|
// (both short and long names correspond to different Opts)
|
|
|
|
pub fn long_to_short(lopt: &OptGroup) -> ~[Opt] {
|
|
|
|
match ((*lopt).short_name.len(),
|
|
|
|
(*lopt).long_name.len()) {
|
|
|
|
|
|
|
|
(0,0) => fail ~"this long-format option was given no name",
|
|
|
|
|
2012-12-11 19:03:22 -06:00
|
|
|
(0,_) => ~[Opt {name: Long(((*lopt).long_name)),
|
|
|
|
hasarg: (*lopt).hasarg,
|
|
|
|
occur: (*lopt).occur}],
|
2012-10-11 18:54:31 -05:00
|
|
|
|
2012-12-11 19:03:22 -06:00
|
|
|
(1,0) => ~[Opt {name: Short(str::char_at((*lopt).short_name, 0)),
|
|
|
|
hasarg: (*lopt).hasarg,
|
|
|
|
occur: (*lopt).occur}],
|
2012-10-11 18:54:31 -05:00
|
|
|
|
2012-12-11 19:03:22 -06:00
|
|
|
(1,_) => ~[Opt {name: Short(str::char_at((*lopt).short_name, 0)),
|
|
|
|
hasarg: (*lopt).hasarg,
|
|
|
|
occur: (*lopt).occur},
|
|
|
|
Opt {name: Long(((*lopt).long_name)),
|
|
|
|
hasarg: (*lopt).hasarg,
|
|
|
|
occur: (*lopt).occur}],
|
2012-10-11 18:54:31 -05:00
|
|
|
|
|
|
|
(_,_) => fail ~"something is wrong with the long-form opt"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Parse command line args with the provided long format options
|
|
|
|
*/
|
2013-01-08 21:37:25 -06:00
|
|
|
pub fn getopts(args: &[~str], opts: &[OptGroup]) -> ::getopts::Result {
|
2012-10-11 18:54:31 -05:00
|
|
|
::getopts::getopts(args, vec::flat_map(opts, long_to_short))
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Derive a usage message from a set of long options
|
|
|
|
*/
|
|
|
|
pub fn usage(brief: &str, opts: &[OptGroup]) -> ~str {
|
|
|
|
|
|
|
|
let desc_sep = ~"\n" + str::repeat(~" ", 24);
|
|
|
|
|
|
|
|
let rows = vec::map(opts, |optref| {
|
|
|
|
let short_name = (*optref).short_name;
|
|
|
|
let long_name = (*optref).long_name;
|
|
|
|
let hint = (*optref).hint;
|
|
|
|
let desc = (*optref).desc;
|
|
|
|
let hasarg = (*optref).hasarg;
|
|
|
|
|
|
|
|
let mut row = str::repeat(~" ", 4);
|
|
|
|
|
|
|
|
// short option
|
|
|
|
row += match short_name.len() {
|
|
|
|
0 => ~"",
|
|
|
|
1 => ~"-" + short_name + " ",
|
|
|
|
_ => fail ~"the short name should only be 1 char long",
|
|
|
|
};
|
|
|
|
|
|
|
|
// long option
|
|
|
|
row += match long_name.len() {
|
|
|
|
0 => ~"",
|
|
|
|
_ => ~"--" + long_name + " ",
|
|
|
|
};
|
|
|
|
|
|
|
|
// arg
|
|
|
|
row += match hasarg {
|
|
|
|
No => ~"",
|
|
|
|
Yes => hint,
|
|
|
|
Maybe => ~"[" + hint + ~"]",
|
|
|
|
};
|
|
|
|
|
|
|
|
// here we just need to indent the start of the description
|
|
|
|
let rowlen = row.len();
|
|
|
|
row += if rowlen < 24 {
|
|
|
|
str::repeat(~" ", 24 - rowlen)
|
|
|
|
} else {
|
|
|
|
desc_sep
|
|
|
|
};
|
|
|
|
|
|
|
|
// wrapped description
|
|
|
|
row += str::connect(str::split_within(desc, 54), desc_sep);
|
|
|
|
|
|
|
|
row
|
|
|
|
});
|
|
|
|
|
|
|
|
return str::from_slice(brief) +
|
|
|
|
~"\n\nOptions:\n" +
|
|
|
|
str::connect(rows, ~"\n") +
|
|
|
|
~"\n\n";
|
|
|
|
}
|
|
|
|
} // end groups module
|
|
|
|
|
2012-01-17 21:05:07 -06:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2012-09-21 20:10:45 -05:00
|
|
|
#[legacy_exports];
|
2013-01-08 21:37:25 -06:00
|
|
|
use core::prelude::*;
|
|
|
|
|
2012-09-07 20:08:21 -05:00
|
|
|
use opt = getopts;
|
2012-12-11 17:16:36 -06:00
|
|
|
use getopts::groups::OptGroup;
|
2013-01-08 21:37:25 -06:00
|
|
|
use getopts::*;
|
2012-12-27 20:24:18 -06:00
|
|
|
|
|
|
|
use core::result::{Err, Ok};
|
|
|
|
use core::result;
|
2012-01-17 21:05:07 -06:00
|
|
|
|
2012-10-03 14:21:48 -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() {
|
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-11-24 14:49:31 -06:00
|
|
|
Ok(ref 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-09-28 04:26:20 -05:00
|
|
|
Err(copy 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() {
|
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-09-28 04:26:20 -05:00
|
|
|
Err(copy 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() {
|
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-09-28 04:26:20 -05:00
|
|
|
Err(copy 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() {
|
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-11-24 14:49:31 -06:00
|
|
|
Ok(ref 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-09-28 04:26:20 -05:00
|
|
|
Err(copy 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() {
|
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-09-28 04:26:20 -05:00
|
|
|
Err(copy 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() {
|
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-09-28 04:26:20 -05:00
|
|
|
Err(copy 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() {
|
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-11-24 14:49:31 -06:00
|
|
|
Ok(ref 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-11-24 14:49:31 -06:00
|
|
|
Ok(ref 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-09-28 04:26:20 -05:00
|
|
|
Err(copy 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() {
|
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-09-28 04:26:20 -05:00
|
|
|
Err(copy 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() {
|
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-11-24 14:49:31 -06:00
|
|
|
Ok(ref 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-11-24 14:49:31 -06:00
|
|
|
Ok(ref 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-09-28 04:26:20 -05:00
|
|
|
Err(copy 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() {
|
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-09-28 04:26:20 -05:00
|
|
|
Err(copy 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() {
|
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-11-24 14:49:31 -06:00
|
|
|
Ok(ref 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-11-24 14:49:31 -06:00
|
|
|
Ok(ref 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-09-28 04:26:20 -05:00
|
|
|
Err(copy 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() {
|
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-09-28 04:26:20 -05:00
|
|
|
Err(copy 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() {
|
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-11-24 14:49:31 -06:00
|
|
|
Ok(ref 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-11-24 14:49:31 -06:00
|
|
|
Ok(ref 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-09-28 04:26:20 -05:00
|
|
|
Ok(ref 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-09-28 04:26:20 -05:00
|
|
|
Err(copy f) => check_fail_type(f, OptionDuplicated_),
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => fail
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-17 21:43:39 -06:00
|
|
|
// Tests for optflagmulti
|
|
|
|
#[test]
|
|
|
|
fn test_optflagmulti_short1() {
|
|
|
|
let args = ~[~"-v"];
|
|
|
|
let opts = ~[optflagmulti(~"v")];
|
|
|
|
let rs = getopts(args, opts);
|
|
|
|
match rs {
|
2012-11-24 14:49:31 -06:00
|
|
|
Ok(ref m) => {
|
2012-11-17 21:43:39 -06:00
|
|
|
assert (opt_count(m, ~"v") == 1);
|
|
|
|
}
|
|
|
|
_ => fail
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2012-11-18 17:00:43 -06:00
|
|
|
fn test_optflagmulti_short2a() {
|
2012-11-17 21:43:39 -06:00
|
|
|
let args = ~[~"-v", ~"-v"];
|
|
|
|
let opts = ~[optflagmulti(~"v")];
|
|
|
|
let rs = getopts(args, opts);
|
|
|
|
match rs {
|
2012-11-24 14:49:31 -06:00
|
|
|
Ok(ref m) => {
|
2012-11-17 21:43:39 -06:00
|
|
|
assert (opt_count(m, ~"v") == 2);
|
|
|
|
}
|
|
|
|
_ => fail
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_optflagmulti_short2b() {
|
|
|
|
let args = ~[~"-vv"];
|
|
|
|
let opts = ~[optflagmulti(~"v")];
|
|
|
|
let rs = getopts(args, opts);
|
|
|
|
match rs {
|
2012-11-24 14:49:31 -06:00
|
|
|
Ok(ref m) => {
|
2012-11-17 21:43:39 -06:00
|
|
|
assert (opt_count(m, ~"v") == 2);
|
|
|
|
}
|
|
|
|
_ => fail
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_optflagmulti_long1() {
|
|
|
|
let args = ~[~"--verbose"];
|
|
|
|
let opts = ~[optflagmulti(~"verbose")];
|
|
|
|
let rs = getopts(args, opts);
|
|
|
|
match rs {
|
2012-11-24 14:49:31 -06:00
|
|
|
Ok(ref m) => {
|
2012-11-17 21:43:39 -06:00
|
|
|
assert (opt_count(m, ~"verbose") == 1);
|
|
|
|
}
|
|
|
|
_ => fail
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_optflagmulti_long2() {
|
|
|
|
let args = ~[~"--verbose", ~"--verbose"];
|
|
|
|
let opts = ~[optflagmulti(~"verbose")];
|
|
|
|
let rs = getopts(args, opts);
|
|
|
|
match rs {
|
2012-11-24 14:49:31 -06:00
|
|
|
Ok(ref m) => {
|
2012-11-17 21:43:39 -06:00
|
|
|
assert (opt_count(m, ~"verbose") == 2);
|
|
|
|
}
|
|
|
|
_ => 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-11-24 14:49:31 -06:00
|
|
|
Ok(ref 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-11-24 14:49:31 -06:00
|
|
|
Ok(ref 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-09-28 04:26:20 -05:00
|
|
|
Err(copy 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() {
|
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-11-24 14:49:31 -06:00
|
|
|
Ok(ref m) => {
|
2012-08-28 17:54:45 -05:00
|
|
|
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() {
|
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-11-24 14:49:31 -06:00
|
|
|
Ok(ref 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-11-24 14:49:31 -06:00
|
|
|
Ok(ref 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-09-28 04:26:20 -05:00
|
|
|
Err(copy 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() {
|
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-11-24 14:49:31 -06:00
|
|
|
Ok(ref m) => {
|
2012-07-14 00:57:48 -05:00
|
|
|
assert (opt_present(m, ~"t"));
|
|
|
|
assert (opt_str(m, ~"t") == ~"20");
|
2012-08-28 17:54:45 -05:00
|
|
|
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() {
|
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-09-28 04:26:20 -05:00
|
|
|
Err(copy 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() {
|
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-09-28 04:26:20 -05:00
|
|
|
Err(copy 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 =
|
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-11-24 14:49:31 -06:00
|
|
|
Ok(ref 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"));
|
2012-08-28 17:54:45 -05:00
|
|
|
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");
|
2012-07-14 00:57:48 -05:00
|
|
|
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-11-24 14:49:31 -06:00
|
|
|
let matches = &match getopts(args, opts) {
|
2012-09-28 04:26:20 -05:00
|
|
|
result::Ok(move m) => m,
|
|
|
|
result::Err(_) => 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-12-07 21:53:45 -06:00
|
|
|
let args = ~[~"-Lfoo", ~"-M."];
|
|
|
|
let opts = ~[optmulti(~"L"), optmulti(~"M")];
|
2012-11-24 14:49:31 -06:00
|
|
|
let matches = &match getopts(args, opts) {
|
2012-09-28 04:26:20 -05:00
|
|
|
result::Ok(move m) => m,
|
|
|
|
result::Err(_) => 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-12-07 21:53:45 -06:00
|
|
|
assert opts_present(matches, ~[~"M"]);
|
|
|
|
assert opts_str(matches, ~[~"M"]) == ~".";
|
|
|
|
|
2012-07-04 10:36:31 -05:00
|
|
|
}
|
2012-10-11 18:54:31 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_groups_reqopt() {
|
|
|
|
let opt = groups::reqopt(~"b", ~"banana", ~"some bananas", ~"VAL");
|
2012-12-11 19:03:22 -06:00
|
|
|
assert opt == OptGroup { short_name: ~"b",
|
2012-10-11 18:54:31 -05:00
|
|
|
long_name: ~"banana",
|
|
|
|
hint: ~"VAL",
|
|
|
|
desc: ~"some bananas",
|
|
|
|
hasarg: Yes,
|
|
|
|
occur: Req }
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_groups_optopt() {
|
|
|
|
let opt = groups::optopt(~"a", ~"apple", ~"some apples", ~"VAL");
|
2012-12-11 19:03:22 -06:00
|
|
|
assert opt == OptGroup { short_name: ~"a",
|
2012-10-11 18:54:31 -05:00
|
|
|
long_name: ~"apple",
|
|
|
|
hint: ~"VAL",
|
|
|
|
desc: ~"some apples",
|
|
|
|
hasarg: Yes,
|
|
|
|
occur: Optional }
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_groups_optflag() {
|
|
|
|
let opt = groups::optflag(~"k", ~"kiwi", ~"some kiwis");
|
2012-12-11 19:03:22 -06:00
|
|
|
assert opt == OptGroup { short_name: ~"k",
|
2012-10-11 18:54:31 -05:00
|
|
|
long_name: ~"kiwi",
|
|
|
|
hint: ~"",
|
|
|
|
desc: ~"some kiwis",
|
|
|
|
hasarg: No,
|
|
|
|
occur: Optional }
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_groups_optflagopt() {
|
|
|
|
let opt = groups::optflagopt(~"p", ~"pineapple",
|
|
|
|
~"some pineapples", ~"VAL");
|
2012-12-11 19:03:22 -06:00
|
|
|
assert opt == OptGroup { short_name: ~"p",
|
2012-10-11 18:54:31 -05:00
|
|
|
long_name: ~"pineapple",
|
|
|
|
hint: ~"VAL",
|
|
|
|
desc: ~"some pineapples",
|
|
|
|
hasarg: Maybe,
|
|
|
|
occur: Optional }
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_groups_optmulti() {
|
|
|
|
let opt = groups::optmulti(~"l", ~"lime",
|
|
|
|
~"some limes", ~"VAL");
|
2012-12-11 19:03:22 -06:00
|
|
|
assert opt == OptGroup { short_name: ~"l",
|
2012-10-11 18:54:31 -05:00
|
|
|
long_name: ~"lime",
|
|
|
|
hint: ~"VAL",
|
|
|
|
desc: ~"some limes",
|
|
|
|
hasarg: Yes,
|
|
|
|
occur: Multi }
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_groups_long_to_short() {
|
|
|
|
let short = ~[reqopt(~"b"), reqopt(~"banana")];
|
|
|
|
let verbose = groups::reqopt(~"b", ~"banana",
|
|
|
|
~"some bananas", ~"VAL");
|
|
|
|
|
|
|
|
assert groups::long_to_short(&verbose) == short;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_groups_getopts() {
|
|
|
|
let short = ~[
|
|
|
|
reqopt(~"b"), reqopt(~"banana"),
|
|
|
|
optopt(~"a"), optopt(~"apple"),
|
|
|
|
optflag(~"k"), optflagopt(~"kiwi"),
|
|
|
|
optflagopt(~"p"),
|
|
|
|
optmulti(~"l")
|
|
|
|
];
|
|
|
|
|
|
|
|
let verbose = ~[
|
|
|
|
groups::reqopt(~"b", ~"banana", ~"Desc", ~"VAL"),
|
|
|
|
groups::optopt(~"a", ~"apple", ~"Desc", ~"VAL"),
|
|
|
|
groups::optflag(~"k", ~"kiwi", ~"Desc"),
|
|
|
|
groups::optflagopt(~"p", ~"", ~"Desc", ~"VAL"),
|
|
|
|
groups::optmulti(~"l", ~"", ~"Desc", ~"VAL"),
|
|
|
|
];
|
|
|
|
|
|
|
|
let sample_args = ~[~"-k", ~"15", ~"--apple", ~"1", ~"k",
|
|
|
|
~"-p", ~"16", ~"l", ~"35"];
|
|
|
|
|
|
|
|
// NOTE: we should sort before comparing
|
|
|
|
assert getopts(sample_args, short)
|
|
|
|
== groups::getopts(sample_args, verbose);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_groups_usage() {
|
|
|
|
let optgroups = ~[
|
|
|
|
groups::reqopt(~"b", ~"banana", ~"Desc", ~"VAL"),
|
|
|
|
groups::optopt(~"a", ~"012345678901234567890123456789",
|
|
|
|
~"Desc", ~"VAL"),
|
|
|
|
groups::optflag(~"k", ~"kiwi", ~"Desc"),
|
|
|
|
groups::optflagopt(~"p", ~"", ~"Desc", ~"VAL"),
|
|
|
|
groups::optmulti(~"l", ~"", ~"Desc", ~"VAL"),
|
|
|
|
];
|
|
|
|
|
|
|
|
let expected =
|
|
|
|
~"Usage: fruits
|
|
|
|
|
|
|
|
Options:
|
|
|
|
-b --banana VAL Desc
|
|
|
|
-a --012345678901234567890123456789 VAL
|
|
|
|
Desc
|
|
|
|
-k --kiwi Desc
|
|
|
|
-p [VAL] Desc
|
|
|
|
-l VAL Desc
|
|
|
|
|
|
|
|
";
|
|
|
|
|
|
|
|
let generated_usage = groups::usage(~"Usage: fruits", optgroups);
|
|
|
|
|
|
|
|
debug!("expected: <<%s>>", expected);
|
|
|
|
debug!("generated: <<%s>>", generated_usage);
|
|
|
|
assert generated_usage == expected;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_groups_usage_description_wrapping() {
|
|
|
|
// indentation should be 24 spaces
|
|
|
|
// lines wrap after 78: or rather descriptions wrap after 54
|
|
|
|
|
|
|
|
let optgroups = ~[
|
|
|
|
groups::optflag(~"k", ~"kiwi",
|
|
|
|
~"This is a long description which won't be wrapped..+.."), // 54
|
|
|
|
groups::optflag(~"a", ~"apple",
|
|
|
|
~"This is a long description which _will_ be wrapped..+.."), // 55
|
|
|
|
];
|
|
|
|
|
|
|
|
let expected =
|
|
|
|
~"Usage: fruits
|
|
|
|
|
|
|
|
Options:
|
|
|
|
-k --kiwi This is a long description which won't be wrapped..+..
|
|
|
|
-a --apple This is a long description which _will_ be
|
|
|
|
wrapped..+..
|
|
|
|
|
|
|
|
";
|
|
|
|
|
|
|
|
let usage = groups::usage(~"Usage: fruits", optgroups);
|
|
|
|
|
|
|
|
debug!("expected: <<%s>>", expected);
|
|
|
|
debug!("generated: <<%s>>", usage);
|
|
|
|
assert usage == expected
|
|
|
|
}
|
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:
|