//! }
//! let output = matches.opt_str("o");
//! let input: &str = if !matches.free.is_empty() {
-//! matches.free[0].clone()
+//! (*matches.free.get(0)).clone()
//! } else {
//! print_usage(program, opts);
//! return;
use std::cmp::Eq;
use std::result::{Err, Ok};
use std::result;
-use std::option::{Some, None};
-use std::slice;
/// Name of an option. Either a string or a single char.
#[deriving(Clone, Eq)]
/// How often it can occur
occur: Occur,
/// Which options it aliases
- priv aliases: ~[Opt],
+ priv aliases: Vec<Opt> ,
}
/// One group of options, e.g., both -h and --help, along with
#[deriving(Clone, Eq)]
pub struct Matches {
/// Options that matched
- priv opts: ~[Opt],
+ priv opts: Vec<Opt> ,
/// Values of the Options that matched
- priv vals: ~[~[Optval]],
+ priv vals: Vec<Vec<Optval> > ,
/// Free string fragments
- free: ~[~str]
-}
+ free: Vec<~str> }
/// The type returned when the command line does not conform to the
/// expected format. Call the `to_err_msg` method to retrieve the
name: Long((long_name)),
hasarg: hasarg,
occur: occur,
- aliases: ~[]
+ aliases: Vec::new()
},
(1,0) => Opt {
name: Short(short_name.char_at(0)),
hasarg: hasarg,
occur: occur,
- aliases: ~[]
+ aliases: Vec::new()
},
(1,_) => Opt {
name: Long((long_name)),
hasarg: hasarg,
occur: occur,
- aliases: ~[
+ aliases: vec!(
Opt {
name: Short(short_name.char_at(0)),
hasarg: hasarg,
occur: occur,
- aliases: ~[]
+ aliases: Vec::new()
}
- ]
+ )
},
(_,_) => fail!("something is wrong with the long-form opt")
}
}
impl Matches {
- fn opt_vals(&self, nm: &str) -> ~[Optval] {
- match find_opt(self.opts, Name::from_str(nm)) {
- Some(id) => self.vals[id].clone(),
+ fn opt_vals(&self, nm: &str) -> Vec<Optval> {
+ match find_opt(self.opts.as_slice(), Name::from_str(nm)) {
+ Some(id) => (*self.vals.get(id)).clone(),
None => fail!("No option '{}' defined", nm)
}
}
if vals.is_empty() {
None
} else {
- Some(vals[0].clone())
+ Some((*vals.get(0)).clone())
}
}
/// Returns true if any of several options were matched.
pub fn opts_present(&self, names: &[~str]) -> bool {
for nm in names.iter() {
- match find_opt(self.opts, Name::from_str(*nm)) {
- Some(id) if !self.vals[id].is_empty() => return true,
+ match find_opt(self.opts.as_slice(), Name::from_str(*nm)) {
+ Some(id) if !self.vals.get(id).is_empty() => return true,
_ => (),
};
}
/// option.
///
/// Used when an option accepts multiple values.
- pub fn opt_strs(&self, nm: &str) -> ~[~str] {
- let mut acc: ~[~str] = ~[];
+ pub fn opt_strs(&self, nm: &str) -> Vec<~str> {
+ let mut acc: Vec<~str> = Vec::new();
let r = self.opt_vals(nm);
for v in r.iter() {
match *v {
if vals.is_empty() {
return None::<~str>;
}
- match vals[0] {
- Val(ref s) => Some((*s).clone()),
+ match vals.get(0) {
+ &Val(ref s) => Some((*s).clone()),
_ => None
}
}
pub fn opt_default(&self, nm: &str, def: &str) -> Option<~str> {
let vals = self.opt_vals(nm);
if vals.is_empty() { return None; }
- match vals[0] {
- Val(ref s) => Some((*s).clone()),
+ match vals.get(0) {
+ &Val(ref s) => Some((*s).clone()),
_ => Some(def.to_owned())
}
}
let opts = optgrps.map(|x| x.long_to_short());
let n_opts = opts.len();
- fn f(_x: uint) -> ~[Optval] { return ~[]; }
+ fn f(_x: uint) -> Vec<Optval> { return Vec::new(); }
- let mut vals = slice::from_fn(n_opts, f);
- let mut free: ~[~str] = ~[];
+ let mut vals = Vec::from_fn(n_opts, f);
+ let mut free: Vec<~str> = Vec::new();
let l = args.len();
let mut i = 0;
while i < l {
let mut i_arg = None;
if cur[1] == '-' as u8 {
let tail = cur.slice(2, curlen);
- let tail_eq: ~[&str] = tail.split('=').collect();
+ let tail_eq: Vec<&str> = tail.split('=').collect();
if tail_eq.len() <= 1 {
- names = ~[Long(tail.to_owned())];
+ names = vec!(Long(tail.to_owned()));
} else {
names =
- ~[Long(tail_eq[0].to_owned())];
- i_arg = Some(tail_eq[1].to_owned());
+ vec!(Long((*tail_eq.get(0)).to_owned()));
+ i_arg = Some((*tail_eq.get(1)).to_owned());
}
} else {
let mut j = 1;
let mut last_valid_opt_id = None;
- names = ~[];
+ names = Vec::new();
while j < curlen {
let range = cur.char_range_at(j);
let opt = Short(range.ch);
if !i_arg.is_none() {
return Err(UnexpectedArgument(nm.to_str()));
}
- vals[optid].push(Given);
+ vals.get_mut(optid).push(Given);
}
Maybe => {
if !i_arg.is_none() {
- vals[optid].push(Val((i_arg.clone()).unwrap()));
+ vals.get_mut(optid)
+ .push(Val((i_arg.clone())
+ .unwrap()));
} 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].clone())); }
+ vals.get_mut(optid).push(Given);
+ } else {
+ i += 1;
+ vals.get_mut(optid).push(Val(args[i].clone()));
+ }
}
Yes => {
if !i_arg.is_none() {
- vals[optid].push(Val(i_arg.clone().unwrap()));
+ vals.get_mut(optid).push(Val(i_arg.clone().unwrap()));
} else if i + 1 == l {
return Err(ArgumentMissing(nm.to_str()));
- } else { i += 1; vals[optid].push(Val(args[i].clone())); }
+ } else {
+ i += 1;
+ vals.get_mut(optid).push(Val(args[i].clone()));
+ }
}
}
}
}
i = 0u;
while i < n_opts {
- let n = vals[i].len();
+ let n = vals.get(i).len();
let occ = opts[i].occur;
if occ == Req {
if n == 0 {
i += 1;
}
Ok(Matches {
- opts: opts.to_owned(),
+ opts: Vec::from_slice(opts),
vals: vals,
free: free
})
}
// FIXME: #5516 should be graphemes not codepoints
- let mut desc_rows = ~[];
+ let mut desc_rows = Vec::new();
each_split_within(desc_normalized_whitespace, 54, |substr| {
desc_rows.push(substr.to_owned());
true
row
});
- format!("{}\n\nOptions:\n{}\n", brief, rows.collect::<~[~str]>().connect("\n"))
+ format!("{}\n\nOptions:\n{}\n", brief, rows.collect::<Vec<~str> >().connect("\n"))
}
fn format_option(opt: &OptGroup) -> ~str {
#[test]
fn test_split_within() {
fn t(s: &str, i: uint, u: &[~str]) {
- let mut v = ~[];
+ let mut v = Vec::new();
each_split_within(s, i, |s| { v.push(s.to_owned()); true });
assert!(v.iter().zip(u.iter()).all(|(a,b)| a == b));
}
use std::result::{Err, Ok};
use std::result;
+ use std::vec_ng::Vec;
fn check_fail_type(f: Fail_, ft: FailType) {
match f {
// Tests for reqopt
#[test]
fn test_reqopt() {
- let long_args = ~[~"--test=20"];
- let opts = ~[reqopt("t", "test", "testing", "TEST")];
- let rs = getopts(long_args, opts);
+ let long_args = vec!(~"--test=20");
+ let opts = vec!(reqopt("t", "test", "testing", "TEST"));
+ let rs = getopts(long_args.as_slice(), opts.as_slice());
match rs {
Ok(ref m) => {
assert!(m.opt_present("test"));
}
_ => { fail!("test_reqopt failed (long arg)"); }
}
- let short_args = ~[~"-t", ~"20"];
- match getopts(short_args, opts) {
+ let short_args = vec!(~"-t", ~"20");
+ match getopts(short_args.as_slice(), opts.as_slice()) {
Ok(ref m) => {
assert!((m.opt_present("test")));
assert_eq!(m.opt_str("test").unwrap(), ~"20");
#[test]
fn test_reqopt_missing() {
- let args = ~[~"blah"];
- let opts = ~[reqopt("t", "test", "testing", "TEST")];
- let rs = getopts(args, opts);
+ let args = vec!(~"blah");
+ let opts = vec!(reqopt("t", "test", "testing", "TEST"));
+ let rs = getopts(args.as_slice(), opts.as_slice());
match rs {
Err(f) => check_fail_type(f, OptionMissing_),
_ => fail!()
#[test]
fn test_reqopt_no_arg() {
- let long_args = ~[~"--test"];
- let opts = ~[reqopt("t", "test", "testing", "TEST")];
- let rs = getopts(long_args, opts);
+ let long_args = vec!(~"--test");
+ let opts = vec!(reqopt("t", "test", "testing", "TEST"));
+ let rs = getopts(long_args.as_slice(), opts.as_slice());
match rs {
Err(f) => check_fail_type(f, ArgumentMissing_),
_ => fail!()
}
- let short_args = ~[~"-t"];
- match getopts(short_args, opts) {
+ let short_args = vec!(~"-t");
+ match getopts(short_args.as_slice(), opts.as_slice()) {
Err(f) => check_fail_type(f, ArgumentMissing_),
_ => fail!()
}
#[test]
fn test_reqopt_multi() {
- let args = ~[~"--test=20", ~"-t", ~"30"];
- let opts = ~[reqopt("t", "test", "testing", "TEST")];
- let rs = getopts(args, opts);
+ let args = vec!(~"--test=20", ~"-t", ~"30");
+ let opts = vec!(reqopt("t", "test", "testing", "TEST"));
+ let rs = getopts(args.as_slice(), opts.as_slice());
match rs {
Err(f) => check_fail_type(f, OptionDuplicated_),
_ => fail!()
// Tests for optopt
#[test]
fn test_optopt() {
- let long_args = ~[~"--test=20"];
- let opts = ~[optopt("t", "test", "testing", "TEST")];
- let rs = getopts(long_args, opts);
+ let long_args = vec!(~"--test=20");
+ let opts = vec!(optopt("t", "test", "testing", "TEST"));
+ let rs = getopts(long_args.as_slice(), opts.as_slice());
match rs {
Ok(ref m) => {
assert!(m.opt_present("test"));
}
_ => fail!()
}
- let short_args = ~[~"-t", ~"20"];
- match getopts(short_args, opts) {
+ let short_args = vec!(~"-t", ~"20");
+ match getopts(short_args.as_slice(), opts.as_slice()) {
Ok(ref m) => {
assert!((m.opt_present("test")));
assert_eq!(m.opt_str("test").unwrap(), ~"20");
#[test]
fn test_optopt_missing() {
- let args = ~[~"blah"];
- let opts = ~[optopt("t", "test", "testing", "TEST")];
- let rs = getopts(args, opts);
+ let args = vec!(~"blah");
+ let opts = vec!(optopt("t", "test", "testing", "TEST"));
+ let rs = getopts(args.as_slice(), opts.as_slice());
match rs {
Ok(ref m) => {
assert!(!m.opt_present("test"));
#[test]
fn test_optopt_no_arg() {
- let long_args = ~[~"--test"];
- let opts = ~[optopt("t", "test", "testing", "TEST")];
- let rs = getopts(long_args, opts);
+ let long_args = vec!(~"--test");
+ let opts = vec!(optopt("t", "test", "testing", "TEST"));
+ let rs = getopts(long_args.as_slice(), opts.as_slice());
match rs {
Err(f) => check_fail_type(f, ArgumentMissing_),
_ => fail!()
}
- let short_args = ~[~"-t"];
- match getopts(short_args, opts) {
+ let short_args = vec!(~"-t");
+ match getopts(short_args.as_slice(), opts.as_slice()) {
Err(f) => check_fail_type(f, ArgumentMissing_),
_ => fail!()
}
#[test]
fn test_optopt_multi() {
- let args = ~[~"--test=20", ~"-t", ~"30"];
- let opts = ~[optopt("t", "test", "testing", "TEST")];
- let rs = getopts(args, opts);
+ let args = vec!(~"--test=20", ~"-t", ~"30");
+ let opts = vec!(optopt("t", "test", "testing", "TEST"));
+ let rs = getopts(args.as_slice(), opts.as_slice());
match rs {
Err(f) => check_fail_type(f, OptionDuplicated_),
_ => fail!()
// Tests for optflag
#[test]
fn test_optflag() {
- let long_args = ~[~"--test"];
- let opts = ~[optflag("t", "test", "testing")];
- let rs = getopts(long_args, opts);
+ let long_args = vec!(~"--test");
+ let opts = vec!(optflag("t", "test", "testing"));
+ let rs = getopts(long_args.as_slice(), opts.as_slice());
match rs {
Ok(ref m) => {
assert!(m.opt_present("test"));
}
_ => fail!()
}
- let short_args = ~[~"-t"];
- match getopts(short_args, opts) {
+ let short_args = vec!(~"-t");
+ match getopts(short_args.as_slice(), opts.as_slice()) {
Ok(ref m) => {
assert!(m.opt_present("test"));
assert!(m.opt_present("t"));
#[test]
fn test_optflag_missing() {
- let args = ~[~"blah"];
- let opts = ~[optflag("t", "test", "testing")];
- let rs = getopts(args, opts);
+ let args = vec!(~"blah");
+ let opts = vec!(optflag("t", "test", "testing"));
+ let rs = getopts(args.as_slice(), opts.as_slice());
match rs {
Ok(ref m) => {
assert!(!m.opt_present("test"));
#[test]
fn test_optflag_long_arg() {
- let args = ~[~"--test=20"];
- let opts = ~[optflag("t", "test", "testing")];
- let rs = getopts(args, opts);
+ let args = vec!(~"--test=20");
+ let opts = vec!(optflag("t", "test", "testing"));
+ let rs = getopts(args.as_slice(), opts.as_slice());
match rs {
Err(f) => {
error!("{:?}", f.clone().to_err_msg());
#[test]
fn test_optflag_multi() {
- let args = ~[~"--test", ~"-t"];
- let opts = ~[optflag("t", "test", "testing")];
- let rs = getopts(args, opts);
+ let args = vec!(~"--test", ~"-t");
+ let opts = vec!(optflag("t", "test", "testing"));
+ let rs = getopts(args.as_slice(), opts.as_slice());
match rs {
Err(f) => check_fail_type(f, OptionDuplicated_),
_ => fail!()
#[test]
fn test_optflag_short_arg() {
- let args = ~[~"-t", ~"20"];
- let opts = ~[optflag("t", "test", "testing")];
- let rs = getopts(args, opts);
+ let args = vec!(~"-t", ~"20");
+ let opts = vec!(optflag("t", "test", "testing"));
+ let rs = getopts(args.as_slice(), opts.as_slice());
match rs {
Ok(ref m) => {
// The next variable after the flag is just a free argument
- assert!(m.free[0] == ~"20");
+ assert!(*m.free.get(0) == ~"20");
}
_ => fail!()
}
// Tests for optflagmulti
#[test]
fn test_optflagmulti_short1() {
- let args = ~[~"-v"];
- let opts = ~[optflagmulti("v", "verbose", "verbosity")];
- let rs = getopts(args, opts);
+ let args = vec!(~"-v");
+ let opts = vec!(optflagmulti("v", "verbose", "verbosity"));
+ let rs = getopts(args.as_slice(), opts.as_slice());
match rs {
Ok(ref m) => {
assert_eq!(m.opt_count("v"), 1);
#[test]
fn test_optflagmulti_short2a() {
- let args = ~[~"-v", ~"-v"];
- let opts = ~[optflagmulti("v", "verbose", "verbosity")];
- let rs = getopts(args, opts);
+ let args = vec!(~"-v", ~"-v");
+ let opts = vec!(optflagmulti("v", "verbose", "verbosity"));
+ let rs = getopts(args.as_slice(), opts.as_slice());
match rs {
Ok(ref m) => {
assert_eq!(m.opt_count("v"), 2);
#[test]
fn test_optflagmulti_short2b() {
- let args = ~[~"-vv"];
- let opts = ~[optflagmulti("v", "verbose", "verbosity")];
- let rs = getopts(args, opts);
+ let args = vec!(~"-vv");
+ let opts = vec!(optflagmulti("v", "verbose", "verbosity"));
+ let rs = getopts(args.as_slice(), opts.as_slice());
match rs {
Ok(ref m) => {
assert_eq!(m.opt_count("v"), 2);
#[test]
fn test_optflagmulti_long1() {
- let args = ~[~"--verbose"];
- let opts = ~[optflagmulti("v", "verbose", "verbosity")];
- let rs = getopts(args, opts);
+ let args = vec!(~"--verbose");
+ let opts = vec!(optflagmulti("v", "verbose", "verbosity"));
+ let rs = getopts(args.as_slice(), opts.as_slice());
match rs {
Ok(ref m) => {
assert_eq!(m.opt_count("verbose"), 1);
#[test]
fn test_optflagmulti_long2() {
- let args = ~[~"--verbose", ~"--verbose"];
- let opts = ~[optflagmulti("v", "verbose", "verbosity")];
- let rs = getopts(args, opts);
+ let args = vec!(~"--verbose", ~"--verbose");
+ let opts = vec!(optflagmulti("v", "verbose", "verbosity"));
+ let rs = getopts(args.as_slice(), opts.as_slice());
match rs {
Ok(ref m) => {
assert_eq!(m.opt_count("verbose"), 2);
#[test]
fn test_optflagmulti_mix() {
- let args = ~[~"--verbose", ~"-v", ~"-vv", ~"verbose"];
- let opts = ~[optflagmulti("v", "verbose", "verbosity")];
- let rs = getopts(args, opts);
+ let args = vec!(~"--verbose", ~"-v", ~"-vv", ~"verbose");
+ let opts = vec!(optflagmulti("v", "verbose", "verbosity"));
+ let rs = getopts(args.as_slice(), opts.as_slice());
match rs {
Ok(ref m) => {
assert_eq!(m.opt_count("verbose"), 4);
// Tests for optmulti
#[test]
fn test_optmulti() {
- let long_args = ~[~"--test=20"];
- let opts = ~[optmulti("t", "test", "testing", "TEST")];
- let rs = getopts(long_args, opts);
+ let long_args = vec!(~"--test=20");
+ let opts = vec!(optmulti("t", "test", "testing", "TEST"));
+ let rs = getopts(long_args.as_slice(), opts.as_slice());
match rs {
Ok(ref m) => {
assert!((m.opt_present("test")));
}
_ => fail!()
}
- let short_args = ~[~"-t", ~"20"];
- match getopts(short_args, opts) {
+ let short_args = vec!(~"-t", ~"20");
+ match getopts(short_args.as_slice(), opts.as_slice()) {
Ok(ref m) => {
assert!((m.opt_present("test")));
assert_eq!(m.opt_str("test").unwrap(), ~"20");
#[test]
fn test_optmulti_missing() {
- let args = ~[~"blah"];
- let opts = ~[optmulti("t", "test", "testing", "TEST")];
- let rs = getopts(args, opts);
+ let args = vec!(~"blah");
+ let opts = vec!(optmulti("t", "test", "testing", "TEST"));
+ let rs = getopts(args.as_slice(), opts.as_slice());
match rs {
Ok(ref m) => {
assert!(!m.opt_present("test"));
#[test]
fn test_optmulti_no_arg() {
- let long_args = ~[~"--test"];
- let opts = ~[optmulti("t", "test", "testing", "TEST")];
- let rs = getopts(long_args, opts);
+ let long_args = vec!(~"--test");
+ let opts = vec!(optmulti("t", "test", "testing", "TEST"));
+ let rs = getopts(long_args.as_slice(), opts.as_slice());
match rs {
Err(f) => check_fail_type(f, ArgumentMissing_),
_ => fail!()
}
- let short_args = ~[~"-t"];
- match getopts(short_args, opts) {
+ let short_args = vec!(~"-t");
+ match getopts(short_args.as_slice(), opts.as_slice()) {
Err(f) => check_fail_type(f, ArgumentMissing_),
_ => fail!()
}
#[test]
fn test_optmulti_multi() {
- let args = ~[~"--test=20", ~"-t", ~"30"];
- let opts = ~[optmulti("t", "test", "testing", "TEST")];
- let rs = getopts(args, opts);
+ let args = vec!(~"--test=20", ~"-t", ~"30");
+ let opts = vec!(optmulti("t", "test", "testing", "TEST"));
+ let rs = getopts(args.as_slice(), opts.as_slice());
match rs {
Ok(ref m) => {
assert!(m.opt_present("test"));
assert!(m.opt_present("t"));
assert_eq!(m.opt_str("t").unwrap(), ~"20");
let pair = m.opt_strs("test");
- assert!(pair[0] == ~"20");
- assert!(pair[1] == ~"30");
+ assert!(*pair.get(0) == ~"20");
+ assert!(*pair.get(1) == ~"30");
}
_ => fail!()
}
#[test]
fn test_unrecognized_option() {
- let long_args = ~[~"--untest"];
- let opts = ~[optmulti("t", "test", "testing", "TEST")];
- let rs = getopts(long_args, opts);
+ let long_args = vec!(~"--untest");
+ let opts = vec!(optmulti("t", "test", "testing", "TEST"));
+ let rs = getopts(long_args.as_slice(), opts.as_slice());
match rs {
Err(f) => check_fail_type(f, UnrecognizedOption_),
_ => fail!()
}
- let short_args = ~[~"-u"];
- match getopts(short_args, opts) {
+ let short_args = vec!(~"-u");
+ match getopts(short_args.as_slice(), opts.as_slice()) {
Err(f) => check_fail_type(f, UnrecognizedOption_),
_ => fail!()
}
#[test]
fn test_combined() {
let args =
- ~[~"prog", ~"free1", ~"-s", ~"20", ~"free2",
+ vec!(~"prog", ~"free1", ~"-s", ~"20", ~"free2",
~"--flag", ~"--long=30", ~"-f", ~"-m", ~"40",
- ~"-m", ~"50", ~"-n", ~"-A B", ~"-n", ~"-60 70"];
+ ~"-m", ~"50", ~"-n", ~"-A B", ~"-n", ~"-60 70");
let opts =
- ~[optopt("s", "something", "something", "SOMETHING"),
+ vec!(optopt("s", "something", "something", "SOMETHING"),
optflag("", "flag", "a flag"),
reqopt("", "long", "hi", "LONG"),
optflag("f", "", "another flag"),
optmulti("m", "", "mmmmmm", "YUM"),
optmulti("n", "", "nothing", "NOTHING"),
- optopt("", "notpresent", "nothing to see here", "NOPE")];
- let rs = getopts(args, opts);
+ optopt("", "notpresent", "nothing to see here", "NOPE"));
+ let rs = getopts(args.as_slice(), opts.as_slice());
match rs {
Ok(ref m) => {
- assert!(m.free[0] == ~"prog");
- assert!(m.free[1] == ~"free1");
+ assert!(*m.free.get(0) == ~"prog");
+ assert!(*m.free.get(1) == ~"free1");
assert_eq!(m.opt_str("s").unwrap(), ~"20");
- assert!(m.free[2] == ~"free2");
+ assert!(*m.free.get(2) == ~"free2");
assert!((m.opt_present("flag")));
assert_eq!(m.opt_str("long").unwrap(), ~"30");
assert!((m.opt_present("f")));
let pair = m.opt_strs("m");
- assert!(pair[0] == ~"40");
- assert!(pair[1] == ~"50");
+ assert!(*pair.get(0) == ~"40");
+ assert!(*pair.get(1) == ~"50");
let pair = m.opt_strs("n");
- assert!(pair[0] == ~"-A B");
- assert!(pair[1] == ~"-60 70");
+ assert!(*pair.get(0) == ~"-A B");
+ assert!(*pair.get(1) == ~"-60 70");
assert!((!m.opt_present("notpresent")));
}
_ => fail!()
#[test]
fn test_multi() {
- let opts = ~[optopt("e", "", "encrypt", "ENCRYPT"),
+ let opts = vec!(optopt("e", "", "encrypt", "ENCRYPT"),
optopt("", "encrypt", "encrypt", "ENCRYPT"),
- optopt("f", "", "flag", "FLAG")];
+ optopt("f", "", "flag", "FLAG"));
- let args_single = ~[~"-e", ~"foo"];
- let matches_single = &match getopts(args_single, opts) {
+ let args_single = vec!(~"-e", ~"foo");
+ let matches_single = &match getopts(args_single.as_slice(),
+ opts.as_slice()) {
result::Ok(m) => m,
result::Err(_) => fail!()
};
assert_eq!(matches_single.opts_str([~"e", ~"encrypt"]).unwrap(), ~"foo");
assert_eq!(matches_single.opts_str([~"encrypt", ~"e"]).unwrap(), ~"foo");
- let args_both = ~[~"-e", ~"foo", ~"--encrypt", ~"foo"];
- let matches_both = &match getopts(args_both, opts) {
+ let args_both = vec!(~"-e", ~"foo", ~"--encrypt", ~"foo");
+ let matches_both = &match getopts(args_both.as_slice(),
+ opts.as_slice()) {
result::Ok(m) => m,
result::Err(_) => fail!()
};
#[test]
fn test_nospace() {
- let args = ~[~"-Lfoo", ~"-M."];
- let opts = ~[optmulti("L", "", "library directory", "LIB"),
- optmulti("M", "", "something", "MMMM")];
- let matches = &match getopts(args, opts) {
+ let args = vec!(~"-Lfoo", ~"-M.");
+ let opts = vec!(optmulti("L", "", "library directory", "LIB"),
+ optmulti("M", "", "something", "MMMM"));
+ let matches = &match getopts(args.as_slice(), opts.as_slice()) {
result::Ok(m) => m,
result::Err(_) => fail!()
};
#[test]
fn test_long_to_short() {
- let mut short = Opt { name: Long(~"banana"),
- hasarg: Yes,
- occur: Req,
- aliases: ~[] };
- short.aliases = ~[Opt { name: Short('b'),
+ let mut short = Opt {
+ name: Long(~"banana"),
+ hasarg: Yes,
+ occur: Req,
+ aliases: Vec::new(),
+ };
+ short.aliases = vec!(Opt { name: Short('b'),
hasarg: Yes,
occur: Req,
- aliases: ~[] }];
+ aliases: Vec::new() });
let verbose = reqopt("b", "banana", "some bananas", "VAL");
assert!(verbose.long_to_short() == short);
#[test]
fn test_aliases_long_and_short() {
- let opts = ~[
- optflagmulti("a", "apple", "Desc"),
- ];
+ let opts = vec!(
+ optflagmulti("a", "apple", "Desc"));
- let args = ~[~"-a", ~"--apple", ~"-a"];
+ let args = vec!(~"-a", ~"--apple", ~"-a");
- let matches = getopts(args, opts).unwrap();
+ let matches = getopts(args.as_slice(), opts.as_slice()).unwrap();
assert_eq!(3, matches.opt_count("a"));
assert_eq!(3, matches.opt_count("apple"));
}
#[test]
fn test_usage() {
- let optgroups = ~[
+ let optgroups = vec!(
reqopt("b", "banana", "Desc", "VAL"),
optopt("a", "012345678901234567890123456789",
"Desc", "VAL"),
optflag("k", "kiwi", "Desc"),
optflagopt("p", "", "Desc", "VAL"),
- optmulti("l", "", "Desc", "VAL"),
- ];
+ optmulti("l", "", "Desc", "VAL"));
let expected =
~"Usage: fruits
-l VAL Desc
";
- let generated_usage = usage("Usage: fruits", optgroups);
+ let generated_usage = usage("Usage: fruits", optgroups.as_slice());
debug!("expected: <<{}>>", expected);
debug!("generated: <<{}>>", generated_usage);
// indentation should be 24 spaces
// lines wrap after 78: or rather descriptions wrap after 54
- let optgroups = ~[
+ let optgroups = vec!(
optflag("k", "kiwi",
"This is a long description which won't be wrapped..+.."), // 54
optflag("a", "apple",
- "This is a long description which _will_ be wrapped..+.."), // 55
- ];
+ "This is a long description which _will_ be wrapped..+.."));
let expected =
~"Usage: fruits
wrapped..+..
";
- let usage = usage("Usage: fruits", optgroups);
+ let usage = usage("Usage: fruits", optgroups.as_slice());
debug!("expected: <<{}>>", expected);
debug!("generated: <<{}>>", usage);
#[test]
fn test_usage_description_multibyte_handling() {
- let optgroups = ~[
+ let optgroups = vec!(
optflag("k", "k\u2013w\u2013",
"The word kiwi is normally spelled with two i's"),
optflag("a", "apple",
"This \u201Cdescription\u201D has some characters that could \
-confuse the line wrapping; an apple costs 0.51€ in some parts of Europe."),
- ];
+confuse the line wrapping; an apple costs 0.51€ in some parts of Europe."));
let expected =
~"Usage: fruits
some parts of Europe.
";
- let usage = usage("Usage: fruits", optgroups);
+ let usage = usage("Usage: fruits", optgroups.as_slice());
debug!("expected: <<{}>>", expected);
debug!("generated: <<{}>>", usage);
#[test]
fn test_short_usage() {
- let optgroups = ~[
+ let optgroups = vec!(
reqopt("b", "banana", "Desc", "VAL"),
optopt("a", "012345678901234567890123456789",
"Desc", "VAL"),
optflag("k", "kiwi", "Desc"),
optflagopt("p", "", "Desc", "VAL"),
- optmulti("l", "", "Desc", "VAL"),
- ];
+ optmulti("l", "", "Desc", "VAL"));
let expected = ~"Usage: fruits -b VAL [-a VAL] [-k] [-p [VAL]] [-l VAL]..";
- let generated_usage = short_usage("fruits", optgroups);
+ let generated_usage = short_usage("fruits", optgroups.as_slice());
debug!("expected: <<{}>>", expected);
debug!("generated: <<{}>>", generated_usage);