Revert "Auto merge of #737 - kbknapp:questionmark, r=kbknapp"

This reverts commit f24a3760e4, reversing
changes made to 087cee7404.
This commit is contained in:
Kevin K 2016-11-12 12:12:05 -05:00
parent f24a3760e4
commit 7cb44abc09
No known key found for this signature in database
GPG key ID: 17218E4B3692F01A
20 changed files with 352 additions and 371 deletions

View file

@ -2,7 +2,7 @@ sudo: true
language: rust
rust:
- nightly
- nightly-2016-11-06
- nightly-2016-10-21
- beta
- stable
matrix:

View file

@ -1,7 +1,7 @@
[package]
name = "clap"
version = "2.19.0"
version = "2.18.0"
authors = ["Kevin K. <kbknapp@gmail.com>"]
exclude = ["examples/*", "clap-test/*", "tests/*", "benches/*", "*.png", "clap-perf/*", "*.dot"]
repository = "https://github.com/kbknapp/clap-rs.git"
@ -24,7 +24,7 @@ ansi_term = { version = "~0.9.0", optional = true }
term_size = { version = "~0.2.0", optional = true }
libc = { version = "~0.2.9", optional = true }
yaml-rust = { version = "~0.3.2", optional = true }
clippy = { version = "~0.0.98", optional = true }
clippy = { version = "~0.0.96", optional = true }
[dev-dependencies]
regex = "~0.1.69"

View file

@ -7,13 +7,3 @@ update-contributors:
@echo "" >> CONTRIBUTORS.md
@githubcontrib --owner kbknapp --repo clap-rs --sha master --cols 6 --format md --showlogin true --sortBy login >> CONTRIBUTORS.md
@rm CONTRIBUTORS.md.bak
run-test TEST:
cargo test --test {{TEST}}
run-tests:
cargo test --features "yaml unstable"
lint:
rustup override add nightly
cargo build --features lints && rustup override remove

View file

@ -106,13 +106,10 @@ impl<'a> Help<'a> {
hide_pv: hide_pv,
term_w: match term_w {
Some(width) => if width == 0 { usize::MAX } else { width },
None => {
cmp::min(term_size::dimensions().map_or(120, |(w, _)| w),
match max_w {
None => cmp::min(term_size::dimensions().map_or(120, |(w, _)| w), match max_w {
None | Some(0) => usize::MAX,
Some(mw) => mw,
})
}
}),
},
color: color,
cizer: cizer,
@ -153,25 +150,18 @@ impl<'a> Help<'a> {
use_stderr: stderr,
when: parser.color(),
};
Self::new(w,
nlh,
hide_v,
color,
cizer,
parser.meta.term_w,
parser.meta.max_w)
.write_help(parser)
Self::new(w, nlh, hide_v, color, cizer, parser.meta.term_w, parser.meta.max_w).write_help(parser)
}
/// Writes the parser help to the wrapped stream.
pub fn write_help(&mut self, parser: &Parser) -> ClapResult<()> {
debugln!("fn=Help::write_help;");
if let Some(h) = parser.meta.help_str {
write!(self.writer, "{}", h).map_err(Error::from)?;
try!(write!(self.writer, "{}", h).map_err(Error::from));
} else if let Some(tmpl) = parser.meta.template {
self.write_templated_help(parser, tmpl)?;
try!(self.write_templated_help(&parser, tmpl));
} else {
self.write_default_help(parser)?;
try!(self.write_default_help(&parser));
}
Ok(())
}
@ -201,9 +191,9 @@ impl<'a> Help<'a> {
if first {
first = false;
} else {
self.writer.write(b"\n")?;
try!(self.writer.write(b"\n"));
}
self.write_arg(arg.as_base())?;
try!(self.write_arg(arg.as_base()));
}
Ok(())
}
@ -237,28 +227,30 @@ impl<'a> Help<'a> {
if first {
first = false;
} else {
self.writer.write(b"\n")?;
try!(self.writer.write(b"\n"));
}
self.write_arg(arg.as_base())?;
try!(self.write_arg(arg.as_base()));
}
}
Ok(())
}
/// Writes help for an argument to the wrapped stream.
fn write_arg<'b, 'c>(&mut self, arg: &ArgWithDisplay<'b, 'c>) -> io::Result<()> {
fn write_arg<'b, 'c>(&mut self,
arg: &ArgWithDisplay<'b, 'c>)
-> io::Result<()> {
debugln!("fn=write_arg;");
self.short(arg)?;
self.long(arg)?;
let spec_vals = self.val(arg)?;
self.help(arg, &*spec_vals)?;
try!(self.short(arg));
try!(self.long(arg));
let spec_vals = try!(self.val(arg));
try!(self.help(arg, &*spec_vals));
Ok(())
}
/// Writes argument's short command to the wrapped stream.
fn short<'b, 'c>(&mut self, arg: &ArgWithDisplay<'b, 'c>) -> io::Result<()> {
debugln!("fn=short;");
write!(self.writer, "{}", TAB)?;
try!(write!(self.writer, "{}", TAB));
if let Some(s) = arg.short() {
color!(self, "-{}", s, good)
} else if arg.has_switch() {
@ -277,16 +269,16 @@ impl<'a> Help<'a> {
if arg.takes_value() {
if let Some(l) = arg.long() {
if arg.short().is_some() {
write!(self.writer, ", ")?;
try!(write!(self.writer, ", "));
}
color!(self, "--{}", l, good)?
try!(color!(self, "--{}", l, good))
}
write!(self.writer, " ")?;
try!(write!(self.writer, " "));
} else if let Some(l) = arg.long() {
if arg.short().is_some() {
write!(self.writer, ", ")?;
try!(write!(self.writer, ", "));
}
color!(self, "--{}", l, good)?;
try!(color!(self, "--{}", l, good));
}
Ok(())
}
@ -298,33 +290,33 @@ impl<'a> Help<'a> {
if let Some(vec) = arg.val_names() {
let mut it = vec.iter().peekable();
while let Some((_, val)) = it.next() {
color!(self, "<{}>", val, good)?;
try!(color!(self, "<{}>", val, good));
if it.peek().is_some() {
write!(self.writer, " ")?;
try!(write!(self.writer, " "));
}
}
let num = vec.len();
if arg.is_set(ArgSettings::Multiple) && num == 1 {
color!(self, "...", good)?;
try!(color!(self, "...", good));
}
} else if let Some(num) = arg.num_vals() {
let mut it = (0..num).peekable();
while let Some(_) = it.next() {
color!(self, "<{}>", arg.name(), good)?;
try!(color!(self, "<{}>", arg.name(), good));
if it.peek().is_some() {
write!(self.writer, " ")?;
try!(write!(self.writer, " "));
}
}
if arg.is_set(ArgSettings::Multiple) && num == 1 {
color!(self, "...", good)?;
try!(color!(self, "...", good));
}
} else if arg.has_switch() {
color!(self, "<{}>", arg.name(), good)?;
try!(color!(self, "<{}>", arg.name(), good));
if arg.is_set(ArgSettings::Multiple) {
color!(self, "...", good)?;
try!(color!(self, "...", good));
}
} else {
color!(self, "{}", arg, good)?;
try!(color!(self, "{}", arg, good));
}
}
@ -343,10 +335,7 @@ impl<'a> Help<'a> {
debugln!("force_next_line...{:?}", self.force_next_line);
debugln!("nlh...{:?}", nlh);
debugln!("taken...{}", taken);
debugln!("help_width > (width - taken)...{} > ({} - {})",
h_w,
self.term_w,
taken);
debugln!("help_width > (width - taken)...{} > ({} - {})", h_w, self.term_w, taken);
debugln!("longest...{}", self.longest);
debug!("next_line...");
if !(nlh || self.force_next_line) {
@ -414,13 +403,13 @@ impl<'a> Help<'a> {
};
if help.contains('\n') {
if let Some(part) = help.lines().next() {
write!(self.writer, "{}", part)?;
try!(write!(self.writer, "{}", part));
}
for part in help.lines().skip(1) {
write!(self.writer, "\n{}", part)?;
try!(write!(self.writer, "\n{}", part));
}
} else {
write!(self.writer, "{}", help)?;
try!(write!(self.writer, "{}", help));
}
Ok(())
}
@ -443,7 +432,7 @@ impl<'a> Help<'a> {
// Is help on next line, if so then indent
if nlh || self.force_next_line {
write!(self.writer, "\n{}{}{}", TAB, TAB, TAB)?;
try!(write!(self.writer, "\n{}{}{}", TAB, TAB, TAB));
}
debug!("Too long...");
@ -481,21 +470,21 @@ impl<'a> Help<'a> {
};
if help.contains('\n') {
if let Some(part) = help.lines().next() {
write!(self.writer, "{}", part)?;
try!(write!(self.writer, "{}", part));
}
for part in help.lines().skip(1) {
write!(self.writer, "\n")?;
try!(write!(self.writer, "\n"));
if nlh || self.force_next_line {
write!(self.writer, "{}{}{}", TAB, TAB, TAB)?;
try!(write!(self.writer, "{}{}{}", TAB, TAB, TAB));
} else if arg.has_switch() {
write_nspaces!(self.writer, self.longest + 12);
} else {
write_nspaces!(self.writer, self.longest + 8);
}
write!(self.writer, "{}", part)?;
try!(write!(self.writer, "{}", part));
}
} else {
write!(self.writer, "{}", help)?;
try!(write!(self.writer, "{}", help));
}
Ok(())
}
@ -563,40 +552,41 @@ impl<'a> Help<'a> {
let opts_flags = parser.flags()
.map(as_arg_trait)
.chain(parser.opts().map(as_arg_trait));
color!(self, "OPTIONS:\n", warning)?;
self.write_args(opts_flags)?;
try!(color!(self, "OPTIONS:\n", warning));
try!(self.write_args(opts_flags));
first = false;
} else {
if flags {
color!(self, "FLAGS:\n", warning)?;
self.write_args(parser.flags().map(as_arg_trait))?;
try!(color!(self, "FLAGS:\n", warning));
try!(self.write_args(parser.flags()
.map(as_arg_trait)));
first = false;
}
if opts {
if !first {
self.writer.write(b"\n\n")?;
try!(self.writer.write(b"\n\n"));
}
color!(self, "OPTIONS:\n", warning)?;
self.write_args(parser.opts().map(as_arg_trait))?;
try!(color!(self, "OPTIONS:\n", warning));
try!(self.write_args(parser.opts().map(as_arg_trait)));
first = false;
}
}
if pos {
if !first {
self.writer.write(b"\n\n")?;
try!(self.writer.write(b"\n\n"));
}
color!(self, "ARGS:\n", warning)?;
self.write_args_unsorted(parser.positionals().map(as_arg_trait))?;
try!(color!(self, "ARGS:\n", warning));
try!(self.write_args_unsorted(parser.positionals().map(as_arg_trait)));
first = false;
}
if subcmds {
if !first {
self.writer.write(b"\n\n")?;
try!(self.writer.write(b"\n\n"));
}
color!(self, "SUBCOMMANDS:\n", warning)?;
self.write_subcommands(parser)?;
try!(color!(self, "SUBCOMMANDS:\n", warning));
try!(self.write_subcommands(&parser));
}
Ok(())
@ -620,9 +610,9 @@ impl<'a> Help<'a> {
if first {
first = false;
} else {
self.writer.write(b"\n")?;
try!(self.writer.write(b"\n"));
}
self.write_arg(sc)?;
try!(self.write_arg(sc));
}
}
Ok(())
@ -630,7 +620,7 @@ impl<'a> Help<'a> {
/// Writes version of a Parser Object to the wrapped stream.
fn write_version(&mut self, parser: &Parser) -> io::Result<()> {
write!(self.writer, "{}", parser.meta.version.unwrap_or("".into()))?;
try!(write!(self.writer, "{}", parser.meta.version.unwrap_or("".into())));
Ok(())
}
@ -639,12 +629,12 @@ impl<'a> Help<'a> {
if let Some(bn) = parser.meta.bin_name.as_ref() {
if bn.contains(' ') {
// Incase we're dealing with subcommands i.e. git mv is translated to git-mv
color!(self, bn.replace(" ", "-"), good)?
try!(color!(self, bn.replace(" ", "-"), good))
} else {
color!(self, &parser.meta.name[..], good)?
try!(color!(self, &parser.meta.name[..], good))
}
} else {
color!(self, &parser.meta.name[..], good)?
try!(color!(self, &parser.meta.name[..], good))
}
Ok(())
}
@ -653,27 +643,27 @@ impl<'a> Help<'a> {
pub fn write_default_help(&mut self, parser: &Parser) -> ClapResult<()> {
debugln!("fn=write_default_help;");
if let Some(h) = parser.meta.pre_help {
self.write_before_after_help(h)?;
self.writer.write(b"\n\n")?;
try!(self.write_before_after_help(h));
try!(self.writer.write(b"\n\n"));
}
// Print the version
self.write_bin_name(parser)?;
self.writer.write(b" ")?;
self.write_version(parser)?;
self.writer.write(b"\n")?;
try!(self.write_bin_name(&parser));
try!(self.writer.write(b" "));
try!(self.write_version(&parser));
try!(self.writer.write(b"\n"));
if let Some(author) = parser.meta.author {
write!(self.writer, "{}\n", author)?;
try!(write!(self.writer, "{}\n", author));
}
if let Some(about) = parser.meta.about {
write!(self.writer, "{}\n", about)?;
try!(write!(self.writer, "{}\n", about));
}
color!(self, "\nUSAGE:", warning)?;
write!(self.writer,
try!(color!(self, "\nUSAGE:", warning));
try!(write!(self.writer,
"\n{}{}\n\n",
TAB,
parser.create_usage_no_title(&[]))?;
parser.create_usage_no_title(&[])));
let flags = parser.has_flags();
let pos = parser.has_positionals();
@ -681,14 +671,14 @@ impl<'a> Help<'a> {
let subcmds = parser.has_subcommands();
if flags || opts || pos || subcmds {
self.write_all_args(parser)?;
try!(self.write_all_args(&parser));
}
if let Some(h) = parser.meta.more_help {
if flags || opts || pos || subcmds {
self.writer.write(b"\n\n")?;
try!(self.writer.write(b"\n\n"));
}
self.write_before_after_help(h)?;
try!(self.write_before_after_help(h));
}
self.writer.flush().map_err(Error::from)
@ -847,65 +837,68 @@ impl<'a> Help<'a> {
});
match &tag_buf.get_ref()[0..tag_length] {
b"?" => {
self.writer.write(b"Could not decode tag name")?;
try!(self.writer.write(b"Could not decode tag name"));
}
b"bin" => {
self.write_bin_name(parser)?;
try!(self.write_bin_name(&parser));
}
b"version" => {
write!(self.writer,
try!(write!(self.writer,
"{}",
parser.meta.version.unwrap_or("unknown version"))?;
parser.meta.version.unwrap_or("unknown version")));
}
b"author" => {
write!(self.writer,
try!(write!(self.writer,
"{}",
parser.meta.author.unwrap_or("unknown author"))?;
parser.meta.author.unwrap_or("unknown author")));
}
b"about" => {
write!(self.writer,
try!(write!(self.writer,
"{}",
parser.meta.about.unwrap_or("unknown about"))?;
parser.meta.about.unwrap_or("unknown about")));
}
b"usage" => {
write!(self.writer, "{}", parser.create_usage_no_title(&[]))?;
try!(write!(self.writer, "{}", parser.create_usage_no_title(&[])));
}
b"all-args" => {
self.write_all_args(parser)?;
try!(self.write_all_args(&parser));
}
b"unified" => {
let opts_flags = parser.flags()
.map(as_arg_trait)
.chain(parser.opts().map(as_arg_trait));
self.write_args(opts_flags)?;
try!(self.write_args(opts_flags));
}
b"flags" => {
self.write_args(parser.flags().map(as_arg_trait))?;
try!(self.write_args(parser.flags()
.map(as_arg_trait)));
}
b"options" => {
self.write_args(parser.opts().map(as_arg_trait))?;
try!(self.write_args(parser.opts()
.map(as_arg_trait)));
}
b"positionals" => {
self.write_args(parser.positionals().map(as_arg_trait))?;
try!(self.write_args(parser.positionals()
.map(as_arg_trait)));
}
b"subcommands" => {
self.write_subcommands(parser)?;
try!(self.write_subcommands(&parser));
}
b"after-help" => {
write!(self.writer,
try!(write!(self.writer,
"{}",
parser.meta.more_help.unwrap_or("unknown after-help"))?;
parser.meta.more_help.unwrap_or("unknown after-help")));
}
b"before-help" => {
write!(self.writer,
try!(write!(self.writer,
"{}",
parser.meta.pre_help.unwrap_or("unknown before-help"))?;
parser.meta.pre_help.unwrap_or("unknown before-help")));
}
// Unknown tag, write it back.
r => {
self.writer.write(b"{")?;
self.writer.write(r)?;
self.writer.write(b"}")?;
try!(self.writer.write(b"{"));
try!(self.writer.write(r));
try!(self.writer.write(b"}"));
}
}
}

View file

@ -1344,7 +1344,7 @@ impl<'a, 'b> App<'a, 'b> {
let p = Path::new(&*bn_os);
if let Some(f) = p.file_name() {
if let Some(s) = f.to_os_string().to_str() {
if self.p.meta.bin_name.is_none() {
if let None = self.p.meta.bin_name {
self.p.meta.bin_name = Some(s.to_owned());
}
}

View file

@ -507,7 +507,6 @@ impl<'a, 'b> Parser<'a, 'b>
self.settings.unset(s)
}
#[cfg_attr(feature = "lints", allow(block_in_if_condition_stmt))]
pub fn verify_positionals(&mut self) {
// Because you must wait until all arguments have been supplied, this is the first chance
// to make assertions on positional argument indexes
@ -740,7 +739,7 @@ impl<'a, 'b> Parser<'a, 'b>
if let Some(arg) = needs_val_of {
// get the OptBuilder so we can check the settings
if let Some(opt) = self.get_opt(arg) {
needs_val_of = self.add_val_to_arg(&*opt, &arg_os, matcher)?;
needs_val_of = try!(self.add_val_to_arg(&*opt, &arg_os, matcher));
// get the next value from the iterator
continue;
}
@ -754,7 +753,7 @@ impl<'a, 'b> Parser<'a, 'b>
continue;
}
needs_val_of = self.parse_long_arg(matcher, &arg_os)?;
needs_val_of = try!(self.parse_long_arg(matcher, &arg_os));
if !(needs_val_of.is_none() && self.is_set(AppSettings::AllowLeadingHyphen)) {
continue;
}
@ -762,7 +761,7 @@ impl<'a, 'b> Parser<'a, 'b>
// Try to parse short args like normal, if AllowLeadingHyphen or
// AllowNegativeNumbers is set, parse_short_arg will *not* throw
// an error, and instead return Ok(None)
needs_val_of = self.parse_short_arg(matcher, &arg_os)?;
needs_val_of = try!(self.parse_short_arg(matcher, &arg_os));
// If it's None, we then check if one of those two AppSettings was set
debugln!("AllowLeadingHyphen set...{:?}", self.is_set(AppSettings::AllowLeadingHyphen));
debugln!("AllowNegativeNumbers set...{:?}", self.is_set(AppSettings::AllowNegativeNumbers));
@ -787,7 +786,7 @@ impl<'a, 'b> Parser<'a, 'b>
if pos_sc {
if &*arg_os == "help" && self.is_set(AppSettings::NeedsSubcommandHelp) {
self.parse_help_subcommand(it)?;
try!(self.parse_help_subcommand(it));
}
subcmd_name = Some(arg_os.to_str().expect(INVALID_UTF8).to_owned());
break;
@ -809,17 +808,14 @@ impl<'a, 'b> Parser<'a, 'b>
debugln!("Positional counter...{}", pos_counter);
debug!("Checking for low index multiples...");
if self.is_set(AppSettings::LowIndexMultiplePositional) &&
pos_counter == (self.positionals.len() - 1) {
if self.is_set(AppSettings::LowIndexMultiplePositional) && pos_counter == (self.positionals.len() - 1) {
sdebugln!("Found");
if let Some(na) = it.peek() {
let n = (*na).clone().into();
if is_new_arg(&n) || self.possible_subcommand(&n) ||
suggestions::did_you_mean(&n.to_string_lossy(),
if is_new_arg(&n) || self.possible_subcommand(&n) || suggestions::did_you_mean(&n.to_string_lossy(),
self.subcommands
.iter()
.map(|s| &s.p.meta.name))
.is_some() {
.map(|s| &s.p.meta.name)).is_some() {
debugln!("Bumping the positional counter...");
pos_counter += 1;
}
@ -849,10 +845,12 @@ impl<'a, 'b> Parser<'a, 'b>
let mut sc_m = ArgMatcher::new();
while let Some(v) = it.next() {
let a = v.into();
if a.to_str().is_none() && !self.settings.is_set(AppSettings::StrictUtf8) {
if let None = a.to_str() {
if !self.settings.is_set(AppSettings::StrictUtf8) {
return Err(Error::invalid_utf8(&*self.create_current_usage(matcher),
self.color()));
}
}
sc_m.add_val_to("", &a);
}
@ -885,7 +883,7 @@ impl<'a, 'b> Parser<'a, 'b>
.any(|&(n, _)| n == &*a))
}) {
sdebugln!("Yes");
self.validate_required(matcher)?;
try!(self.validate_required(matcher));
reqs_validated = true;
let should_err = if let Some(v) = matcher.0.args.get(&*o.name) {
v.vals.is_empty() && !(o.min_vals.is_some() && o.min_vals.unwrap() == 0)
@ -909,14 +907,14 @@ impl<'a, 'b> Parser<'a, 'b>
}
}
self.add_defaults(matcher)?;
self.validate_blacklist(matcher)?;
self.validate_num_args(matcher)?;
try!(self.add_defaults(matcher));
try!(self.validate_blacklist(matcher));
try!(self.validate_num_args(matcher));
matcher.usage(self.create_usage(&[]));
if !(self.settings.is_set(AppSettings::SubcommandsNegateReqs) && subcmd_name.is_some()) &&
!reqs_validated {
self.validate_required(matcher)?;
try!(self.validate_required(matcher));
}
if let Some(pos_sc_name) = subcmd_name {
// is this is a real subcommand, or an alias
@ -940,7 +938,7 @@ impl<'a, 'b> Parser<'a, 'b>
.next()
.expect(INTERNAL_ERROR_MSG)
};
self.parse_subcommand(sc_name, matcher, it)?;
try!(self.parse_subcommand(sc_name, matcher, it));
} else if self.is_set(AppSettings::SubcommandRequired) {
let bn = self.meta.bin_name.as_ref().unwrap_or(&self.meta.name);
return Err(Error::missing_subcommand(bn,
@ -948,7 +946,7 @@ impl<'a, 'b> Parser<'a, 'b>
self.color()));
} else if self.is_set(AppSettings::SubcommandRequiredElseHelp) {
let mut out = vec![];
self.write_help_err(&mut out)?;
try!(self.write_help_err(&mut out));
return Err(Error {
message: String::from_utf8_lossy(&*out).into_owned(),
kind: ErrorKind::MissingArgumentOrSubcommand,
@ -958,7 +956,7 @@ impl<'a, 'b> Parser<'a, 'b>
if matcher.is_empty() && matcher.subcommand_name().is_none() &&
self.is_set(AppSettings::ArgRequiredElseHelp) {
let mut out = vec![];
self.write_help_err(&mut out)?;
try!(self.write_help_err(&mut out));
return Err(Error {
message: String::from_utf8_lossy(&*out).into_owned(),
kind: ErrorKind::MissingArgumentOrSubcommand,
@ -1051,7 +1049,7 @@ impl<'a, 'b> Parser<'a, 'b>
""
},
&*sc.p.meta.name));
sc.p.get_matches_with(&mut sc_matcher, it)?;
try!(sc.p.get_matches_with(&mut sc_matcher, it));
matcher.subcommand(SubCommand {
name: sc.p.meta.name.clone(),
matches: sc_matcher.into(),
@ -1205,11 +1203,11 @@ impl<'a, 'b> Parser<'a, 'b>
arg.to_str().unwrap());
if arg == "help" && self.settings.is_set(AppSettings::NeedsLongHelp) {
sdebugln!("Help");
self._help()?;
try!(self._help());
}
if arg == "version" && self.settings.is_set(AppSettings::NeedsLongVersion) {
sdebugln!("Version");
self._version()?;
try!(self._version());
}
sdebugln!("Neither");
@ -1221,13 +1219,13 @@ impl<'a, 'b> Parser<'a, 'b>
if let Some(h) = self.help_short {
if arg == h && self.settings.is_set(AppSettings::NeedsLongHelp) {
sdebugln!("Help");
self._help()?;
try!(self._help());
}
}
if let Some(v) = self.version_short {
if arg == v && self.settings.is_set(AppSettings::NeedsLongVersion) {
sdebugln!("Version");
self._version()?;
try!(self._version());
}
}
sdebugln!("Neither");
@ -1236,7 +1234,10 @@ impl<'a, 'b> Parser<'a, 'b>
fn _help(&self) -> ClapResult<()> {
let mut buf = vec![];
Help::write_parser_help(&mut buf, self)?;
try!(Help::write_parser_help(&mut buf, self));
// let out = io::stdout();
// let mut out_buf = BufWriter::new(out.lock());
// try!(out_buf.write(&*buf));
Err(Error {
message: unsafe { String::from_utf8_unchecked(buf) },
kind: ErrorKind::HelpDisplayed,
@ -1247,7 +1248,7 @@ impl<'a, 'b> Parser<'a, 'b>
fn _version(&self) -> ClapResult<()> {
let out = io::stdout();
let mut buf_w = BufWriter::new(out.lock());
self.print_version(&mut buf_w)?;
try!(self.print_version(&mut buf_w));
Err(Error {
message: String::new(),
kind: ErrorKind::VersionDisplayed,
@ -1285,7 +1286,7 @@ impl<'a, 'b> Parser<'a, 'b>
.any(|&(n, _)| n == &*arg))
}) {
debugln!("Found valid opt '{}'", opt.to_string());
let ret = self.parse_opt(val, opt, matcher)?;
let ret = try!(self.parse_opt(val, opt, matcher));
arg_post_processing!(self, opt, matcher);
return Ok(ret);
@ -1303,9 +1304,9 @@ impl<'a, 'b> Parser<'a, 'b>
debugln!("Found valid flag '{}'", flag.to_string());
// Only flags could be help or version, and we need to check the raw long
// so this is the first point to check
self.check_for_help_and_version_str(arg)?;
try!(self.check_for_help_and_version_str(&arg));
self.parse_flag(flag, matcher)?;
try!(self.parse_flag(flag, matcher));
// Handle conflicts, requirements, etc.
arg_post_processing!(self, flag, matcher);
@ -1359,7 +1360,7 @@ impl<'a, 'b> Parser<'a, 'b>
};
// Default to "we're expecting a value later"
let ret = self.parse_opt(val, opt, matcher)?;
let ret = try!(self.parse_opt(val, opt, matcher));
arg_post_processing!(self, opt, matcher);
@ -1369,8 +1370,8 @@ impl<'a, 'b> Parser<'a, 'b>
.find(|&v| v.short.is_some() && v.short.unwrap() == c) {
debugln!("Found valid short flag -{}", c);
// Only flags can be help or version
self.check_for_help_and_version_char(c)?;
self.parse_flag(flag, matcher)?;
try!(self.check_for_help_and_version_char(c));
try!(self.parse_flag(flag, matcher));
// Handle conflicts, requirements, overrides, etc.
// Must be called here due to mutablilty
arg_post_processing!(self, flag, matcher);
@ -1409,7 +1410,7 @@ impl<'a, 'b> Parser<'a, 'b>
}
sdebugln!("Found - {:?}, len: {}", v, v.len_());
debugln!("{:?} contains '='...{:?}", fv, fv.starts_with(&[b'=']));
self.add_val_to_arg(opt, v, matcher)?;
try!(self.add_val_to_arg(opt, v, matcher));
} else {
sdebugln!("None");
}
@ -1439,10 +1440,10 @@ impl<'a, 'b> Parser<'a, 'b>
if !(self.trailing_vals && self.is_set(AppSettings::DontDelimitTrailingValues)) {
if let Some(delim) = arg.val_delim() {
if val.is_empty_() {
ret = self.add_single_val_to_arg(arg, val, matcher)?;
ret = try!(self.add_single_val_to_arg(arg, val, matcher));
} else {
for v in val.split(delim as u32 as u8) {
ret = self.add_single_val_to_arg(arg, v, matcher)?;
ret = try!(self.add_single_val_to_arg(arg, v, matcher));
}
// If there was a delimiter used, we're not looking for more values
if val.contains_byte(delim as u32 as u8) ||
@ -1451,10 +1452,10 @@ impl<'a, 'b> Parser<'a, 'b>
}
}
} else {
ret = self.add_single_val_to_arg(arg, val, matcher)?;
ret = try!(self.add_single_val_to_arg(arg, val, matcher));
}
} else {
ret = self.add_single_val_to_arg(arg, val, matcher)?;
ret = try!(self.add_single_val_to_arg(arg, val, matcher));
}
Ok(ret)
}
@ -1605,11 +1606,11 @@ impl<'a, 'b> Parser<'a, 'b>
} else if let Some(opt) = self.opts
.iter()
.find(|o| &o.name == name) {
self._validate_num_vals(opt, ma, matcher)?;
try!(self._validate_num_vals(opt, ma, matcher));
} else if let Some(pos) = self.positionals
.values()
.find(|p| &p.name == name) {
self._validate_num_vals(pos, ma, matcher)?;
try!(self._validate_num_vals(pos, ma, matcher));
}
}
Ok(())
@ -1894,7 +1895,7 @@ impl<'a, 'b> Parser<'a, 'b>
// Prints the version to the user and exits if quit=true
fn print_version<W: Write>(&self, w: &mut W) -> ClapResult<()> {
self.write_version(w)?;
try!(self.write_version(w));
w.flush().map_err(Error::from)
}
@ -1938,9 +1939,9 @@ impl<'a, 'b> Parser<'a, 'b>
macro_rules! add_val {
($_self:ident, $a:ident, $m:ident) => {
if $m.get($a.name).is_none() {
$_self.add_val_to_arg($a, OsStr::new($a.default_val
try!($_self.add_val_to_arg($a, OsStr::new($a.default_val
.as_ref()
.unwrap()), $m)?;
.unwrap()), $m));
arg_post_processing!($_self, $a, $m);
}
};

View file

@ -667,6 +667,7 @@ pub enum AppSettings {
#[doc(hidden)]
LowIndexMultiplePositional,
}
impl FromStr for AppSettings {

View file

@ -87,9 +87,9 @@ impl<'a, 'b, 'z> From<&'z Arg<'a, 'b>> for FlagBuilder<'a, 'b> {
impl<'n, 'e> Display for FlagBuilder<'n, 'e> {
fn fmt(&self, f: &mut Formatter) -> Result {
if let Some(l) = self.long {
write!(f, "--{}", l)?;
try!(write!(f, "--{}", l));
} else {
write!(f, "-{}", self.short.unwrap())?;
try!(write!(f, "-{}", self.short.unwrap()));
}
Ok(())
@ -180,7 +180,8 @@ impl<'n, 'e> AnyArg<'n, 'e> for FlagBuilder<'n, 'e> {
}
fn aliases(&self) -> Option<Vec<&'e str>> {
if let Some(ref aliases) = self.aliases {
let vis_aliases: Vec<_> = aliases.iter()
let vis_aliases: Vec<_> =
aliases.iter()
.filter_map(|&(n, v)| if v { Some(n) } else { None })
.collect();
if vis_aliases.is_empty() {
@ -232,8 +233,12 @@ mod test {
fn flagbuilder_display_multiple_aliases() {
let mut f = FlagBuilder::new("flg");
f.short = Some('f');
f.aliases =
Some(vec![("alias_not_visible", false), ("f2", true), ("f3", true), ("f4", true)]);
f.aliases = Some(vec![
("alias_not_visible", false),
("f2", true),
("f3", true),
("f4", true)
]);
assert_eq!(&*format!("{}", f), "-f");
}
}

View file

@ -125,41 +125,41 @@ impl<'n, 'e> Display for OptBuilder<'n, 'e> {
debugln!("fn=fmt");
// Write the name such --long or -l
if let Some(l) = self.long {
write!(f, "--{} ", l)?;
try!(write!(f, "--{} ", l));
} else {
write!(f, "-{} ", self.short.unwrap())?;
try!(write!(f, "-{} ", self.short.unwrap()));
}
// Write the values such as <name1> <name2>
if let Some(ref vec) = self.val_names {
let mut it = vec.iter().peekable();
while let Some((_, val)) = it.next() {
write!(f, "<{}>", val)?;
try!(write!(f, "<{}>", val));
if it.peek().is_some() {
write!(f, " ")?;
try!(write!(f, " "));
}
}
let num = vec.len();
if self.is_set(ArgSettings::Multiple) && num == 1 {
write!(f, "...")?;
try!(write!(f, "..."));
}
} else if let Some(num) = self.num_vals {
let mut it = (0..num).peekable();
while let Some(_) = it.next() {
write!(f, "<{}>", self.name)?;
try!(write!(f, "<{}>", self.name));
if it.peek().is_some() {
write!(f, " ")?;
try!(write!(f, " "));
}
}
} else {
write!(f,
try!(write!(f,
"<{}>{}",
self.name,
if self.is_set(ArgSettings::Multiple) {
"..."
} else {
""
})?;
}));
}
Ok(())
@ -259,7 +259,8 @@ impl<'n, 'e> AnyArg<'n, 'e> for OptBuilder<'n, 'e> {
}
fn aliases(&self) -> Option<Vec<&'e str>> {
if let Some(ref aliases) = self.aliases {
let vis_aliases: Vec<_> = aliases.iter()
let vis_aliases: Vec<_> =
aliases.iter()
.filter_map(|&(n, v)| if v { Some(n) } else { None })
.collect();
if vis_aliases.is_empty() {
@ -334,8 +335,12 @@ mod test {
fn optbuilder_display_multiple_aliases() {
let mut o = OptBuilder::new("opt");
o.long = Some("option");
o.aliases =
Some(vec![("als_not_visible", false), ("als2", true), ("als3", true), ("als4", true)]);
o.aliases = Some(vec![
("als_not_visible", false),
("als2", true),
("als3", true),
("als4", true)
]);
assert_eq!(&*format!("{}", o), "--option <opt>");
}
}

View file

@ -136,17 +136,17 @@ impl<'n, 'e> PosBuilder<'n, 'e> {
impl<'n, 'e> Display for PosBuilder<'n, 'e> {
fn fmt(&self, f: &mut Formatter) -> Result {
if let Some(ref names) = self.val_names {
write!(f,
try!(write!(f,
"{}",
names.values()
.map(|n| format!("<{}>", n))
.collect::<Vec<_>>()
.join(" "))?;
.join(" ")));
} else {
write!(f, "<{}>", self.name)?;
try!(write!(f, "<{}>", self.name));
}
if self.settings.is_set(ArgSettings::Multiple) && self.val_names.is_none() {
write!(f, "...")?;
try!(write!(f, "..."));
}
Ok(())

View file

@ -20,7 +20,8 @@ impl<'a, 'b> FishGen<'a, 'b> {
let command = self.p.meta.bin_name.as_ref().unwrap();
// function to detect subcommand
let detect_subcommand_function = r#"function __fish_using_command
let detect_subcommand_function =
r#"function __fish_using_command
set cmd (commandline -opc)
if [ (count $cmd) -eq (count $argv) ]
for i in (seq (count $argv))
@ -33,8 +34,7 @@ impl<'a, 'b> FishGen<'a, 'b> {
return 1
end
"#
.to_string();
"#.to_string();
let mut buffer = detect_subcommand_function;
gen_fish_inner(command, self, &command.to_string(), &mut buffer);
@ -42,7 +42,10 @@ end
}
}
fn gen_fish_inner(root_command: &str, comp_gen: &FishGen, parent_cmds: &str, buffer: &mut String) {
fn gen_fish_inner(root_command: &str,
comp_gen: &FishGen,
parent_cmds: &str,
buffer: &mut String) {
// example :
//
// complete
@ -109,6 +112,9 @@ fn gen_fish_inner(root_command: &str, comp_gen: &FishGen, parent_cmds: &str, buf
sub_parent_cmds.push_str(" ");
}
sub_parent_cmds.push_str(&subcommand.p.meta.name);
gen_fish_inner(root_command, &sub_comp_gen, &sub_parent_cmds, buffer);
gen_fish_inner(root_command,
&sub_comp_gen,
&sub_parent_cmds,
buffer);
}
}

View file

@ -75,9 +75,7 @@ pub fn all_subcommands(p: &Parser) -> Vec<(String, String)> {
// Also note, aliases are treated as their own subcommands but duplicates of whatever they're
// aliasing.
pub fn subcommands_of(p: &Parser) -> Vec<(String, String)> {
debugln!("fn=subcommands_of;name={};bin_name={}",
p.meta.name,
p.meta.bin_name.as_ref().unwrap());
debugln!("fn=subcommands_of;name={};bin_name={}", p.meta.name, p.meta.bin_name.as_ref().unwrap());
let mut subcmds = vec![];
debug!("Has subcommands...");
@ -88,8 +86,7 @@ pub fn subcommands_of(p: &Parser) -> Vec<(String, String)> {
if let Some(ref aliases) = p.meta.aliases {
for &(n, _) in aliases {
debugln!("Found alias...{}", n);
let mut als_bin_name: Vec<_> =
p.meta.bin_name.as_ref().unwrap().split(' ').collect();
let mut als_bin_name: Vec<_> = p.meta.bin_name.as_ref().unwrap().split(' ').collect();
als_bin_name.push(n);
let old = als_bin_name.len() - 2;
als_bin_name.swap_remove(old);
@ -100,16 +97,13 @@ pub fn subcommands_of(p: &Parser) -> Vec<(String, String)> {
}
sdebugln!("Yes");
for sc in &p.subcommands {
debugln!("iter;name={};bin_name={}",
sc.p.meta.name,
sc.p.meta.bin_name.as_ref().unwrap());
debugln!("iter;name={};bin_name={}", sc.p.meta.name, sc.p.meta.bin_name.as_ref().unwrap());
debugln!("Looking for aliases...");
if let Some(ref aliases) = sc.p.meta.aliases {
for &(n, _) in aliases {
debugln!("Found alias...{}", n);
let mut als_bin_name: Vec<_> =
p.meta.bin_name.as_ref().unwrap().split(' ').collect();
let mut als_bin_name: Vec<_> = p.meta.bin_name.as_ref().unwrap().split(' ').collect();
als_bin_name.push(n);
let old = als_bin_name.len() - 2;
als_bin_name.swap_remove(old);

View file

@ -19,7 +19,7 @@ impl<'a, 'b> PowerShellGen<'a, 'b> {
pub fn generate_to<W: Write>(&self, buf: &mut W) {
let bin_name = self.p.meta.bin_name.as_ref().unwrap();
let (subcommands_detection_cases, subcommands_cases) = generate_inner(self.p, "");
let (subcommands_detection_cases, subcommands_cases) = generate_inner(&self.p, "");
let mut bin_names = vec![
bin_name.to_string(),
@ -32,9 +32,7 @@ impl<'a, 'b> PowerShellGen<'a, 'b> {
bin_names.push(format!(r"./{0}.exe", bin_name));
}
let bin_names = bin_names.iter().fold(String::new(), |previous, current| {
format!("{0}, '{1}'", previous, current)
});
let bin_names = bin_names.iter().fold(String::new(), |previous, current| format!("{0}, '{1}'", previous, current));
let bin_names = bin_names.trim_left_matches(", ");
let result = format!(r#"
@ -77,16 +75,17 @@ impl<'a, 'b> PowerShellGen<'a, 'b> {
fn generate_inner<'a, 'b>(p: &Parser<'a, 'b>, previous_command_name: &str) -> (String, String) {
let command_name = format!("{}_{}", previous_command_name, &p.meta.name);
let mut subcommands_detection_cases = if previous_command_name == "" {
let mut subcommands_detection_cases =
if previous_command_name == "" {
String::new()
} else {
}
else {
format!(r"
'{0}' {{
$command += '_{0}'
break
}}
",
&p.meta.name)
", &p.meta.name)
};
let mut completions = String::new();
@ -104,13 +103,10 @@ fn generate_inner<'a, 'b>(p: &Parser<'a, 'b>, previous_command_name: &str) -> (S
'{}' {{
$completions = @({})
}}
",
&command_name,
completions.trim_right_matches(", "));
", &command_name, completions.trim_right_matches(", "));
for subcommand in &p.subcommands {
let (subcommand_subcommands_detection_cases, subcommand_subcommands_cases) =
generate_inner(&subcommand.p, &command_name);
let (subcommand_subcommands_detection_cases, subcommand_subcommands_cases) = generate_inner(&subcommand.p, &command_name);
subcommands_detection_cases.push_str(&subcommand_subcommands_detection_cases);
subcommands_cases.push_str(&subcommand_subcommands_cases);
}

View file

@ -3,7 +3,6 @@ use std::str::FromStr;
use std::fmt;
/// Describes which shell to produce a completions file for
#[cfg_attr(feature = "lints", allow(enum_variant_names))]
#[derive(Debug, Copy, Clone)]
pub enum Shell {
/// Generates a .bash-completion completion file for the Bourne Again SHell (BASH)
@ -19,7 +18,12 @@ pub enum Shell {
impl Shell {
/// A list of possible variants in `&'static str` form
pub fn variants() -> [&'static str; 4] {
["zsh", "bash", "fish", "powershell"]
[
"zsh",
"bash",
"fish",
"powershell"
]
}
}
@ -33,7 +37,9 @@ impl FromStr for Shell {
"FISH" | _ if s.eq_ignore_ascii_case("fish") => Ok(Shell::Fish),
"BASH" | _ if s.eq_ignore_ascii_case("bash") => Ok(Shell::Bash),
"POWERSHELL" | _ if s.eq_ignore_ascii_case("powershell") => Ok(Shell::PowerShell),
_ => Err(String::from("[valid values: bash, fish, zsh, powershell]")),
_ => Err(
String::from("[valid values: bash, fish, zsh, powershell]")
),
}
}
}
@ -48,3 +54,4 @@ impl fmt::Display for Shell {
}
}
}

View file

@ -19,7 +19,9 @@ pub struct ZshGen<'a, 'b>
impl<'a, 'b> ZshGen<'a, 'b> {
pub fn new(p: &'b Parser<'a, 'b>) -> Self {
debugln!("fn=ZshGen::new;");
ZshGen { p: p }
ZshGen {
p: p,
}
}
pub fn generate_to<W: Write>(&self, buf: &mut W) {
@ -43,8 +45,7 @@ _{name} \"$@\"",
name = self.p.meta.bin_name.as_ref().unwrap(),
initial_args = get_args_of(self.p),
subcommands = get_subcommands_of(self.p),
subcommand_details = subcommand_details(self.p))
.as_bytes());
subcommand_details = subcommand_details(self.p)).as_bytes());
}
}
@ -87,8 +88,7 @@ _{bin_name_underscore}_commands() {{
)
_describe -t commands '{bin_name} commands' commands \"$@\"
}}",
bin_name_underscore =
p.meta.bin_name.as_ref().unwrap().replace(" ", "_"),
bin_name_underscore = p.meta.bin_name.as_ref().unwrap().replace(" ", "_"),
bin_name = p.meta.bin_name.as_ref().unwrap(),
subcommands_and_args = subcommands_and_args_of(p))];
@ -220,7 +220,8 @@ fn get_subcommands_of(p: &Parser) -> String {
subcmds.push(v.join("\n"));
}
format!("case $state in
format!(
"case $state in
({name})
curcontext=\"${{curcontext%:*:*}}:{name_hyphen}-command-$words[1]:\"
case $line[1] in
@ -299,17 +300,9 @@ fn write_opts_of(p: &Parser) -> String {
debugln!("iter;o={}", o.name());
let help = o.help().unwrap_or("");
let mut conflicts = get_zsh_arg_conflicts!(p, o, INTERNAL_ERROR_MSG);
conflicts = if conflicts.is_empty() {
String::new()
} else {
format!("({})", conflicts)
};
conflicts = if conflicts.is_empty() { String::new() } else { format!("({})", conflicts) };
let multiple = if o.is_set(ArgSettings::Multiple) {
"*"
} else {
""
};
let multiple = if o.is_set(ArgSettings::Multiple) { "*" } else { "" };
let pv = if let Some(pv_vec) = o.possible_vals() {
format!(": :({})", pv_vec.join(" "))
} else {
@ -349,17 +342,9 @@ fn write_flags_of(p: &Parser) -> String {
debugln!("iter;f={}", f.name());
let help = f.help().unwrap_or("");
let mut conflicts = get_zsh_arg_conflicts!(p, f, INTERNAL_ERROR_MSG);
conflicts = if conflicts.is_empty() {
String::new()
} else {
format!("({})", conflicts)
};
conflicts = if conflicts.is_empty() { String::new() } else { format!("({})", conflicts) };
let multiple = if f.is_set(ArgSettings::Multiple) {
"*"
} else {
""
};
let multiple = if f.is_set(ArgSettings::Multiple) { "*" } else { "" };
if let Some(short) = f.short() {
let s = format!("\"{conflicts}{multiple}-{arg}[{help}]\" \\",
multiple = multiple,

View file

@ -626,7 +626,7 @@ macro_rules! write_spaces {
($num:expr, $w:ident) => ({
debugln!("macro=write_spaces!;");
for _ in 0..$num {
write!($w, " ")?;
try!(write!($w, " "));
}
})
}
@ -639,7 +639,7 @@ macro_rules! write_nspaces {
($dst:expr, $num:expr) => ({
debugln!("macro=write_spaces!;num={}", $num);
for _ in 0..$num {
$dst.write(b" ")?;
try!($dst.write(b" "));
}
})
}

View file

@ -990,8 +990,7 @@ mod test {
assert!(d.is_set(ArgSettings::Multiple));
assert!(d.is_set(ArgSettings::TakesValue));
assert!(d.is_set(ArgSettings::Required));
assert_eq!(d.val_names.unwrap().values().collect::<Vec<_>>(),
[&"option"]);
assert_eq!(d.val_names.unwrap().values().collect::<Vec<_>>(), [&"option"]);
assert!(d.num_vals.is_none());
}
@ -1200,8 +1199,7 @@ mod test {
let a = Arg::from_usage("[ñämê] --ôpt=[üñíčöĐ€] 'hælp'");
assert_eq!(a.name, "ñämê");
assert_eq!(a.long, Some("ôpt"));
assert_eq!(a.val_names.unwrap().values().collect::<Vec<_>>(),
[&"üñíčöĐ€"]);
assert_eq!(a.val_names.unwrap().values().collect::<Vec<_>>(), [&"üñíčöĐ€"]);
assert_eq!(a.help, Some("hælp"));
}
}