2013-03-10 19:47:28 -05:00
|
|
|
|
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
|
2012-12-03 18:48:01 -06:00
|
|
|
|
// 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.
|
|
|
|
|
|
2013-09-17 20:42:23 -05:00
|
|
|
|
//! Simple getopt alternative.
|
|
|
|
|
//!
|
2013-09-26 00:51:19 -05:00
|
|
|
|
//! 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
|
2013-09-17 20:42:23 -05:00
|
|
|
|
//! either get a failure code back, or a match. You'll have to verify whether
|
2013-09-26 00:51:19 -05:00
|
|
|
|
//! the amount of 'free' arguments in the match is what you expect. Use `opt_*`
|
2013-09-17 20:42:23 -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. Single-character
|
|
|
|
|
//! options don't require the space.
|
|
|
|
|
//!
|
|
|
|
|
//! # Example
|
|
|
|
|
//!
|
|
|
|
|
//! The following example shows simple command line parsing for an application
|
|
|
|
|
//! that requires an input file to be specified, accepts an optional output
|
2013-09-26 00:51:19 -05:00
|
|
|
|
//! file name following `-o`, and accepts both `-h` and `--help` as optional flags.
|
2013-09-17 20:42:23 -05:00
|
|
|
|
//!
|
2013-09-23 16:18:26 -05:00
|
|
|
|
//! ~~~{.rust}
|
2013-12-14 23:34:14 -06:00
|
|
|
|
//! extern mod extra;
|
2013-12-31 00:51:11 -06:00
|
|
|
|
//! use extra::getopts::{optopt,optflag,getopts,Opt};
|
2013-09-17 20:42:23 -05:00
|
|
|
|
//! use std::os;
|
|
|
|
|
//!
|
|
|
|
|
//! fn do_work(inp: &str, out: Option<~str>) {
|
2014-01-09 04:06:55 -06:00
|
|
|
|
//! println!("{}", inp);
|
|
|
|
|
//! match out {
|
|
|
|
|
//! Some(x) => println!("{}", x),
|
|
|
|
|
//! None => println!("No Output"),
|
|
|
|
|
//! }
|
2013-09-17 20:42:23 -05:00
|
|
|
|
//! }
|
|
|
|
|
//!
|
|
|
|
|
//! fn print_usage(program: &str, _opts: &[Opt]) {
|
2013-09-25 00:16:43 -05:00
|
|
|
|
//! println!("Usage: {} [options]", program);
|
2014-01-09 04:06:55 -06:00
|
|
|
|
//! println!("-o\t\tOutput");
|
|
|
|
|
//! println!("-h --help\tUsage");
|
2013-09-17 20:42:23 -05:00
|
|
|
|
//! }
|
|
|
|
|
//!
|
|
|
|
|
//! fn main() {
|
|
|
|
|
//! let args = os::args();
|
|
|
|
|
//!
|
|
|
|
|
//! let program = args[0].clone();
|
|
|
|
|
//!
|
|
|
|
|
//! let opts = ~[
|
|
|
|
|
//! optopt("o"),
|
|
|
|
|
//! optflag("h"),
|
|
|
|
|
//! optflag("help")
|
|
|
|
|
//! ];
|
|
|
|
|
//! let matches = match getopts(args.tail(), opts) {
|
|
|
|
|
//! Ok(m) => { m }
|
2013-10-21 15:08:31 -05:00
|
|
|
|
//! Err(f) => { fail!(f.to_err_msg()) }
|
2013-09-17 20:42:23 -05:00
|
|
|
|
//! };
|
|
|
|
|
//! if matches.opt_present("h") || matches.opt_present("help") {
|
|
|
|
|
//! print_usage(program, opts);
|
|
|
|
|
//! return;
|
|
|
|
|
//! }
|
|
|
|
|
//! let output = matches.opt_str("o");
|
|
|
|
|
//! let input: &str = if !matches.free.is_empty() {
|
|
|
|
|
//! matches.free[0].clone()
|
|
|
|
|
//! } else {
|
|
|
|
|
//! print_usage(program, opts);
|
|
|
|
|
//! return;
|
|
|
|
|
//! };
|
|
|
|
|
//! do_work(input, output);
|
|
|
|
|
//! }
|
2013-09-23 16:18:26 -05:00
|
|
|
|
//! ~~~
|
2013-05-17 17:28:44 -05:00
|
|
|
|
|
2013-06-28 17:32:26 -05:00
|
|
|
|
use std::cmp::Eq;
|
|
|
|
|
use std::result::{Err, Ok};
|
|
|
|
|
use std::result;
|
|
|
|
|
use std::option::{Some, None};
|
|
|
|
|
use std::vec;
|
2011-05-21 18:30:04 -05:00
|
|
|
|
|
2013-09-17 20:42:23 -05:00
|
|
|
|
/// Name of an option. Either a string or a single char.
|
2013-07-02 14:47:32 -05:00
|
|
|
|
#[deriving(Clone, Eq)]
|
2013-10-02 01:26:45 -05:00
|
|
|
|
#[allow(missing_doc)]
|
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
|
|
|
|
|
2013-09-17 20:42:23 -05:00
|
|
|
|
/// Describes whether an option has an argument.
|
2013-07-02 14:47:32 -05:00
|
|
|
|
#[deriving(Clone, Eq)]
|
2013-10-02 01:26:45 -05:00
|
|
|
|
#[allow(missing_doc)]
|
2013-07-02 14:47:32 -05:00
|
|
|
|
pub enum HasArg {
|
|
|
|
|
Yes,
|
|
|
|
|
No,
|
|
|
|
|
Maybe,
|
|
|
|
|
}
|
2011-06-15 13:19:50 -05:00
|
|
|
|
|
2013-09-17 20:42:23 -05:00
|
|
|
|
/// Describes how often an option may occur.
|
2013-07-02 14:47:32 -05:00
|
|
|
|
#[deriving(Clone, Eq)]
|
2013-10-02 01:26:45 -05:00
|
|
|
|
#[allow(missing_doc)]
|
2013-07-02 14:47:32 -05:00
|
|
|
|
pub enum Occur {
|
|
|
|
|
Req,
|
|
|
|
|
Optional,
|
|
|
|
|
Multi,
|
|
|
|
|
}
|
2011-04-26 10:46:54 -05:00
|
|
|
|
|
2013-09-17 20:42:23 -05:00
|
|
|
|
/// A description of a possible option.
|
2013-07-02 14:47:32 -05:00
|
|
|
|
#[deriving(Clone, Eq)]
|
2012-12-11 19:03:22 -06:00
|
|
|
|
pub struct Opt {
|
2013-09-17 20:42:23 -05:00
|
|
|
|
/// Name of the option
|
2012-12-11 19:03:22 -06:00
|
|
|
|
name: Name,
|
2013-12-14 23:34:14 -06:00
|
|
|
|
/// Whether it has an argument
|
2012-12-11 19:03:22 -06:00
|
|
|
|
hasarg: HasArg,
|
2013-10-20 00:51:30 -05:00
|
|
|
|
/// How often it can occur
|
2013-08-05 07:37:54 -05:00
|
|
|
|
occur: Occur,
|
2013-09-17 20:42:23 -05:00
|
|
|
|
/// Which options it aliases
|
2013-10-19 19:33:09 -05:00
|
|
|
|
priv aliases: ~[Opt],
|
2012-12-11 19:03:22 -06:00
|
|
|
|
}
|
2011-04-26 10:46:54 -05:00
|
|
|
|
|
2013-09-17 20:42:23 -05:00
|
|
|
|
/// Describes wether an option is given at all or has a value.
|
|
|
|
|
#[deriving(Clone, Eq)]
|
|
|
|
|
enum Optval {
|
|
|
|
|
Val(~str),
|
|
|
|
|
Given,
|
2011-04-26 10:46:54 -05:00
|
|
|
|
}
|
2011-06-15 13:19:50 -05:00
|
|
|
|
|
2013-09-17 20:42:23 -05:00
|
|
|
|
/// The result of checking command line arguments. Contains a vector
|
|
|
|
|
/// of matches and a vector of free strings.
|
|
|
|
|
#[deriving(Clone, Eq)]
|
|
|
|
|
pub struct Matches {
|
|
|
|
|
/// Options that matched
|
2013-10-19 19:33:09 -05:00
|
|
|
|
priv opts: ~[Opt],
|
2013-09-17 20:42:23 -05:00
|
|
|
|
/// Values of the Options that matched
|
2013-10-19 19:33:09 -05:00
|
|
|
|
priv vals: ~[~[Optval]],
|
2013-09-17 20:42:23 -05:00
|
|
|
|
/// Free string fragments
|
|
|
|
|
free: ~[~str]
|
2011-04-26 10:46:54 -05:00
|
|
|
|
}
|
2011-06-15 13:19:50 -05:00
|
|
|
|
|
2013-09-17 20:42:23 -05:00
|
|
|
|
/// The type returned when the command line does not conform to the
|
2013-11-10 06:27:03 -06:00
|
|
|
|
/// expected format. Call the `to_err_msg` method to retrieve the
|
|
|
|
|
/// error as a string.
|
2013-09-17 20:42:23 -05:00
|
|
|
|
#[deriving(Clone, Eq, ToStr)]
|
2013-10-02 01:26:45 -05:00
|
|
|
|
#[allow(missing_doc)]
|
2013-09-17 20:42:23 -05:00
|
|
|
|
pub enum Fail_ {
|
|
|
|
|
ArgumentMissing(~str),
|
|
|
|
|
UnrecognizedOption(~str),
|
|
|
|
|
OptionMissing(~str),
|
|
|
|
|
OptionDuplicated(~str),
|
|
|
|
|
UnexpectedArgument(~str),
|
2011-04-26 10:46:54 -05:00
|
|
|
|
}
|
2011-06-15 13:19:50 -05:00
|
|
|
|
|
2013-12-14 23:34:14 -06:00
|
|
|
|
/// The type of failure that occurred.
|
2013-09-17 20:42:23 -05:00
|
|
|
|
#[deriving(Eq)]
|
2013-10-02 01:26:45 -05:00
|
|
|
|
#[allow(missing_doc)]
|
2013-09-17 20:42:23 -05:00
|
|
|
|
pub enum FailType {
|
|
|
|
|
ArgumentMissing_,
|
|
|
|
|
UnrecognizedOption_,
|
|
|
|
|
OptionMissing_,
|
|
|
|
|
OptionDuplicated_,
|
|
|
|
|
UnexpectedArgument_,
|
2011-04-26 10:46:54 -05:00
|
|
|
|
}
|
2011-06-15 13:19:50 -05:00
|
|
|
|
|
2013-09-17 20:42:23 -05:00
|
|
|
|
/// The result of parsing a command line with a set of options.
|
|
|
|
|
pub type Result = result::Result<Matches, Fail_>;
|
2012-11-17 21:43:39 -06:00
|
|
|
|
|
2013-09-17 20:42:23 -05:00
|
|
|
|
impl Name {
|
|
|
|
|
fn from_str(nm: &str) -> Name {
|
|
|
|
|
if nm.len() == 1u {
|
|
|
|
|
Short(nm.char_at(0u))
|
|
|
|
|
} else {
|
|
|
|
|
Long(nm.to_owned())
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-06-15 13:19:50 -05:00
|
|
|
|
|
2013-09-17 20:42:23 -05:00
|
|
|
|
fn to_str(&self) -> ~str {
|
|
|
|
|
match *self {
|
|
|
|
|
Short(ch) => ch.to_str(),
|
|
|
|
|
Long(ref s) => s.to_owned()
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-04-26 10:46:54 -05:00
|
|
|
|
}
|
|
|
|
|
|
2013-09-17 20:42:23 -05:00
|
|
|
|
impl Matches {
|
2013-11-08 18:20:25 -06:00
|
|
|
|
fn opt_vals(&self, nm: &str) -> ~[Optval] {
|
2013-09-17 20:42:23 -05:00
|
|
|
|
match find_opt(self.opts, Name::from_str(nm)) {
|
|
|
|
|
Some(id) => self.vals[id].clone(),
|
2013-10-21 15:08:31 -05:00
|
|
|
|
None => fail!("No option '{}' defined", nm)
|
2013-09-17 20:42:23 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
2011-04-26 10:46:54 -05:00
|
|
|
|
|
2013-11-08 18:20:25 -06:00
|
|
|
|
fn opt_val(&self, nm: &str) -> Option<Optval> {
|
2013-09-17 20:42:23 -05:00
|
|
|
|
let vals = self.opt_vals(nm);
|
|
|
|
|
if (vals.is_empty()) {
|
|
|
|
|
None
|
|
|
|
|
} else {
|
|
|
|
|
Some(vals[0].clone())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Returns true if an option was matched.
|
|
|
|
|
pub fn opt_present(&self, nm: &str) -> bool {
|
|
|
|
|
!self.opt_vals(nm).is_empty()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Returns the number of times an option was matched.
|
|
|
|
|
pub fn opt_count(&self, nm: &str) -> uint {
|
|
|
|
|
self.opt_vals(nm).len()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// 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,
|
|
|
|
|
_ => (),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Returns the string argument supplied to one of several matching options or `None`.
|
|
|
|
|
pub fn opts_str(&self, names: &[~str]) -> Option<~str> {
|
|
|
|
|
for nm in names.iter() {
|
|
|
|
|
match self.opt_val(*nm) {
|
|
|
|
|
Some(Val(ref s)) => return Some(s.clone()),
|
|
|
|
|
_ => ()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Returns a vector of the arguments provided to all matches of the given
|
|
|
|
|
/// option.
|
|
|
|
|
///
|
|
|
|
|
/// Used when an option accepts multiple values.
|
|
|
|
|
pub fn opt_strs(&self, nm: &str) -> ~[~str] {
|
|
|
|
|
let mut acc: ~[~str] = ~[];
|
|
|
|
|
let r = self.opt_vals(nm);
|
|
|
|
|
for v in r.iter() {
|
|
|
|
|
match *v {
|
|
|
|
|
Val(ref s) => acc.push((*s).clone()),
|
|
|
|
|
_ => ()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
acc
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Returns the string argument supplied to a matching option or `None`.
|
|
|
|
|
pub fn opt_str(&self, nm: &str) -> Option<~str> {
|
|
|
|
|
let vals = self.opt_vals(nm);
|
|
|
|
|
if vals.is_empty() {
|
|
|
|
|
return None::<~str>;
|
|
|
|
|
}
|
|
|
|
|
match vals[0] {
|
|
|
|
|
Val(ref s) => Some((*s).clone()),
|
|
|
|
|
_ => None
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// 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.
|
|
|
|
|
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()),
|
|
|
|
|
_ => Some(def.to_owned())
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-04-26 10:46:54 -05:00
|
|
|
|
|
|
|
|
|
}
|
2011-06-15 13:19:50 -05:00
|
|
|
|
|
2013-09-17 20:42:23 -05:00
|
|
|
|
fn is_arg(arg: &str) -> bool {
|
|
|
|
|
arg.len() > 1 && arg[0] == '-' as u8
|
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> {
|
2013-09-17 20:42:23 -05:00
|
|
|
|
// Search main options.
|
2013-08-05 07:37:54 -05:00
|
|
|
|
let pos = opts.iter().position(|opt| opt.name == nm);
|
|
|
|
|
if pos.is_some() {
|
|
|
|
|
return pos
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-17 20:42:23 -05:00
|
|
|
|
// Search in aliases.
|
2013-08-05 07:37:54 -05:00
|
|
|
|
for candidate in opts.iter() {
|
|
|
|
|
if candidate.aliases.iter().position(|opt| opt.name == nm).is_some() {
|
|
|
|
|
return opts.iter().position(|opt| opt.name == candidate.name);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
None
|
2011-04-26 10:46:54 -05:00
|
|
|
|
}
|
|
|
|
|
|
2013-09-17 20:42:23 -05:00
|
|
|
|
/// Create an option that is required and takes an argument.
|
|
|
|
|
pub fn reqopt(name: &str) -> Opt {
|
|
|
|
|
Opt {
|
|
|
|
|
name: Name::from_str(name),
|
|
|
|
|
hasarg: Yes,
|
|
|
|
|
occur: Req,
|
|
|
|
|
aliases: ~[]
|
|
|
|
|
}
|
2011-04-26 10:46:54 -05:00
|
|
|
|
}
|
|
|
|
|
|
2013-09-17 20:42:23 -05:00
|
|
|
|
/// Create an option that is optional and takes an argument.
|
|
|
|
|
pub fn optopt(name: &str) -> Opt {
|
|
|
|
|
Opt {
|
|
|
|
|
name: Name::from_str(name),
|
|
|
|
|
hasarg: Yes,
|
|
|
|
|
occur: Optional,
|
|
|
|
|
aliases: ~[]
|
|
|
|
|
}
|
2011-04-26 10:46:54 -05:00
|
|
|
|
}
|
|
|
|
|
|
2013-09-17 20:42:23 -05:00
|
|
|
|
/// Create an option that is optional and does not take an argument.
|
|
|
|
|
pub fn optflag(name: &str) -> Opt {
|
|
|
|
|
Opt {
|
|
|
|
|
name: Name::from_str(name),
|
|
|
|
|
hasarg: No,
|
|
|
|
|
occur: Optional,
|
|
|
|
|
aliases: ~[]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Create an option that is optional, does not take an argument,
|
|
|
|
|
/// and may occur multiple times.
|
|
|
|
|
pub fn optflagmulti(name: &str) -> Opt {
|
|
|
|
|
Opt {
|
|
|
|
|
name: Name::from_str(name),
|
|
|
|
|
hasarg: No,
|
|
|
|
|
occur: Multi,
|
|
|
|
|
aliases: ~[]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Create an option that is optional and takes an optional argument.
|
|
|
|
|
pub fn optflagopt(name: &str) -> Opt {
|
|
|
|
|
Opt {
|
|
|
|
|
name: Name::from_str(name),
|
|
|
|
|
hasarg: Maybe,
|
|
|
|
|
occur: Optional,
|
|
|
|
|
aliases: ~[]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Create an option that is optional, takes an argument, and may occur
|
|
|
|
|
/// multiple times.
|
|
|
|
|
pub fn optmulti(name: &str) -> Opt {
|
|
|
|
|
Opt {
|
|
|
|
|
name: Name::from_str(name),
|
|
|
|
|
hasarg: Yes,
|
|
|
|
|
occur: Multi,
|
|
|
|
|
aliases: ~[]
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-04-26 10:46:54 -05:00
|
|
|
|
|
2013-09-17 20:42:23 -05:00
|
|
|
|
impl Fail_ {
|
|
|
|
|
/// Convert a `Fail_` enum into an error string.
|
|
|
|
|
pub fn to_err_msg(self) -> ~str {
|
|
|
|
|
match self {
|
|
|
|
|
ArgumentMissing(ref nm) => {
|
2013-09-27 22:18:50 -05:00
|
|
|
|
format!("Argument to option '{}' missing.", *nm)
|
2013-09-17 20:42:23 -05:00
|
|
|
|
}
|
|
|
|
|
UnrecognizedOption(ref nm) => {
|
2013-09-27 22:18:50 -05:00
|
|
|
|
format!("Unrecognized option: '{}'.", *nm)
|
2013-09-17 20:42:23 -05:00
|
|
|
|
}
|
|
|
|
|
OptionMissing(ref nm) => {
|
2013-09-27 22:18:50 -05:00
|
|
|
|
format!("Required option '{}' missing.", *nm)
|
2013-09-17 20:42:23 -05:00
|
|
|
|
}
|
|
|
|
|
OptionDuplicated(ref nm) => {
|
2013-09-27 22:18:50 -05:00
|
|
|
|
format!("Option '{}' given more than once.", *nm)
|
2013-09-17 20:42:23 -05:00
|
|
|
|
}
|
|
|
|
|
UnexpectedArgument(ref nm) => {
|
2013-09-27 22:18:50 -05:00
|
|
|
|
format!("Option '{}' does not take an argument.", *nm)
|
2013-09-17 20:42:23 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Parse command line arguments according to the provided options.
|
|
|
|
|
///
|
|
|
|
|
/// On success returns `Ok(Opt)`. Use methods such as `opt_present`
|
|
|
|
|
/// `opt_str`, etc. to interrogate results. Returns `Err(Fail_)` on failure.
|
|
|
|
|
/// Use `to_err_msg` to get an error message.
|
2013-01-23 13:43:58 -06:00
|
|
|
|
pub fn getopts(args: &[~str], opts: &[Opt]) -> Result {
|
2013-04-09 00:31:42 -05:00
|
|
|
|
let n_opts = opts.len();
|
2013-09-17 20:42:23 -05:00
|
|
|
|
|
2013-04-09 00:31:42 -05:00
|
|
|
|
fn f(_x: uint) -> ~[Optval] { return ~[]; }
|
2013-09-17 20:42:23 -05:00
|
|
|
|
|
2013-04-09 00:31:42 -05:00
|
|
|
|
let mut vals = vec::from_fn(n_opts, f);
|
|
|
|
|
let mut free: ~[~str] = ~[];
|
|
|
|
|
let l = args.len();
|
|
|
|
|
let mut i = 0;
|
|
|
|
|
while i < l {
|
2013-07-02 14:47:32 -05:00
|
|
|
|
let cur = args[i].clone();
|
2013-04-09 00:31:42 -05:00
|
|
|
|
let curlen = cur.len();
|
|
|
|
|
if !is_arg(cur) {
|
|
|
|
|
free.push(cur);
|
|
|
|
|
} else if cur == ~"--" {
|
|
|
|
|
let mut j = i + 1;
|
2013-07-02 14:47:32 -05:00
|
|
|
|
while j < l { free.push(args[j].clone()); j += 1; }
|
2013-04-09 00:31:42 -05:00
|
|
|
|
break;
|
|
|
|
|
} else {
|
|
|
|
|
let mut names;
|
|
|
|
|
let mut i_arg = None;
|
|
|
|
|
if cur[1] == '-' as u8 {
|
2013-06-09 09:44:58 -05:00
|
|
|
|
let tail = cur.slice(2, curlen);
|
2013-11-23 04:18:51 -06:00
|
|
|
|
let tail_eq: ~[&str] = tail.split('=').collect();
|
2013-04-09 00:31:42 -05:00
|
|
|
|
if tail_eq.len() <= 1 {
|
2013-05-29 13:10:16 -05:00
|
|
|
|
names = ~[Long(tail.to_owned())];
|
2013-01-23 13:43:58 -06:00
|
|
|
|
} else {
|
2013-04-09 00:31:42 -05:00
|
|
|
|
names =
|
2013-06-09 08:10:50 -05:00
|
|
|
|
~[Long(tail_eq[0].to_owned())];
|
|
|
|
|
i_arg = Some(tail_eq[1].to_owned());
|
2013-04-09 00:31:42 -05:00
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
let mut j = 1;
|
|
|
|
|
let mut last_valid_opt_id = None;
|
|
|
|
|
names = ~[];
|
|
|
|
|
while j < curlen {
|
2013-06-10 06:46:36 -05:00
|
|
|
|
let range = cur.char_range_at(j);
|
2013-04-09 00:31:42 -05:00
|
|
|
|
let opt = Short(range.ch);
|
2012-07-04 10:36:31 -05:00
|
|
|
|
|
2013-04-09 00:31:42 -05: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-07-02 14:47:32 -05:00
|
|
|
|
match find_opt(opts, opt.clone()) {
|
2013-04-09 00:31:42 -05:00
|
|
|
|
Some(id) => last_valid_opt_id = Some(id),
|
|
|
|
|
None => {
|
|
|
|
|
let arg_follows =
|
|
|
|
|
last_valid_opt_id.is_some() &&
|
2013-08-03 18:59:24 -05:00
|
|
|
|
match opts[last_valid_opt_id.unwrap()]
|
2013-04-09 00:31:42 -05:00
|
|
|
|
.hasarg {
|
2012-08-06 14:34:08 -05:00
|
|
|
|
|
2013-04-09 00:31:42 -05:00
|
|
|
|
Yes | Maybe => true,
|
|
|
|
|
No => false
|
|
|
|
|
};
|
|
|
|
|
if arg_follows && j < curlen {
|
|
|
|
|
i_arg = Some(cur.slice(j, curlen).to_owned());
|
|
|
|
|
break;
|
|
|
|
|
} else {
|
|
|
|
|
last_valid_opt_id = None;
|
2012-07-04 10:36:31 -05:00
|
|
|
|
}
|
2013-04-09 00:31:42 -05:00
|
|
|
|
}
|
2012-07-04 10:36:31 -05:00
|
|
|
|
}
|
2013-04-09 00:31:42 -05:00
|
|
|
|
names.push(opt);
|
|
|
|
|
j = range.next;
|
2011-04-26 10:46:54 -05:00
|
|
|
|
}
|
2013-04-09 00:31:42 -05:00
|
|
|
|
}
|
|
|
|
|
let mut name_pos = 0;
|
2013-08-03 11:45:23 -05:00
|
|
|
|
for nm in names.iter() {
|
2013-04-09 00:31:42 -05:00
|
|
|
|
name_pos += 1;
|
2013-07-02 14:47:32 -05:00
|
|
|
|
let optid = match find_opt(opts, (*nm).clone()) {
|
2013-04-09 00:31:42 -05:00
|
|
|
|
Some(id) => id,
|
2013-09-17 20:42:23 -05:00
|
|
|
|
None => return Err(UnrecognizedOption(nm.to_str()))
|
2013-04-09 00:31:42 -05:00
|
|
|
|
};
|
|
|
|
|
match opts[optid].hasarg {
|
|
|
|
|
No => {
|
|
|
|
|
if !i_arg.is_none() {
|
2013-09-17 20:42:23 -05:00
|
|
|
|
return Err(UnexpectedArgument(nm.to_str()));
|
2013-01-23 13:43:58 -06:00
|
|
|
|
}
|
2013-04-09 00:31:42 -05:00
|
|
|
|
vals[optid].push(Given);
|
|
|
|
|
}
|
|
|
|
|
Maybe => {
|
|
|
|
|
if !i_arg.is_none() {
|
2013-08-03 18:59:24 -05:00
|
|
|
|
vals[optid].push(Val((i_arg.clone()).unwrap()));
|
2013-04-09 00:31:42 -05:00
|
|
|
|
} else if name_pos < names.len() ||
|
|
|
|
|
i + 1 == l || is_arg(args[i + 1]) {
|
|
|
|
|
vals[optid].push(Given);
|
2013-07-02 14:47:32 -05:00
|
|
|
|
} else { i += 1; vals[optid].push(Val(args[i].clone())); }
|
2013-04-09 00:31:42 -05:00
|
|
|
|
}
|
|
|
|
|
Yes => {
|
|
|
|
|
if !i_arg.is_none() {
|
2013-08-03 18:59:24 -05:00
|
|
|
|
vals[optid].push(Val(i_arg.clone().unwrap()));
|
2013-04-09 00:31:42 -05:00
|
|
|
|
} else if i + 1 == l {
|
2013-09-17 20:42:23 -05:00
|
|
|
|
return Err(ArgumentMissing(nm.to_str()));
|
2013-07-02 14:47:32 -05:00
|
|
|
|
} else { i += 1; vals[optid].push(Val(args[i].clone())); }
|
2013-04-09 00:31:42 -05:00
|
|
|
|
}
|
2011-04-26 10:46:54 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-04-09 00:31:42 -05:00
|
|
|
|
i += 1;
|
|
|
|
|
}
|
|
|
|
|
i = 0u;
|
|
|
|
|
while i < n_opts {
|
|
|
|
|
let n = vals[i].len();
|
|
|
|
|
let occ = opts[i].occur;
|
|
|
|
|
if occ == Req {
|
|
|
|
|
if n == 0 {
|
2013-09-17 20:42:23 -05:00
|
|
|
|
return Err(OptionMissing(opts[i].name.to_str()));
|
2011-06-15 13:19:50 -05:00
|
|
|
|
}
|
2013-04-09 00:31:42 -05:00
|
|
|
|
}
|
|
|
|
|
if occ != Multi {
|
|
|
|
|
if n > 1 {
|
2013-09-17 20:42:23 -05:00
|
|
|
|
return Err(OptionDuplicated(opts[i].name.to_str()));
|
2011-06-15 13:19:50 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
2013-04-09 00:31:42 -05:00
|
|
|
|
i += 1;
|
2011-04-26 10:46:54 -05:00
|
|
|
|
}
|
2013-09-17 20:42:23 -05:00
|
|
|
|
Ok(Matches {
|
|
|
|
|
opts: opts.to_owned(),
|
|
|
|
|
vals: vals,
|
|
|
|
|
free: free
|
|
|
|
|
})
|
2012-05-31 19:02:03 -05:00
|
|
|
|
}
|
|
|
|
|
|
2013-09-17 20:42:23 -05:00
|
|
|
|
/// A module which provides a way to specify descriptions and
|
|
|
|
|
/// groups of short and long option names, together.
|
2012-10-11 18:54:31 -05:00
|
|
|
|
pub mod groups {
|
2013-01-08 21:37:25 -06:00
|
|
|
|
use getopts::{HasArg, Long, Maybe, Multi, No, Occur, Opt, Optional, Req};
|
2013-03-26 15:38:07 -05:00
|
|
|
|
use getopts::{Short, Yes};
|
2012-12-23 16:41:37 -06:00
|
|
|
|
|
2013-09-17 20:42:23 -05:00
|
|
|
|
/// One group of options, e.g., both -h and --help, along with
|
|
|
|
|
/// their shared description and properties.
|
2013-07-02 14:47:32 -05:00
|
|
|
|
#[deriving(Clone, Eq)]
|
2012-12-11 19:03:22 -06:00
|
|
|
|
pub struct OptGroup {
|
2013-09-17 20:42:23 -05:00
|
|
|
|
/// Short Name of the `OptGroup`
|
2012-10-11 18:54:31 -05:00
|
|
|
|
short_name: ~str,
|
2013-09-17 20:42:23 -05:00
|
|
|
|
/// Long Name of the `OptGroup`
|
2012-10-11 18:54:31 -05:00
|
|
|
|
long_name: ~str,
|
2013-09-17 20:42:23 -05:00
|
|
|
|
/// Hint
|
2012-10-11 18:54:31 -05:00
|
|
|
|
hint: ~str,
|
2013-09-17 20:42:23 -05:00
|
|
|
|
/// Description
|
2012-10-11 18:54:31 -05:00
|
|
|
|
desc: ~str,
|
2013-09-17 20:42:23 -05:00
|
|
|
|
/// Whether it has an argument
|
2012-10-11 18:54:31 -05:00
|
|
|
|
hasarg: HasArg,
|
2013-09-17 20:42:23 -05:00
|
|
|
|
/// How often it can occur
|
2012-10-11 18:54:31 -05:00
|
|
|
|
occur: Occur
|
2012-12-11 19:03:22 -06:00
|
|
|
|
}
|
2012-10-11 18:54:31 -05:00
|
|
|
|
|
2013-09-17 20:42:23 -05:00
|
|
|
|
impl OptGroup {
|
|
|
|
|
/// Translate OptGroup into Opt.
|
|
|
|
|
/// (Both short and long names correspond to different Opts).
|
|
|
|
|
pub fn long_to_short(&self) -> Opt {
|
|
|
|
|
let OptGroup {
|
|
|
|
|
short_name: short_name,
|
|
|
|
|
long_name: long_name,
|
|
|
|
|
hasarg: hasarg,
|
|
|
|
|
occur: occur,
|
2013-11-28 14:22:53 -06:00
|
|
|
|
..
|
2013-09-17 20:42:23 -05:00
|
|
|
|
} = (*self).clone();
|
|
|
|
|
|
|
|
|
|
match (short_name.len(), long_name.len()) {
|
2013-10-21 15:08:31 -05:00
|
|
|
|
(0,0) => fail!("this long-format option was given no name"),
|
2013-09-17 20:42:23 -05:00
|
|
|
|
(0,_) => Opt {
|
|
|
|
|
name: Long((long_name)),
|
|
|
|
|
hasarg: hasarg,
|
|
|
|
|
occur: occur,
|
|
|
|
|
aliases: ~[]
|
|
|
|
|
},
|
|
|
|
|
(1,0) => Opt {
|
|
|
|
|
name: Short(short_name.char_at(0)),
|
|
|
|
|
hasarg: hasarg,
|
|
|
|
|
occur: occur,
|
|
|
|
|
aliases: ~[]
|
|
|
|
|
},
|
|
|
|
|
(1,_) => Opt {
|
|
|
|
|
name: Long((long_name)),
|
|
|
|
|
hasarg: hasarg,
|
|
|
|
|
occur: occur,
|
|
|
|
|
aliases: ~[
|
|
|
|
|
Opt {
|
|
|
|
|
name: Short(short_name.char_at(0)),
|
|
|
|
|
hasarg: hasarg,
|
|
|
|
|
occur: occur,
|
|
|
|
|
aliases: ~[]
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
},
|
2013-10-21 15:08:31 -05:00
|
|
|
|
(_,_) => fail!("something is wrong with the long-form opt")
|
2013-09-17 20:42:23 -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 {
|
2012-10-11 18:54:31 -05:00
|
|
|
|
let len = short_name.len();
|
2013-03-28 20:39:09 -05:00
|
|
|
|
assert!(len == 1 || len == 0);
|
2013-09-17 20:42:23 -05:00
|
|
|
|
OptGroup {
|
|
|
|
|
short_name: short_name.to_owned(),
|
|
|
|
|
long_name: long_name.to_owned(),
|
|
|
|
|
hint: hint.to_owned(),
|
|
|
|
|
desc: desc.to_owned(),
|
|
|
|
|
hasarg: Yes,
|
|
|
|
|
occur: Req
|
|
|
|
|
}
|
2012-10-11 18:54:31 -05:00
|
|
|
|
}
|
|
|
|
|
|
2013-09-17 20:42:23 -05:00
|
|
|
|
/// 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 {
|
2012-10-11 18:54:31 -05:00
|
|
|
|
let len = short_name.len();
|
2013-03-28 20:39:09 -05:00
|
|
|
|
assert!(len == 1 || len == 0);
|
2013-09-17 20:42:23 -05:00
|
|
|
|
OptGroup {
|
|
|
|
|
short_name: short_name.to_owned(),
|
|
|
|
|
long_name: long_name.to_owned(),
|
|
|
|
|
hint: hint.to_owned(),
|
|
|
|
|
desc: desc.to_owned(),
|
|
|
|
|
hasarg: Yes,
|
|
|
|
|
occur: Optional
|
|
|
|
|
}
|
2012-10-11 18:54:31 -05:00
|
|
|
|
}
|
|
|
|
|
|
2013-09-17 20:42:23 -05:00
|
|
|
|
/// 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 {
|
2012-10-11 18:54:31 -05:00
|
|
|
|
let len = short_name.len();
|
2013-03-28 20:39:09 -05:00
|
|
|
|
assert!(len == 1 || len == 0);
|
2013-09-17 20:42:23 -05:00
|
|
|
|
OptGroup {
|
|
|
|
|
short_name: short_name.to_owned(),
|
|
|
|
|
long_name: long_name.to_owned(),
|
|
|
|
|
hint: ~"",
|
|
|
|
|
desc: desc.to_owned(),
|
|
|
|
|
hasarg: No,
|
|
|
|
|
occur: Optional
|
|
|
|
|
}
|
2013-08-05 07:34:58 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Create a long option that can occur more than once and does not
|
2013-09-17 20:42:23 -05:00
|
|
|
|
/// take an argument.
|
|
|
|
|
pub fn optflagmulti(short_name: &str, long_name: &str, desc: &str) -> OptGroup {
|
2013-08-05 07:34:58 -05:00
|
|
|
|
let len = short_name.len();
|
|
|
|
|
assert!(len == 1 || len == 0);
|
2013-09-17 20:42:23 -05:00
|
|
|
|
OptGroup {
|
|
|
|
|
short_name: short_name.to_owned(),
|
|
|
|
|
long_name: long_name.to_owned(),
|
|
|
|
|
hint: ~"",
|
|
|
|
|
desc: desc.to_owned(),
|
|
|
|
|
hasarg: No,
|
|
|
|
|
occur: Multi
|
|
|
|
|
}
|
2012-10-11 18:54:31 -05:00
|
|
|
|
}
|
|
|
|
|
|
2013-09-17 20:42:23 -05:00
|
|
|
|
/// 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 {
|
2012-10-11 18:54:31 -05:00
|
|
|
|
let len = short_name.len();
|
2013-03-28 20:39:09 -05:00
|
|
|
|
assert!(len == 1 || len == 0);
|
2013-09-17 20:42:23 -05:00
|
|
|
|
OptGroup {
|
|
|
|
|
short_name: short_name.to_owned(),
|
|
|
|
|
long_name: long_name.to_owned(),
|
|
|
|
|
hint: hint.to_owned(),
|
|
|
|
|
desc: desc.to_owned(),
|
|
|
|
|
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 {
|
2012-10-11 18:54:31 -05:00
|
|
|
|
let len = short_name.len();
|
2013-03-28 20:39:09 -05:00
|
|
|
|
assert!(len == 1 || len == 0);
|
2013-09-17 20:42:23 -05:00
|
|
|
|
OptGroup {
|
|
|
|
|
short_name: short_name.to_owned(),
|
|
|
|
|
long_name: long_name.to_owned(),
|
|
|
|
|
hint: hint.to_owned(),
|
|
|
|
|
desc: desc.to_owned(),
|
|
|
|
|
hasarg: Yes,
|
|
|
|
|
occur: Multi
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// 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 {
|
2013-09-17 20:42:23 -05:00
|
|
|
|
::getopts::getopts(args, opts.map(|x| x.long_to_short()))
|
2012-10-11 18:54:31 -05:00
|
|
|
|
}
|
|
|
|
|
|
2013-09-17 20:42:23 -05:00
|
|
|
|
/// Derive a usage message from a set of long options.
|
2012-10-11 18:54:31 -05:00
|
|
|
|
pub fn usage(brief: &str, opts: &[OptGroup]) -> ~str {
|
|
|
|
|
|
2013-06-29 00:05:50 -05:00
|
|
|
|
let desc_sep = "\n" + " ".repeat(24);
|
2012-10-11 18:54:31 -05:00
|
|
|
|
|
2013-08-09 22:09:47 -05:00
|
|
|
|
let mut rows = opts.iter().map(|optref| {
|
2013-05-08 21:44:43 -05:00
|
|
|
|
let OptGroup{short_name: short_name,
|
|
|
|
|
long_name: long_name,
|
|
|
|
|
hint: hint,
|
|
|
|
|
desc: desc,
|
|
|
|
|
hasarg: hasarg,
|
2013-11-28 14:22:53 -06:00
|
|
|
|
..} = (*optref).clone();
|
2012-10-11 18:54:31 -05:00
|
|
|
|
|
2013-06-10 21:05:42 -05:00
|
|
|
|
let mut row = " ".repeat(4);
|
2012-10-11 18:54:31 -05:00
|
|
|
|
|
|
|
|
|
// short option
|
2013-06-11 21:13:42 -05:00
|
|
|
|
match short_name.len() {
|
|
|
|
|
0 => {}
|
|
|
|
|
1 => {
|
|
|
|
|
row.push_char('-');
|
|
|
|
|
row.push_str(short_name);
|
|
|
|
|
row.push_char(' ');
|
|
|
|
|
}
|
2013-10-21 15:08:31 -05:00
|
|
|
|
_ => fail!("the short name should only be 1 ascii char long"),
|
2013-06-11 21:13:42 -05:00
|
|
|
|
}
|
2012-10-11 18:54:31 -05:00
|
|
|
|
|
|
|
|
|
// long option
|
2013-06-11 21:13:42 -05:00
|
|
|
|
match long_name.len() {
|
|
|
|
|
0 => {}
|
|
|
|
|
_ => {
|
|
|
|
|
row.push_str("--");
|
|
|
|
|
row.push_str(long_name);
|
|
|
|
|
row.push_char(' ');
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-10-11 18:54:31 -05:00
|
|
|
|
|
|
|
|
|
// arg
|
2013-06-11 21:13:42 -05:00
|
|
|
|
match hasarg {
|
|
|
|
|
No => {}
|
|
|
|
|
Yes => row.push_str(hint),
|
|
|
|
|
Maybe => {
|
|
|
|
|
row.push_char('[');
|
|
|
|
|
row.push_str(hint);
|
|
|
|
|
row.push_char(']');
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-10-11 18:54:31 -05:00
|
|
|
|
|
2013-08-24 05:09:18 -05:00
|
|
|
|
// FIXME: #5516 should be graphemes not codepoints
|
2012-10-11 18:54:31 -05:00
|
|
|
|
// here we just need to indent the start of the description
|
2013-08-25 04:19:35 -05:00
|
|
|
|
let rowlen = row.char_len();
|
2013-06-11 21:13:42 -05:00
|
|
|
|
if rowlen < 24 {
|
2013-11-20 17:46:49 -06:00
|
|
|
|
(24 - rowlen).times(|| {
|
2013-06-11 21:13:42 -05:00
|
|
|
|
row.push_char(' ')
|
2013-11-20 17:46:49 -06:00
|
|
|
|
})
|
2012-10-11 18:54:31 -05:00
|
|
|
|
} else {
|
2013-06-11 21:13:42 -05:00
|
|
|
|
row.push_str(desc_sep)
|
|
|
|
|
}
|
2012-10-11 18:54:31 -05:00
|
|
|
|
|
2013-05-08 12:34:47 -05:00
|
|
|
|
// Normalize desc to contain words separated by one space character
|
2013-03-24 01:51:18 -05:00
|
|
|
|
let mut desc_normalized_whitespace = ~"";
|
2013-11-23 04:18:51 -06:00
|
|
|
|
for word in desc.words() {
|
2013-03-23 11:25:16 -05:00
|
|
|
|
desc_normalized_whitespace.push_str(word);
|
|
|
|
|
desc_normalized_whitespace.push_char(' ');
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-24 05:09:18 -05:00
|
|
|
|
// FIXME: #5516 should be graphemes not codepoints
|
2013-03-24 01:51:18 -05:00
|
|
|
|
let mut desc_rows = ~[];
|
2013-11-20 17:46:49 -06:00
|
|
|
|
each_split_within(desc_normalized_whitespace, 54, |substr| {
|
2013-03-24 01:51:18 -05:00
|
|
|
|
desc_rows.push(substr.to_owned());
|
2013-07-31 14:07:44 -05:00
|
|
|
|
true
|
2013-11-20 17:46:49 -06:00
|
|
|
|
});
|
2013-03-23 11:25:16 -05:00
|
|
|
|
|
2013-08-24 05:09:18 -05:00
|
|
|
|
// FIXME: #5516 should be graphemes not codepoints
|
2012-10-11 18:54:31 -05:00
|
|
|
|
// wrapped description
|
2013-06-11 21:13:42 -05:00
|
|
|
|
row.push_str(desc_rows.connect(desc_sep));
|
2012-10-11 18:54:31 -05:00
|
|
|
|
|
|
|
|
|
row
|
|
|
|
|
});
|
|
|
|
|
|
2013-09-27 22:18:50 -05:00
|
|
|
|
format!("{}\n\nOptions:\n{}\n", brief, rows.collect::<~[~str]>().connect("\n"))
|
2013-09-17 20:42:23 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Splits a string into substrings with possibly internal whitespace,
|
|
|
|
|
/// each of them at most `lim` bytes long. The substrings have leading and trailing
|
|
|
|
|
/// whitespace removed, and are only cut at whitespace boundaries.
|
|
|
|
|
///
|
|
|
|
|
/// Note: Function was moved here from `std::str` because this module is the only place that
|
|
|
|
|
/// uses it, and because it was to specific for a general string function.
|
|
|
|
|
///
|
|
|
|
|
/// #Failure:
|
|
|
|
|
///
|
|
|
|
|
/// Fails during iteration if the string contains a non-whitespace
|
|
|
|
|
/// sequence longer than the limit.
|
2013-11-18 23:54:13 -06:00
|
|
|
|
fn each_split_within<'a>(ss: &'a str, lim: uint, it: |&'a str| -> bool)
|
|
|
|
|
-> bool {
|
2013-09-17 20:42:23 -05:00
|
|
|
|
// Just for fun, let's write this as a state machine:
|
2013-07-27 16:38:38 -05:00
|
|
|
|
|
|
|
|
|
enum SplitWithinState {
|
|
|
|
|
A, // leading whitespace, initial state
|
|
|
|
|
B, // words
|
|
|
|
|
C, // internal and trailing whitespace
|
|
|
|
|
}
|
|
|
|
|
enum Whitespace {
|
|
|
|
|
Ws, // current char is whitespace
|
|
|
|
|
Cr // current char is not whitespace
|
|
|
|
|
}
|
|
|
|
|
enum LengthLimit {
|
|
|
|
|
UnderLim, // current char makes current substring still fit in limit
|
|
|
|
|
OverLim // current char makes current substring no longer fit in limit
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let mut slice_start = 0;
|
|
|
|
|
let mut last_start = 0;
|
|
|
|
|
let mut last_end = 0;
|
|
|
|
|
let mut state = A;
|
|
|
|
|
let mut fake_i = ss.len();
|
|
|
|
|
let mut lim = lim;
|
|
|
|
|
|
|
|
|
|
let mut cont = true;
|
2013-11-18 23:54:13 -06:00
|
|
|
|
let slice: || = || { cont = it(ss.slice(slice_start, last_end)) };
|
2013-07-27 16:38:38 -05:00
|
|
|
|
|
|
|
|
|
// if the limit is larger than the string, lower it to save cycles
|
|
|
|
|
if (lim >= fake_i) {
|
|
|
|
|
lim = fake_i;
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-18 23:54:13 -06:00
|
|
|
|
let machine: |(uint, char)| -> bool = |(i, c)| {
|
2013-07-27 16:38:38 -05:00
|
|
|
|
let whitespace = if ::std::char::is_whitespace(c) { Ws } else { Cr };
|
|
|
|
|
let limit = if (i - slice_start + 1) <= lim { UnderLim } else { OverLim };
|
|
|
|
|
|
|
|
|
|
state = match (state, whitespace, limit) {
|
|
|
|
|
(A, Ws, _) => { A }
|
|
|
|
|
(A, Cr, _) => { slice_start = i; last_start = i; B }
|
|
|
|
|
|
|
|
|
|
(B, Cr, UnderLim) => { B }
|
|
|
|
|
(B, Cr, OverLim) if (i - last_start + 1) > lim
|
2013-10-21 15:08:31 -05:00
|
|
|
|
=> fail!("word starting with {} longer than limit!",
|
2013-07-27 16:38:38 -05:00
|
|
|
|
ss.slice(last_start, i + 1)),
|
|
|
|
|
(B, Cr, OverLim) => { slice(); slice_start = last_start; B }
|
|
|
|
|
(B, Ws, UnderLim) => { last_end = i; C }
|
|
|
|
|
(B, Ws, OverLim) => { last_end = i; slice(); A }
|
|
|
|
|
|
|
|
|
|
(C, Cr, UnderLim) => { last_start = i; B }
|
|
|
|
|
(C, Cr, OverLim) => { slice(); slice_start = i; last_start = i; last_end = i; B }
|
|
|
|
|
(C, Ws, OverLim) => { slice(); A }
|
|
|
|
|
(C, Ws, UnderLim) => { C }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
cont
|
|
|
|
|
};
|
|
|
|
|
|
2013-11-23 04:18:51 -06:00
|
|
|
|
ss.char_indices().advance(|x| machine(x));
|
2013-07-27 16:38:38 -05:00
|
|
|
|
|
|
|
|
|
// Let the automaton 'run out' by supplying trailing whitespace
|
|
|
|
|
while cont && match state { B | C => true, A => false } {
|
|
|
|
|
machine((fake_i, ' '));
|
|
|
|
|
fake_i += 1;
|
|
|
|
|
}
|
|
|
|
|
return cont;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2013-08-07 01:03:31 -05:00
|
|
|
|
fn test_split_within() {
|
2013-07-27 16:38:38 -05:00
|
|
|
|
fn t(s: &str, i: uint, u: &[~str]) {
|
|
|
|
|
let mut v = ~[];
|
2013-11-20 17:46:49 -06:00
|
|
|
|
each_split_within(s, i, |s| { v.push(s.to_owned()); true });
|
2013-07-27 16:38:38 -05:00
|
|
|
|
assert!(v.iter().zip(u.iter()).all(|(a,b)| a == b));
|
|
|
|
|
}
|
|
|
|
|
t("", 0, []);
|
|
|
|
|
t("", 15, []);
|
|
|
|
|
t("hello", 15, [~"hello"]);
|
|
|
|
|
t("\nMary had a little lamb\nLittle lamb\n", 15,
|
|
|
|
|
[~"Mary had a", ~"little lamb", ~"Little lamb"]);
|
|
|
|
|
t("\nMary had a little lamb\nLittle lamb\n", ::std::uint::max_value,
|
|
|
|
|
[~"Mary had a little lamb\nLittle lamb"]);
|
|
|
|
|
}
|
2012-10-11 18:54:31 -05:00
|
|
|
|
} // end groups module
|
|
|
|
|
|
2012-01-17 21:05:07 -06:00
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
2013-01-08 21:37:25 -06:00
|
|
|
|
|
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
|
|
|
|
|
2013-06-28 17:32:26 -05:00
|
|
|
|
use std::result::{Err, Ok};
|
|
|
|
|
use std::result;
|
2012-01-17 21:05:07 -06:00
|
|
|
|
|
2013-04-15 10:08:52 -05:00
|
|
|
|
fn check_fail_type(f: Fail_, ft: FailType) {
|
2012-08-06 14:34:08 -05:00
|
|
|
|
match f {
|
2013-03-28 20:39:09 -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]
|
2013-04-15 10:08:52 -05:00
|
|
|
|
fn test_reqopt_long() {
|
2012-07-14 00:57:48 -05:00
|
|
|
|
let args = ~[~"--test=20"];
|
2013-05-23 11:39:00 -05:00
|
|
|
|
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) => {
|
2013-09-17 20:42:23 -05:00
|
|
|
|
assert!(m.opt_present("test"));
|
|
|
|
|
assert_eq!(m.opt_str("test").unwrap(), ~"20");
|
2012-01-17 21:05:07 -06:00
|
|
|
|
}
|
2013-10-21 15:08:31 -05:00
|
|
|
|
_ => { fail!("test_reqopt_long failed"); }
|
2012-01-17 21:05:07 -06:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
|
fn test_reqopt_long_missing() {
|
2012-07-14 00:57:48 -05:00
|
|
|
|
let args = ~[~"blah"];
|
2013-05-23 11:39:00 -05:00
|
|
|
|
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 {
|
2013-05-29 18:59:33 -05:00
|
|
|
|
Err(f) => check_fail_type(f, OptionMissing_),
|
2013-10-21 15:08:31 -05:00
|
|
|
|
_ => fail!()
|
2012-01-17 21:05:07 -06:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
|
fn test_reqopt_long_no_arg() {
|
2012-07-14 00:57:48 -05:00
|
|
|
|
let args = ~[~"--test"];
|
2013-05-23 11:39:00 -05:00
|
|
|
|
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 {
|
2013-05-29 18:59:33 -05:00
|
|
|
|
Err(f) => check_fail_type(f, ArgumentMissing_),
|
2013-10-21 15:08:31 -05:00
|
|
|
|
_ => fail!()
|
2012-01-17 21:05:07 -06:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
|
fn test_reqopt_long_multi() {
|
2012-07-14 00:57:48 -05:00
|
|
|
|
let args = ~[~"--test=20", ~"--test=30"];
|
2013-05-23 11:39:00 -05:00
|
|
|
|
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 {
|
2013-05-29 18:59:33 -05:00
|
|
|
|
Err(f) => check_fail_type(f, OptionDuplicated_),
|
2013-10-21 15:08:31 -05:00
|
|
|
|
_ => fail!()
|
2012-01-17 21:05:07 -06:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
|
fn test_reqopt_short() {
|
2012-07-14 00:57:48 -05:00
|
|
|
|
let args = ~[~"-t", ~"20"];
|
2013-05-23 11:39:00 -05:00
|
|
|
|
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) => {
|
2013-09-17 20:42:23 -05:00
|
|
|
|
assert!(m.opt_present("t"));
|
|
|
|
|
assert_eq!(m.opt_str("t").unwrap(), ~"20");
|
2012-01-17 21:05:07 -06:00
|
|
|
|
}
|
2013-10-21 15:08:31 -05:00
|
|
|
|
_ => fail!()
|
2012-01-17 21:05:07 -06:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
|
fn test_reqopt_short_missing() {
|
2012-07-14 00:57:48 -05:00
|
|
|
|
let args = ~[~"blah"];
|
2013-05-23 11:39:00 -05:00
|
|
|
|
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 {
|
2013-05-29 18:59:33 -05:00
|
|
|
|
Err(f) => check_fail_type(f, OptionMissing_),
|
2013-10-21 15:08:31 -05:00
|
|
|
|
_ => fail!()
|
2012-01-17 21:05:07 -06:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
|
fn test_reqopt_short_no_arg() {
|
2012-07-14 00:57:48 -05:00
|
|
|
|
let args = ~[~"-t"];
|
2013-05-23 11:39:00 -05:00
|
|
|
|
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 {
|
2013-05-29 18:59:33 -05:00
|
|
|
|
Err(f) => check_fail_type(f, ArgumentMissing_),
|
2013-10-21 15:08:31 -05:00
|
|
|
|
_ => fail!()
|
2012-01-17 21:05:07 -06:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
|
fn test_reqopt_short_multi() {
|
2012-07-14 00:57:48 -05:00
|
|
|
|
let args = ~[~"-t", ~"20", ~"-t", ~"30"];
|
2013-05-23 11:39:00 -05:00
|
|
|
|
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 {
|
2013-05-29 18:59:33 -05:00
|
|
|
|
Err(f) => check_fail_type(f, OptionDuplicated_),
|
2013-10-21 15:08:31 -05:00
|
|
|
|
_ => fail!()
|
2012-01-17 21:05:07 -06:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Tests for optopt
|
|
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
|
fn test_optopt_long() {
|
2012-07-14 00:57:48 -05:00
|
|
|
|
let args = ~[~"--test=20"];
|
2013-05-23 11:39:00 -05:00
|
|
|
|
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) => {
|
2013-09-17 20:42:23 -05:00
|
|
|
|
assert!(m.opt_present("test"));
|
|
|
|
|
assert_eq!(m.opt_str("test").unwrap(), ~"20");
|
2012-01-17 21:05:07 -06:00
|
|
|
|
}
|
2013-10-21 15:08:31 -05:00
|
|
|
|
_ => fail!()
|
2012-01-17 21:05:07 -06:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
|
fn test_optopt_long_missing() {
|
2012-07-14 00:57:48 -05:00
|
|
|
|
let args = ~[~"blah"];
|
2013-05-23 11:39:00 -05:00
|
|
|
|
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 {
|
2013-09-17 20:42:23 -05:00
|
|
|
|
Ok(ref m) => assert!(!m.opt_present("test")),
|
2013-10-21 15:08:31 -05:00
|
|
|
|
_ => fail!()
|
2012-01-17 21:05:07 -06:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
|
fn test_optopt_long_no_arg() {
|
2012-07-14 00:57:48 -05:00
|
|
|
|
let args = ~[~"--test"];
|
2013-05-23 11:39:00 -05:00
|
|
|
|
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 {
|
2013-05-29 18:59:33 -05:00
|
|
|
|
Err(f) => check_fail_type(f, ArgumentMissing_),
|
2013-10-21 15:08:31 -05:00
|
|
|
|
_ => fail!()
|
2012-01-17 21:05:07 -06:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
|
fn test_optopt_long_multi() {
|
2012-07-14 00:57:48 -05:00
|
|
|
|
let args = ~[~"--test=20", ~"--test=30"];
|
2013-05-23 11:39:00 -05:00
|
|
|
|
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 {
|
2013-05-29 18:59:33 -05:00
|
|
|
|
Err(f) => check_fail_type(f, OptionDuplicated_),
|
2013-10-21 15:08:31 -05:00
|
|
|
|
_ => fail!()
|
2012-01-17 21:05:07 -06:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
|
fn test_optopt_short() {
|
2012-07-14 00:57:48 -05:00
|
|
|
|
let args = ~[~"-t", ~"20"];
|
2013-05-23 11:39:00 -05:00
|
|
|
|
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) => {
|
2013-09-17 20:42:23 -05:00
|
|
|
|
assert!((m.opt_present("t")));
|
|
|
|
|
assert_eq!(m.opt_str("t").unwrap(), ~"20");
|
2012-01-17 21:05:07 -06:00
|
|
|
|
}
|
2013-10-21 15:08:31 -05:00
|
|
|
|
_ => fail!()
|
2012-01-17 21:05:07 -06:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
|
fn test_optopt_short_missing() {
|
2012-07-14 00:57:48 -05:00
|
|
|
|
let args = ~[~"blah"];
|
2013-05-23 11:39:00 -05:00
|
|
|
|
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 {
|
2013-09-17 20:42:23 -05:00
|
|
|
|
Ok(ref m) => assert!(!m.opt_present("t")),
|
2013-10-21 15:08:31 -05:00
|
|
|
|
_ => fail!()
|
2012-01-17 21:05:07 -06:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
|
fn test_optopt_short_no_arg() {
|
2012-07-14 00:57:48 -05:00
|
|
|
|
let args = ~[~"-t"];
|
2013-05-23 11:39:00 -05:00
|
|
|
|
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 {
|
2013-05-29 18:59:33 -05:00
|
|
|
|
Err(f) => check_fail_type(f, ArgumentMissing_),
|
2013-10-21 15:08:31 -05:00
|
|
|
|
_ => fail!()
|
2012-01-17 21:05:07 -06:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
|
fn test_optopt_short_multi() {
|
2012-07-14 00:57:48 -05:00
|
|
|
|
let args = ~[~"-t", ~"20", ~"-t", ~"30"];
|
2013-05-23 11:39:00 -05:00
|
|
|
|
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 {
|
2013-05-29 18:59:33 -05:00
|
|
|
|
Err(f) => check_fail_type(f, OptionDuplicated_),
|
2013-10-21 15:08:31 -05:00
|
|
|
|
_ => fail!()
|
2012-01-17 21:05:07 -06:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Tests for optflag
|
|
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
|
fn test_optflag_long() {
|
2012-07-14 00:57:48 -05:00
|
|
|
|
let args = ~[~"--test"];
|
2013-05-23 11:39:00 -05:00
|
|
|
|
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 {
|
2013-09-17 20:42:23 -05:00
|
|
|
|
Ok(ref m) => assert!(m.opt_present("test")),
|
2013-10-21 15:08:31 -05:00
|
|
|
|
_ => fail!()
|
2012-01-17 21:05:07 -06:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
|
fn test_optflag_long_missing() {
|
2012-07-14 00:57:48 -05:00
|
|
|
|
let args = ~[~"blah"];
|
2013-05-23 11:39:00 -05:00
|
|
|
|
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 {
|
2013-09-17 20:42:23 -05:00
|
|
|
|
Ok(ref m) => assert!(!m.opt_present("test")),
|
2013-10-21 15:08:31 -05:00
|
|
|
|
_ => fail!()
|
2012-01-17 21:05:07 -06:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
|
fn test_optflag_long_arg() {
|
2012-07-14 00:57:48 -05:00
|
|
|
|
let args = ~[~"--test=20"];
|
2013-05-23 11:39:00 -05:00
|
|
|
|
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 {
|
2013-05-29 18:59:33 -05:00
|
|
|
|
Err(f) => {
|
2013-10-21 15:08:31 -05:00
|
|
|
|
error!("{:?}", f.clone().to_err_msg());
|
2012-08-11 09:08:42 -05:00
|
|
|
|
check_fail_type(f, UnexpectedArgument_);
|
2012-01-17 21:05:07 -06:00
|
|
|
|
}
|
2013-10-21 15:08:31 -05:00
|
|
|
|
_ => fail!()
|
2012-01-17 21:05:07 -06:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
|
fn test_optflag_long_multi() {
|
2012-07-14 00:57:48 -05:00
|
|
|
|
let args = ~[~"--test", ~"--test"];
|
2013-05-23 11:39:00 -05:00
|
|
|
|
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 {
|
2013-05-29 18:59:33 -05:00
|
|
|
|
Err(f) => check_fail_type(f, OptionDuplicated_),
|
2013-10-21 15:08:31 -05:00
|
|
|
|
_ => fail!()
|
2012-01-17 21:05:07 -06:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
|
fn test_optflag_short() {
|
2012-07-14 00:57:48 -05:00
|
|
|
|
let args = ~[~"-t"];
|
2013-05-23 11:39:00 -05:00
|
|
|
|
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 {
|
2013-09-17 20:42:23 -05:00
|
|
|
|
Ok(ref m) => assert!(m.opt_present("t")),
|
2013-10-21 15:08:31 -05:00
|
|
|
|
_ => fail!()
|
2012-01-17 21:05:07 -06:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
|
fn test_optflag_short_missing() {
|
2012-07-14 00:57:48 -05:00
|
|
|
|
let args = ~[~"blah"];
|
2013-05-23 11:39:00 -05:00
|
|
|
|
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 {
|
2013-09-17 20:42:23 -05:00
|
|
|
|
Ok(ref m) => assert!(!m.opt_present("t")),
|
2013-10-21 15:08:31 -05:00
|
|
|
|
_ => fail!()
|
2012-01-17 21:05:07 -06:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
|
fn test_optflag_short_arg() {
|
2012-07-14 00:57:48 -05:00
|
|
|
|
let args = ~[~"-t", ~"20"];
|
2013-05-23 11:39:00 -05:00
|
|
|
|
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
|
|
|
|
|
|
2013-05-18 21:02:45 -05:00
|
|
|
|
assert!(m.free[0] == ~"20");
|
2012-01-17 21:05:07 -06:00
|
|
|
|
}
|
2013-10-21 15:08:31 -05:00
|
|
|
|
_ => fail!()
|
2012-01-17 21:05:07 -06:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
|
fn test_optflag_short_multi() {
|
2012-07-14 00:57:48 -05:00
|
|
|
|
let args = ~[~"-t", ~"-t"];
|
2013-05-23 11:39:00 -05:00
|
|
|
|
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 {
|
2013-05-29 18:59:33 -05:00
|
|
|
|
Err(f) => check_fail_type(f, OptionDuplicated_),
|
2013-10-21 15:08:31 -05:00
|
|
|
|
_ => fail!()
|
2012-01-17 21:05:07 -06:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-17 21:43:39 -06:00
|
|
|
|
// Tests for optflagmulti
|
|
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
|
fn test_optflagmulti_short1() {
|
2012-11-17 21:43:39 -06:00
|
|
|
|
let args = ~[~"-v"];
|
2013-05-23 11:39:00 -05:00
|
|
|
|
let opts = ~[optflagmulti("v")];
|
2012-11-17 21:43:39 -06:00
|
|
|
|
let rs = getopts(args, opts);
|
|
|
|
|
match rs {
|
2012-11-24 14:49:31 -06:00
|
|
|
|
Ok(ref m) => {
|
2013-09-17 20:42:23 -05:00
|
|
|
|
assert_eq!(m.opt_count("v"), 1);
|
2012-11-17 21:43:39 -06:00
|
|
|
|
}
|
2013-10-21 15:08:31 -05:00
|
|
|
|
_ => fail!()
|
2012-11-17 21:43:39 -06:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
|
fn test_optflagmulti_short2a() {
|
2012-11-17 21:43:39 -06:00
|
|
|
|
let args = ~[~"-v", ~"-v"];
|
2013-05-23 11:39:00 -05:00
|
|
|
|
let opts = ~[optflagmulti("v")];
|
2012-11-17 21:43:39 -06:00
|
|
|
|
let rs = getopts(args, opts);
|
|
|
|
|
match rs {
|
2012-11-24 14:49:31 -06:00
|
|
|
|
Ok(ref m) => {
|
2013-09-17 20:42:23 -05:00
|
|
|
|
assert_eq!(m.opt_count("v"), 2);
|
2012-11-17 21:43:39 -06:00
|
|
|
|
}
|
2013-10-21 15:08:31 -05:00
|
|
|
|
_ => fail!()
|
2012-11-17 21:43:39 -06:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
|
fn test_optflagmulti_short2b() {
|
2012-11-17 21:43:39 -06:00
|
|
|
|
let args = ~[~"-vv"];
|
2013-05-23 11:39:00 -05:00
|
|
|
|
let opts = ~[optflagmulti("v")];
|
2012-11-17 21:43:39 -06:00
|
|
|
|
let rs = getopts(args, opts);
|
|
|
|
|
match rs {
|
2012-11-24 14:49:31 -06:00
|
|
|
|
Ok(ref m) => {
|
2013-09-17 20:42:23 -05:00
|
|
|
|
assert_eq!(m.opt_count("v"), 2);
|
2012-11-17 21:43:39 -06:00
|
|
|
|
}
|
2013-10-21 15:08:31 -05:00
|
|
|
|
_ => fail!()
|
2012-11-17 21:43:39 -06:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
|
fn test_optflagmulti_long1() {
|
2012-11-17 21:43:39 -06:00
|
|
|
|
let args = ~[~"--verbose"];
|
2013-05-23 11:39:00 -05:00
|
|
|
|
let opts = ~[optflagmulti("verbose")];
|
2012-11-17 21:43:39 -06:00
|
|
|
|
let rs = getopts(args, opts);
|
|
|
|
|
match rs {
|
2012-11-24 14:49:31 -06:00
|
|
|
|
Ok(ref m) => {
|
2013-09-17 20:42:23 -05:00
|
|
|
|
assert_eq!(m.opt_count("verbose"), 1);
|
2012-11-17 21:43:39 -06:00
|
|
|
|
}
|
2013-10-21 15:08:31 -05:00
|
|
|
|
_ => fail!()
|
2012-11-17 21:43:39 -06:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
|
fn test_optflagmulti_long2() {
|
2012-11-17 21:43:39 -06:00
|
|
|
|
let args = ~[~"--verbose", ~"--verbose"];
|
2013-05-23 11:39:00 -05:00
|
|
|
|
let opts = ~[optflagmulti("verbose")];
|
2012-11-17 21:43:39 -06:00
|
|
|
|
let rs = getopts(args, opts);
|
|
|
|
|
match rs {
|
2012-11-24 14:49:31 -06:00
|
|
|
|
Ok(ref m) => {
|
2013-09-17 20:42:23 -05:00
|
|
|
|
assert_eq!(m.opt_count("verbose"), 2);
|
2012-11-17 21:43:39 -06:00
|
|
|
|
}
|
2013-10-21 15:08:31 -05:00
|
|
|
|
_ => fail!()
|
2012-11-17 21:43:39 -06:00
|
|
|
|
}
|
|
|
|
|
}
|
2012-01-17 21:05:07 -06:00
|
|
|
|
|
|
|
|
|
// Tests for optmulti
|
|
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
|
fn test_optmulti_long() {
|
2012-07-14 00:57:48 -05:00
|
|
|
|
let args = ~[~"--test=20"];
|
2013-05-23 11:39:00 -05:00
|
|
|
|
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) => {
|
2013-09-17 20:42:23 -05:00
|
|
|
|
assert!((m.opt_present("test")));
|
|
|
|
|
assert_eq!(m.opt_str("test").unwrap(), ~"20");
|
2012-01-17 21:05:07 -06:00
|
|
|
|
}
|
2013-10-21 15:08:31 -05:00
|
|
|
|
_ => fail!()
|
2012-01-17 21:05:07 -06:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
|
fn test_optmulti_long_missing() {
|
2012-07-14 00:57:48 -05:00
|
|
|
|
let args = ~[~"blah"];
|
2013-05-23 11:39:00 -05:00
|
|
|
|
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 {
|
2013-09-17 20:42:23 -05:00
|
|
|
|
Ok(ref m) => assert!(!m.opt_present("test")),
|
2013-10-21 15:08:31 -05:00
|
|
|
|
_ => fail!()
|
2012-01-17 21:05:07 -06:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
|
fn test_optmulti_long_no_arg() {
|
2012-07-14 00:57:48 -05:00
|
|
|
|
let args = ~[~"--test"];
|
2013-05-23 11:39:00 -05:00
|
|
|
|
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 {
|
2013-05-29 18:59:33 -05:00
|
|
|
|
Err(f) => check_fail_type(f, ArgumentMissing_),
|
2013-10-21 15:08:31 -05:00
|
|
|
|
_ => fail!()
|
2012-01-17 21:05:07 -06:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
|
fn test_optmulti_long_multi() {
|
2012-07-14 00:57:48 -05:00
|
|
|
|
let args = ~[~"--test=20", ~"--test=30"];
|
2013-05-23 11:39:00 -05:00
|
|
|
|
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) => {
|
2013-09-17 20:42:23 -05:00
|
|
|
|
assert!(m.opt_present("test"));
|
|
|
|
|
assert_eq!(m.opt_str("test").unwrap(), ~"20");
|
|
|
|
|
let pair = m.opt_strs("test");
|
2013-05-18 21:02:45 -05:00
|
|
|
|
assert!(pair[0] == ~"20");
|
|
|
|
|
assert!(pair[1] == ~"30");
|
2012-01-17 21:05:07 -06:00
|
|
|
|
}
|
2013-10-21 15:08:31 -05:00
|
|
|
|
_ => fail!()
|
2012-01-17 21:05:07 -06:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
|
fn test_optmulti_short() {
|
2012-07-14 00:57:48 -05:00
|
|
|
|
let args = ~[~"-t", ~"20"];
|
2013-05-23 11:39:00 -05:00
|
|
|
|
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) => {
|
2013-09-17 20:42:23 -05:00
|
|
|
|
assert!((m.opt_present("t")));
|
|
|
|
|
assert_eq!(m.opt_str("t").unwrap(), ~"20");
|
2012-01-17 21:05:07 -06:00
|
|
|
|
}
|
2013-10-21 15:08:31 -05:00
|
|
|
|
_ => fail!()
|
2012-01-17 21:05:07 -06:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
|
fn test_optmulti_short_missing() {
|
2012-07-14 00:57:48 -05:00
|
|
|
|
let args = ~[~"blah"];
|
2013-05-23 11:39:00 -05:00
|
|
|
|
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 {
|
2013-09-17 20:42:23 -05:00
|
|
|
|
Ok(ref m) => assert!(!m.opt_present("t")),
|
2013-10-21 15:08:31 -05:00
|
|
|
|
_ => fail!()
|
2012-01-17 21:05:07 -06:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
|
fn test_optmulti_short_no_arg() {
|
2012-07-14 00:57:48 -05:00
|
|
|
|
let args = ~[~"-t"];
|
2013-05-23 11:39:00 -05:00
|
|
|
|
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 {
|
2013-05-29 18:59:33 -05:00
|
|
|
|
Err(f) => check_fail_type(f, ArgumentMissing_),
|
2013-10-21 15:08:31 -05:00
|
|
|
|
_ => fail!()
|
2012-01-17 21:05:07 -06:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
|
fn test_optmulti_short_multi() {
|
2012-07-14 00:57:48 -05:00
|
|
|
|
let args = ~[~"-t", ~"20", ~"-t", ~"30"];
|
2013-05-23 11:39:00 -05:00
|
|
|
|
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) => {
|
2013-09-17 20:42:23 -05:00
|
|
|
|
assert!((m.opt_present("t")));
|
|
|
|
|
assert_eq!(m.opt_str("t").unwrap(), ~"20");
|
|
|
|
|
let pair = m.opt_strs("t");
|
2013-05-18 21:02:45 -05:00
|
|
|
|
assert!(pair[0] == ~"20");
|
|
|
|
|
assert!(pair[1] == ~"30");
|
2012-01-17 21:05:07 -06:00
|
|
|
|
}
|
2013-10-21 15:08:31 -05:00
|
|
|
|
_ => fail!()
|
2012-01-17 21:05:07 -06:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
|
fn test_unrecognized_option_long() {
|
2012-07-14 00:57:48 -05:00
|
|
|
|
let args = ~[~"--untest"];
|
2013-05-23 11:39:00 -05:00
|
|
|
|
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 {
|
2013-05-29 18:59:33 -05:00
|
|
|
|
Err(f) => check_fail_type(f, UnrecognizedOption_),
|
2013-10-21 15:08:31 -05:00
|
|
|
|
_ => fail!()
|
2012-01-17 21:05:07 -06:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
|
fn test_unrecognized_option_short() {
|
2012-07-14 00:57:48 -05:00
|
|
|
|
let args = ~[~"-t"];
|
2013-05-23 11:39:00 -05:00
|
|
|
|
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 {
|
2013-05-29 18:59:33 -05:00
|
|
|
|
Err(f) => check_fail_type(f, UnrecognizedOption_),
|
2013-10-21 15:08:31 -05:00
|
|
|
|
_ => fail!()
|
2012-01-17 21:05:07 -06:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
|
fn test_combined() {
|
2012-01-17 21:05:07 -06:00
|
|
|
|
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 =
|
2013-05-23 11:39:00 -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) => {
|
2013-05-18 21:02:45 -05:00
|
|
|
|
assert!(m.free[0] == ~"prog");
|
|
|
|
|
assert!(m.free[1] == ~"free1");
|
2013-09-17 20:42:23 -05:00
|
|
|
|
assert_eq!(m.opt_str("s").unwrap(), ~"20");
|
2013-05-18 21:02:45 -05:00
|
|
|
|
assert!(m.free[2] == ~"free2");
|
2013-09-17 20:42:23 -05:00
|
|
|
|
assert!((m.opt_present("flag")));
|
|
|
|
|
assert_eq!(m.opt_str("long").unwrap(), ~"30");
|
|
|
|
|
assert!((m.opt_present("f")));
|
|
|
|
|
let pair = m.opt_strs("m");
|
2013-05-18 21:02:45 -05:00
|
|
|
|
assert!(pair[0] == ~"40");
|
|
|
|
|
assert!(pair[1] == ~"50");
|
2013-09-17 20:42:23 -05:00
|
|
|
|
let pair = m.opt_strs("n");
|
2013-05-18 21:02:45 -05:00
|
|
|
|
assert!(pair[0] == ~"-A B");
|
|
|
|
|
assert!(pair[1] == ~"-60 70");
|
2013-09-17 20:42:23 -05:00
|
|
|
|
assert!((!m.opt_present("notpresent")));
|
2012-01-17 21:05:07 -06:00
|
|
|
|
}
|
2013-10-21 15:08:31 -05:00
|
|
|
|
_ => fail!()
|
2012-01-17 21:05:07 -06:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-31 19:02:03 -05:00
|
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
|
fn test_multi() {
|
2013-05-23 11:39:00 -05:00
|
|
|
|
let opts = ~[optopt("e"), optopt("encrypt"), optopt("f")];
|
2013-07-30 14:23:19 -05:00
|
|
|
|
|
|
|
|
|
let args_single = ~[~"-e", ~"foo"];
|
|
|
|
|
let matches_single = &match getopts(args_single, opts) {
|
|
|
|
|
result::Ok(m) => m,
|
2013-10-21 15:08:31 -05:00
|
|
|
|
result::Err(_) => fail!()
|
2013-07-30 14:23:19 -05:00
|
|
|
|
};
|
2013-09-17 20:42:23 -05:00
|
|
|
|
assert!(matches_single.opts_present([~"e"]));
|
|
|
|
|
assert!(matches_single.opts_present([~"encrypt", ~"e"]));
|
|
|
|
|
assert!(matches_single.opts_present([~"e", ~"encrypt"]));
|
|
|
|
|
assert!(!matches_single.opts_present([~"encrypt"]));
|
|
|
|
|
assert!(!matches_single.opts_present([~"thing"]));
|
|
|
|
|
assert!(!matches_single.opts_present([]));
|
2013-07-30 14:23:19 -05:00
|
|
|
|
|
2013-09-17 20:42:23 -05:00
|
|
|
|
assert_eq!(matches_single.opts_str([~"e"]).unwrap(), ~"foo");
|
|
|
|
|
assert_eq!(matches_single.opts_str([~"e", ~"encrypt"]).unwrap(), ~"foo");
|
|
|
|
|
assert_eq!(matches_single.opts_str([~"encrypt", ~"e"]).unwrap(), ~"foo");
|
2013-07-30 14:23:19 -05:00
|
|
|
|
|
|
|
|
|
let args_both = ~[~"-e", ~"foo", ~"--encrypt", ~"foo"];
|
|
|
|
|
let matches_both = &match getopts(args_both, opts) {
|
2013-02-15 01:30:30 -06:00
|
|
|
|
result::Ok(m) => m,
|
2013-10-21 15:08:31 -05:00
|
|
|
|
result::Err(_) => fail!()
|
2012-05-31 19:02:03 -05:00
|
|
|
|
};
|
2013-09-17 20:42:23 -05:00
|
|
|
|
assert!(matches_both.opts_present([~"e"]));
|
|
|
|
|
assert!(matches_both.opts_present([~"encrypt"]));
|
|
|
|
|
assert!(matches_both.opts_present([~"encrypt", ~"e"]));
|
|
|
|
|
assert!(matches_both.opts_present([~"e", ~"encrypt"]));
|
|
|
|
|
assert!(!matches_both.opts_present([~"f"]));
|
|
|
|
|
assert!(!matches_both.opts_present([~"thing"]));
|
|
|
|
|
assert!(!matches_both.opts_present([]));
|
2013-07-30 14:23:19 -05:00
|
|
|
|
|
2013-09-17 20:42:23 -05:00
|
|
|
|
assert_eq!(matches_both.opts_str([~"e"]).unwrap(), ~"foo");
|
|
|
|
|
assert_eq!(matches_both.opts_str([~"encrypt"]).unwrap(), ~"foo");
|
|
|
|
|
assert_eq!(matches_both.opts_str([~"e", ~"encrypt"]).unwrap(), ~"foo");
|
|
|
|
|
assert_eq!(matches_both.opts_str([~"encrypt", ~"e"]).unwrap(), ~"foo");
|
2012-05-31 19:02:03 -05:00
|
|
|
|
}
|
2012-07-04 10:36:31 -05:00
|
|
|
|
|
|
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
|
fn test_nospace() {
|
2012-12-07 21:53:45 -06:00
|
|
|
|
let args = ~[~"-Lfoo", ~"-M."];
|
2013-05-23 11:39:00 -05:00
|
|
|
|
let opts = ~[optmulti("L"), optmulti("M")];
|
2012-11-24 14:49:31 -06:00
|
|
|
|
let matches = &match getopts(args, opts) {
|
2013-02-15 01:30:30 -06:00
|
|
|
|
result::Ok(m) => m,
|
2013-10-21 15:08:31 -05:00
|
|
|
|
result::Err(_) => fail!()
|
2012-07-04 10:36:31 -05:00
|
|
|
|
};
|
2013-09-17 20:42:23 -05:00
|
|
|
|
assert!(matches.opts_present([~"L"]));
|
|
|
|
|
assert_eq!(matches.opts_str([~"L"]).unwrap(), ~"foo");
|
|
|
|
|
assert!(matches.opts_present([~"M"]));
|
|
|
|
|
assert_eq!(matches.opts_str([~"M"]).unwrap(), ~".");
|
2012-12-07 21:53:45 -06:00
|
|
|
|
|
2012-07-04 10:36:31 -05:00
|
|
|
|
}
|
2012-10-11 18:54:31 -05:00
|
|
|
|
|
|
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
|
fn test_groups_reqopt() {
|
2013-05-23 11:39:00 -05:00
|
|
|
|
let opt = groups::reqopt("b", "banana", "some bananas", "VAL");
|
2013-03-28 20:39:09 -05: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,
|
2013-03-06 15:58:02 -06:00
|
|
|
|
occur: Req })
|
2012-10-11 18:54:31 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
|
fn test_groups_optopt() {
|
2013-05-23 11:39:00 -05:00
|
|
|
|
let opt = groups::optopt("a", "apple", "some apples", "VAL");
|
2013-03-28 20:39:09 -05: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,
|
2013-03-06 15:58:02 -06:00
|
|
|
|
occur: Optional })
|
2012-10-11 18:54:31 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
|
fn test_groups_optflag() {
|
2013-05-23 11:39:00 -05:00
|
|
|
|
let opt = groups::optflag("k", "kiwi", "some kiwis");
|
2013-03-28 20:39:09 -05:00
|
|
|
|
assert!(opt == OptGroup { short_name: ~"k",
|
2012-10-11 18:54:31 -05:00
|
|
|
|
long_name: ~"kiwi",
|
|
|
|
|
hint: ~"",
|
|
|
|
|
desc: ~"some kiwis",
|
|
|
|
|
hasarg: No,
|
2013-03-06 15:58:02 -06:00
|
|
|
|
occur: Optional })
|
2012-10-11 18:54:31 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
|
fn test_groups_optflagopt() {
|
2013-05-23 11:39:00 -05:00
|
|
|
|
let opt = groups::optflagopt("p", "pineapple", "some pineapples", "VAL");
|
2013-03-28 20:39:09 -05: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,
|
2013-03-06 15:58:02 -06:00
|
|
|
|
occur: Optional })
|
2012-10-11 18:54:31 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
|
fn test_groups_optmulti() {
|
2013-05-23 11:39:00 -05:00
|
|
|
|
let opt = groups::optmulti("l", "lime", "some limes", "VAL");
|
2013-03-28 20:39:09 -05: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,
|
2013-03-06 15:58:02 -06:00
|
|
|
|
occur: Multi })
|
2012-10-11 18:54:31 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
|
fn test_groups_long_to_short() {
|
2013-08-05 07:37:54 -05:00
|
|
|
|
let mut short = reqopt("banana");
|
|
|
|
|
short.aliases = ~[reqopt("b")];
|
2013-05-23 11:39:00 -05:00
|
|
|
|
let verbose = groups::reqopt("b", "banana", "some bananas", "VAL");
|
2012-10-11 18:54:31 -05:00
|
|
|
|
|
2013-09-17 20:42:23 -05:00
|
|
|
|
assert_eq!(verbose.long_to_short(), short);
|
2012-10-11 18:54:31 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
|
fn test_groups_getopts() {
|
2013-08-05 07:37:54 -05:00
|
|
|
|
let mut banana = reqopt("banana");
|
|
|
|
|
banana.aliases = ~[reqopt("b")];
|
|
|
|
|
let mut apple = optopt("apple");
|
|
|
|
|
apple.aliases = ~[optopt("a")];
|
|
|
|
|
let mut kiwi = optflag("kiwi");
|
|
|
|
|
kiwi.aliases = ~[optflag("k")];
|
2012-10-11 18:54:31 -05:00
|
|
|
|
let short = ~[
|
2013-08-05 07:37:54 -05:00
|
|
|
|
banana,
|
|
|
|
|
apple,
|
|
|
|
|
kiwi,
|
2013-05-23 11:39:00 -05:00
|
|
|
|
optflagopt("p"),
|
|
|
|
|
optmulti("l")
|
2012-10-11 18:54:31 -05:00
|
|
|
|
];
|
|
|
|
|
|
2013-12-30 16:07:19 -06:00
|
|
|
|
// short and verbose should always be in the same order. if they
|
|
|
|
|
// aren't the test will fail (and in mysterious ways)
|
|
|
|
|
|
2012-10-11 18:54:31 -05:00
|
|
|
|
let verbose = ~[
|
2013-05-23 11:39:00 -05:00
|
|
|
|
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"),
|
2012-10-11 18:54:31 -05:00
|
|
|
|
];
|
|
|
|
|
|
2013-08-05 07:37:54 -05:00
|
|
|
|
let sample_args = ~[~"--kiwi", ~"15", ~"--apple", ~"1", ~"k",
|
2012-10-11 18:54:31 -05:00
|
|
|
|
~"-p", ~"16", ~"l", ~"35"];
|
|
|
|
|
|
2013-03-28 20:39:09 -05:00
|
|
|
|
assert!(getopts(sample_args, short)
|
2013-03-06 15:58:02 -06:00
|
|
|
|
== groups::getopts(sample_args, verbose));
|
2012-10-11 18:54:31 -05:00
|
|
|
|
}
|
|
|
|
|
|
2013-08-05 07:37:54 -05:00
|
|
|
|
#[test]
|
|
|
|
|
fn test_groups_aliases_long_and_short() {
|
|
|
|
|
let opts = ~[
|
|
|
|
|
groups::optflagmulti("a", "apple", "Desc"),
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
let args = ~[~"-a", ~"--apple", ~"-a"];
|
|
|
|
|
|
|
|
|
|
let matches = groups::getopts(args, opts).unwrap();
|
2013-09-17 20:42:23 -05:00
|
|
|
|
assert_eq!(3, matches.opt_count("a"));
|
|
|
|
|
assert_eq!(3, matches.opt_count("apple"));
|
2013-08-05 07:37:54 -05:00
|
|
|
|
}
|
|
|
|
|
|
2012-10-11 18:54:31 -05:00
|
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
|
fn test_groups_usage() {
|
2012-10-11 18:54:31 -05:00
|
|
|
|
let optgroups = ~[
|
2013-05-23 11:39:00 -05:00
|
|
|
|
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"),
|
2012-10-11 18:54:31 -05:00
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
let expected =
|
|
|
|
|
~"Usage: fruits
|
|
|
|
|
|
|
|
|
|
Options:
|
|
|
|
|
-b --banana VAL Desc
|
|
|
|
|
-a --012345678901234567890123456789 VAL
|
|
|
|
|
Desc
|
|
|
|
|
-k --kiwi Desc
|
|
|
|
|
-p [VAL] Desc
|
|
|
|
|
-l VAL Desc
|
|
|
|
|
";
|
|
|
|
|
|
2013-05-23 11:39:00 -05:00
|
|
|
|
let generated_usage = groups::usage("Usage: fruits", optgroups);
|
2012-10-11 18:54:31 -05:00
|
|
|
|
|
2013-10-21 15:08:31 -05:00
|
|
|
|
debug!("expected: <<{}>>", expected);
|
|
|
|
|
debug!("generated: <<{}>>", generated_usage);
|
2013-05-18 21:02:45 -05:00
|
|
|
|
assert_eq!(generated_usage, expected);
|
2012-10-11 18:54:31 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
|
fn test_groups_usage_description_wrapping() {
|
2012-10-11 18:54:31 -05:00
|
|
|
|
// indentation should be 24 spaces
|
|
|
|
|
// lines wrap after 78: or rather descriptions wrap after 54
|
|
|
|
|
|
|
|
|
|
let optgroups = ~[
|
2013-05-23 11:39:00 -05:00
|
|
|
|
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
|
2012-10-11 18:54:31 -05:00
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
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..+..
|
|
|
|
|
";
|
|
|
|
|
|
2013-05-23 11:39:00 -05:00
|
|
|
|
let usage = groups::usage("Usage: fruits", optgroups);
|
2012-10-11 18:54:31 -05:00
|
|
|
|
|
2013-10-21 15:08:31 -05:00
|
|
|
|
debug!("expected: <<{}>>", expected);
|
|
|
|
|
debug!("generated: <<{}>>", usage);
|
2013-03-28 20:39:09 -05:00
|
|
|
|
assert!(usage == expected)
|
2012-10-11 18:54:31 -05:00
|
|
|
|
}
|
2013-08-24 05:09:18 -05:00
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_groups_usage_description_multibyte_handling() {
|
|
|
|
|
let optgroups = ~[
|
|
|
|
|
groups::optflag("k", "k\u2013w\u2013",
|
|
|
|
|
"The word kiwi is normally spelled with two i's"),
|
|
|
|
|
groups::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."),
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
let expected =
|
|
|
|
|
~"Usage: fruits
|
|
|
|
|
|
|
|
|
|
Options:
|
|
|
|
|
-k --k–w– The word kiwi is normally spelled with two i's
|
|
|
|
|
-a --apple This “description” has some characters that could
|
|
|
|
|
confuse the line wrapping; an apple costs 0.51€ in
|
|
|
|
|
some parts of Europe.
|
|
|
|
|
";
|
|
|
|
|
|
|
|
|
|
let usage = groups::usage("Usage: fruits", optgroups);
|
|
|
|
|
|
2013-10-21 15:08:31 -05:00
|
|
|
|
debug!("expected: <<{}>>", expected);
|
|
|
|
|
debug!("generated: <<{}>>", usage);
|
2013-08-24 05:09:18 -05:00
|
|
|
|
assert!(usage == expected)
|
|
|
|
|
}
|
2012-01-17 21:05:07 -06:00
|
|
|
|
}
|