Merge pull request #101 from kbknapp/patch-99

Fixes #99 as well as other misc fixes
This commit is contained in:
Kevin K. 2015-05-04 22:04:04 -04:00
commit be77f0c5cf
5 changed files with 178 additions and 55 deletions

View file

@ -9,7 +9,7 @@ Kevin K. <kbknapp@gmail.com>
tests clap library
USAGE:
\tclaptests [POSITIONAL] [FLAGS] [OPTIONS] [SUBCOMMANDS]
claptests [POSITIONAL] [FLAGS] [OPTIONS] [SUBCOMMANDS]
FLAGS:
-f, --flag tests flags
@ -18,9 +18,13 @@ FLAGS:
-v, --version Prints version information
OPTIONS:
-o, --option <opt>... tests options
--long-option-2 <option2> tests long options with exclusions
-O <option3> tests options with specific value sets [values: fast slow]
--maxvals3 <maxvals>... Tests 3 max vals
--minvals2 <minvals>... Tests 2 min vals
--multvals <one> <two> Tests mutliple values, not mult occs
--multvalsmo <one> <two> Tests mutliple values, not mult occs
-o, --option <opt>... tests options
--long-option-2 <option2> tests long options with exclusions
-O <option3> tests options with specific value sets [values: fast slow]
POSITIONAL ARGUMENTS:
positional tests positionals
@ -98,7 +102,7 @@ Kevin K. <kbknapp@gmail.com>
tests subcommands
USAGE:
\tclaptests subcmd [POSITIONAL] [FLAGS] [OPTIONS]
claptests subcmd [POSITIONAL] [FLAGS] [OPTIONS]
FLAGS:
-h, --help Prints help information
@ -106,7 +110,7 @@ FLAGS:
-v, --version Prints version information
OPTIONS:
-o, --option <scoption>... tests options
-o, --option <scoption>... tests options
POSITIONAL ARGUMENTS:
scpositional tests positionals'''
@ -143,6 +147,50 @@ scoption present with value: some
An scoption: some
scpositional present with value: value'''
_min_vals_few = '''The argument '--minvals2 <minvals>...' requires at least 2 values, but 1 was provided
USAGE:
\tclaptests --minvals2 <minvals>...
For more information try --help'''
_exact = '''flag NOT present
option NOT present
positional NOT present
flag2 NOT present
option2 maybe present with value of: Nothing
positional2 maybe present with value of: Nothing
option3 NOT present
positional3 NOT present
option NOT present
positional NOT present
subcmd NOT present'''
_max_vals_more = '''flag NOT present
option NOT present
positional present with value: too
flag2 NOT present
option2 maybe present with value of: Nothing
positional2 maybe present with value of: Nothing
option3 NOT present
positional3 NOT present
option NOT present
positional present with value: too
subcmd NOT present'''
_mult_vals_more = '''Argument --multvals was supplied more than once, but does not support multiple values
USAGE:
\tclaptests --multvals <one> <two>
For more information try --help'''
_mult_vals_few = '''Argument '--multvals <one> <two>' requires a value but none was supplied
USAGE:
\tclaptests --multvals <one> <two>
For more information try --help'''
_mult_vals_2m1 = '''The argument '--multvalsmo <one> <two>' requires 2 values, but 1 was provided
USAGE:
\tclaptests --multvalsmo <one> <two>
For more information try --help'''
_bin = './target/release/claptests'
cmds = {'help short: ': ['{} -h'.format(_bin), _help],
@ -152,6 +200,18 @@ cmds = {'help short: ': ['{} -h'.format(_bin), _help],
'excluded last: ': ['{} -F -f'.format(_bin), _excluded_l],
'missing required: ': ['{} -F'.format(_bin), _required],
'F2(ll),O(s),P: ': ['{} value --flag --flag -o some'.format(_bin), _f2op],
'max_vals too many: ': ['{} --maxvals3 some other value too'.format(_bin), _max_vals_more],
'max_vals exact: ': ['{} --maxvals3 some other value'.format(_bin), _exact],
'max_vals less: ': ['{} --maxvals3 some other'.format(_bin), _exact],
'min_vals more: ': ['{} --minvals2 some other value too'.format(_bin), _exact],
'min_vals exact: ': ['{} --minvals2 some value'.format(_bin), _exact],
'min_vals too few: ': ['{} --minvals2 some'.format(_bin), _min_vals_few],
'mult_vals too many: ': ['{} --multvals some other --multvals some other'.format(_bin), _mult_vals_more],
'mult_vals too few: ': ['{} --multvals some'.format(_bin), _mult_vals_few],
'mult_vals exact: ': ['{} --multvals some other'.format(_bin), _exact],
'mult_valsmo x2: ': ['{} --multvalsmo some other --multvalsmo some other'.format(_bin), _exact],
'mult_valsmo x2-1: ': ['{} --multvalsmo some other --multvalsmo some'.format(_bin), _mult_vals_2m1],
'mult_valsmo x1: ': ['{} --multvalsmo some other'.format(_bin), _exact],
'F2(ss),O(s),P: ': ['{} value -f -f -o some'.format(_bin), _f2op],
'O2(ll)P: ': ['{} value --option some --option other'.format(_bin), _o2p],
'O2(l=l=)P: ': ['{} value --option=some --option=other'.format(_bin), _o2p],

View file

@ -5,6 +5,7 @@ use clap::{App, Arg, SubCommand};
fn main() {
let m_val_names = ["one", "two"];
let args = "-f --flag... 'tests flags'
-o --option=[opt]... 'tests options'
[positional] 'tests positionals'";
@ -21,7 +22,11 @@ fn main() {
Arg::from_usage("--long-option-2 [option2] 'tests long options with exclusions'").mutually_excludes("option").requires("positional2"),
Arg::from_usage("[positional2] 'tests positionals with exclusions'"),
Arg::from_usage("-O [option3] 'tests options with specific value sets'").possible_values(&opt3_vals),
Arg::from_usage("[positional3]... 'tests positionals with specific values'").possible_values(&pos3_vals)
Arg::from_usage("[positional3]... 'tests positionals with specific values'").possible_values(&pos3_vals),
Arg::from_usage("--multvals [multvals] 'Tests mutliple values, not mult occs'").value_names(&m_val_names),
Arg::from_usage("--multvalsmo [multvalsmo]... 'Tests mutliple values, not mult occs'").value_names(&m_val_names),
Arg::from_usage("--minvals2 [minvals]... 'Tests 2 min vals'").min_values(2),
Arg::from_usage("--maxvals3 [maxvals]... 'Tests 3 max vals'").max_values(3)
])
.subcommand(SubCommand::new("subcmd")
.about("tests subcommands")

View file

@ -315,7 +315,11 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
max_vals: a.max_vals,
help: a.help,
};
if pb.num_vals.unwrap_or(0) > 1 && !pb.multiple {
if pb.min_vals.is_some() && !pb.multiple {
panic!("Argument \"{}\" does not allow multiple values, yet it is expecting {} \
values", pb.name, pb.num_vals.unwrap());
}
if pb.max_vals.is_some() && !pb.multiple {
panic!("Argument \"{}\" does not allow multiple values, yet it is expecting {} \
values", pb.name, pb.num_vals.unwrap());
}
@ -332,7 +336,7 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
let mut rhs = HashSet::new();
// without derefing n = &&str
for n in r {
rhs.insert(*n);
rhs.insert(*n);
if pb.required {
self.required.insert(*n);
}
@ -371,7 +375,11 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
if let Some(ref vec) = ob.val_names {
ob.num_vals = Some(vec.len() as u8);
}
if ob.num_vals.unwrap_or(0) > 1 && !ob.multiple {
if ob.min_vals.is_some() && !ob.multiple {
panic!("Argument \"{}\" does not allow multiple values, yet it is expecting {} \
values", ob.name, ob.num_vals.unwrap());
}
if ob.max_vals.is_some() && !ob.multiple {
panic!("Argument \"{}\" does not allow multiple values, yet it is expecting {} \
values", ob.name, ob.num_vals.unwrap());
}
@ -387,8 +395,8 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
if let Some(ref r) = a.requires {
let mut rhs = HashSet::new();
// without derefing n = &&str
for n in r {
rhs.insert(*n);
for n in r {
rhs.insert(*n);
if ob.required {
self.required.insert(*n);
}
@ -683,8 +691,8 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
}
if g_vec.is_empty() {
return args.iter().map(|s| s.to_owned()).collect::<Vec<_>>()
}
return args.iter().map(|s| s.to_owned()).collect::<Vec<_>>()
}
return g_vec.iter().map(|g| self.get_group_members(g)).fold(vec![], |acc, v| acc + &v)
}
@ -707,8 +715,8 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
}
if g_vec.is_empty() {
return args.iter().map(|s| *s).collect::<Vec<_>>()
}
return args.iter().map(|s| *s).collect::<Vec<_>>()
}
return g_vec.iter()
.map(|g| self.get_group_members_names(g))
.fold(vec![], |acc, v| acc + &v)
@ -841,7 +849,7 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
let mut hs = self.required.iter().map(|n| *n).collect::<HashSet<_>>();
tmp_vec.iter().map(|n| hs.insert(*n)).collect::<Vec<_>>();
let reqs = self.get_required_from(hs);
let r_string = reqs.iter().fold(String::new(), |acc, s| acc + &format!(" {}", s)[..]);
usage.push_str(&format!("{}{}",
@ -933,7 +941,9 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
.filter(|ref o| o.short.is_some())
// 3='...'
// 4='- <>'
.map(|ref a| if a.multiple { 3 } else { 0 } + a.name.len() + 4) {
.map(|ref a| format!("{}",a).len() + if a.short.is_some() &&
a.long.is_some() { 4 }
else { 0 }) {
if ol > longest_opt {longest_opt = ol;}
}
}
@ -987,30 +997,34 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
println!("OPTIONS:");
for v in self.opts.values() {
// if it supports multiple we add '...' i.e. 3 to the name length
let mult = if v.multiple { 3 } else { 0 };
println!("{}{}{}{}{}{}",tab,
if let Some(s) = v.short{format!("-{}",s)}else{tab.to_owned()},
if let Some(l) = v.long {
format!("{}--{} ",
if v.short.is_some() {", "} else {""},l)
} else {
" ".to_owned()
"".to_owned()
},
format!("{}",
if let Some(ref vec) = v.val_names {
vec.iter().fold(String::new(), |acc, s| {
acc + &format!("<{}> ", s)[..]
acc + &format!(" <{}>", s)[..]
})
} else if let Some(num) = v.num_vals {
(0..num).fold(String::new(), |acc, _| {
acc + &format!(" <{}>", v.name)[..]
})
} else {
format!("<{}>{}", v.name, if v.multiple{"..."} else {""})
format!(" <{}>{}", v.name, if v.multiple{"..."} else {""})
}),
if v.long.is_some() {
self.get_spaces(
(longest_opt) - (v.long.unwrap().len() + v.name.len() + mult + 1)
)
} else {
self.get_spaces((longest_opt + 3) - (v.name.len() + mult))
},
if v.long.is_some() {
self.get_spaces(
(longest_opt + 4) - (format!("{}",v).len())
)
} else {
// 8 = tab + '-a, '.len()
self.get_spaces((longest_opt + 9) - (format!("{}", v).len()))
},
get_help!(v) );
}
}
@ -1203,11 +1217,11 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
if let Some(num) = opt.num_vals {
if let Some(ref ma) = matches.args.get(opt.name) {
if let Some(ref vals) = ma.values {
if num == vals.len() as u8 {
if num == vals.len() as u8 && !opt.multiple {
self.report_error(format!("The argument \"{}\" was found, \
but '{}' only expects {} values",
arg,
opt,
but '{}' only expects {} values",
arg,
opt,
vals.len()),
true,
true,
@ -1401,10 +1415,10 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
match needs_val_of {
Some(ref a) => {
if let Some(o) = self.opts.get(a) {
if o.multiple && self.required.is_empty() {
if o.multiple && self.required.is_empty() {
let should_err = match matches.values_of(o.name) {
Some(ref v) => if v.len() == 0 { true } else { false },
None => true,
None => true,
};
if should_err {
self.report_error(
@ -1423,14 +1437,14 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
}
else {
self.report_error(format!("The following required arguments were not \
supplied:\n{}",
supplied:\n{}",
self.get_required_from(self.required.iter()
.map(|s| *s)
.collect::<HashSet<_>>())
.iter()
.fold(String::new(), |acc, s| acc + &format!("\t'{}'\n",s)[..])),
true,
true,
true,
true,
Some(matches.args.keys().map(|k| *k).collect::<Vec<_>>()));
}
} else {
@ -1452,14 +1466,14 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
if !self.required.is_empty() {
if self.validate_required(&matches) {
self.report_error(format!("The following required arguments were not \
supplied:\n{}",
supplied:\n{}",
self.get_required_from(self.required.iter()
.map(|s| *s)
.collect::<HashSet<_>>())
.iter()
.fold(String::new(), |acc, s| acc + &format!("\t'{}'\n",s)[..])),
true,
true,
true,
true,
Some(matches.args.keys().map(|k| *k).collect::<Vec<_>>()));
}
}
@ -1741,7 +1755,7 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
}
// Shouldn't reach here
self.report_error(format!("Argument --{} isn't valid", arg),
self.report_error(format!("The argument --{} isn't valid", arg),
true,
true,
Some(matches.args.keys().map(|k| *k).collect::<Vec<_>>()));
@ -1757,7 +1771,7 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
for c in arg.chars() {
self.check_for_help_and_version(c);
if !self.parse_single_short_flag(matches, c) {
self.report_error(format!("Argument -{} isn't valid",arg),
self.report_error(format!("The argument -{} isn't valid",arg),
true,
true,
Some(matches.args.keys().map(|k| *k).collect::<Vec<_>>()));
@ -1795,8 +1809,8 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
if matches.args.contains_key(v.name) {
if !v.multiple {
self.report_error(format!("Argument -{} was supplied more than once, but does \
not support multiple values", arg),
self.report_error(format!("The argument -{} was supplied more than once, but \
does not support multiple values", arg),
true,
true,
Some(matches.args.keys().map(|k| *k).collect::<Vec<_>>()));
@ -1834,7 +1848,7 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
}
// Didn't match a flag or option, must be invalid
self.report_error( format!("Argument -{} isn't valid",arg_c),
self.report_error( format!("The argument -{} isn't valid",arg_c),
true,
true,
Some(matches.args.keys().map(|k| *k).collect::<Vec<_>>()));
@ -1863,7 +1877,7 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
// Make sure this isn't one being added multiple times if it doesn't suppor it
if matches.args.contains_key(v.name) && !v.multiple {
self.report_error(format!("Argument -{} was supplied more than once, but does \
self.report_error(format!("The argument -{} was supplied more than once, but does \
not support multiple values", arg),
true,
true,
@ -1961,13 +1975,24 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
if let Some(ref vals) = ma.values {
if let Some(f) = self.opts.get(name) {
if let Some(num) = f.num_vals {
if num != vals.len() as u8 {
let should_err = if f.multiple {
((vals.len() as u8) % num) != 0
} else {
num != (vals.len() as u8)
};
if should_err {
self.report_error(format!("The argument '{}' requires {} values, \
but {} w{} provided",
f,
num,
vals.len(),
if vals.len() == 1 {"as"}else{"ere"}),
if f.multiple {
vals.len() % num as usize
} else {
vals.len()
},
if vals.len() == 1 ||
( f.multiple &&
( vals.len() % num as usize) == 1) {"as"}else{"ere"}),
true,
true,
Some(matches.args.keys().map(|k| *k).collect::<Vec<_>>()));

View file

@ -654,9 +654,11 @@ impl<'n, 'l, 'h, 'g, 'p, 'r> Arg<'n, 'l, 'h, 'g, 'p, 'r> {
/// `.number_of_values(3)`, and this argument wouldn't be satisfied unless the user provided
/// 3 and only 3 values.
///
/// **NOTE:** The argument *must* have `.multiple(true)` or `...` to use this setting. Which
/// implies that `qty` must be > 1 (i.e. setting `.number_of_values(1)` would be unnecessary)
/// **NOTE:** `qty` must be > 1
///
/// **NOTE:** Does *not* require `.multiple(true)` to be set. Setting `.multiple(true)` would
/// allow `-f <file> <file> <file> -f <file> <file> <file>` where as *not* setting
/// `.multiple(true)` would only allow one occurrence of this argument.
///
/// # Example
///
@ -668,6 +670,11 @@ impl<'n, 'l, 'h, 'g, 'p, 'r> Arg<'n, 'l, 'h, 'g, 'p, 'r> {
/// .number_of_values(3)
/// # ).get_matches();
pub fn number_of_values(mut self, qty: u8) -> Arg<'n, 'l, 'h, 'g, 'p, 'r> {
if qty < 2 {
panic!("Arguments with number_of_values(qty) qty must be > 1. Prefer \
takes_value(true) for arguments with onyl one value, or flags for arguments \
with 0 values.");
}
self.num_vals = Some(qty);
self
}
@ -677,8 +684,9 @@ impl<'n, 'l, 'h, 'g, 'p, 'r> Arg<'n, 'l, 'h, 'g, 'p, 'r> {
/// `.max_values(3)`, and this argument would be satisfied if the user provided, 1, 2, or 3
/// values.
///
/// **NOTE:** The argument *must* have `.multiple(true)` or `...` to use this setting. Which
/// implies that `qty` must be > 1 (i.e. setting `.max_values(1)` would be unnecessary)
/// **NOTE:** `qty` must be > 1
///
/// **NOTE:** This implicity sets `.multiple(true)`
///
/// # Example
///
@ -690,7 +698,13 @@ impl<'n, 'l, 'h, 'g, 'p, 'r> Arg<'n, 'l, 'h, 'g, 'p, 'r> {
/// .max_values(3)
/// # ).get_matches();
pub fn max_values(mut self, qty: u8) -> Arg<'n, 'l, 'h, 'g, 'p, 'r> {
if qty < 2 {
panic!("Arguments with max_values(qty) qty must be > 1. Prefer \
takes_value(true) for arguments with onyl one value, or flags for arguments \
with 0 values.");
}
self.max_vals = Some(qty);
self.multiple = true;
self
}
@ -699,7 +713,9 @@ impl<'n, 'l, 'h, 'g, 'p, 'r> Arg<'n, 'l, 'h, 'g, 'p, 'r> {
/// `.min_values(2)`, and this argument would be satisfied if the user provided, 2 or more
/// values.
///
/// **NOTE:** The argument *must* have `.multiple(true)` or `...` to use this setting.
/// **NOTE:** This implicity sets `.multiple(true)`
///
/// **NOTE:** `qty` must be > 0
///
/// **NOTE:** `qty` *must* be > 0. If you wish to have an argument with 0 or more values prefer
/// two seperate arguments (a flag, and an option with multiple values).
@ -714,7 +730,12 @@ impl<'n, 'l, 'h, 'g, 'p, 'r> Arg<'n, 'l, 'h, 'g, 'p, 'r> {
/// .min_values(2)
/// # ).get_matches();
pub fn min_values(mut self, qty: u8) -> Arg<'n, 'l, 'h, 'g, 'p, 'r> {
if qty < 1 {
panic!("Arguments with min_values(qty) qty must be > 0. Prefer flags for arguments \
with 0 values.");
}
self.min_vals = Some(qty);
self.multiple = true;
self
}
@ -727,6 +748,10 @@ impl<'n, 'l, 'h, 'g, 'p, 'r> Arg<'n, 'l, 'h, 'g, 'p, 'r> {
/// be aware that the number of "names" you set for the values, will be the *exact* number of
/// values required to satisfy this argument
///
/// **NOTE:** Does *not* require `.multiple(true)` to be set. Setting `.multiple(true)` would
/// allow `-f <file> <file> <file> -f <file> <file> <file>` where as *not* setting
/// `.multiple(true)` would only allow one occurrence of this argument.
///
/// # Example
///
/// ```no_run

View file

@ -42,6 +42,14 @@ impl<'n> Display for OptBuilder<'n> {
format!("-{}", self.short.unwrap())
},
vec.iter().fold(String::new(),|acc, i| acc + &format!(" <{}>",i)[..]) )
} else if let Some(num) = self.num_vals {
format!("{}{}",
if self.long.is_some() {
format!("--{}", self.long.unwrap())
} else {
format!("-{}", self.short.unwrap())
},
(0..num).fold(String::new(), |acc, _| acc + &format!(" <{}>", self.name)[..]) )
} else {
format!("{} <{}>{}",
if self.long.is_some() {