mirror of
https://github.com/clap-rs/clap
synced 2025-03-04 23:37:32 +00:00
refactor: Remove use of '?'
This commit is contained in:
parent
18ed37ee67
commit
c4b3a4f491
16 changed files with 140 additions and 125 deletions
|
@ -822,7 +822,7 @@ impl Command {
|
||||||
let mut styled = StyledStr::new();
|
let mut styled = StyledStr::new();
|
||||||
let usage = Usage::new(self);
|
let usage = Usage::new(self);
|
||||||
write_help(&mut styled, self, &usage, false);
|
write_help(&mut styled, self, &usage, false);
|
||||||
write!(w, "{}", styled)?;
|
ok!(write!(w, "{}", styled));
|
||||||
w.flush()
|
w.flush()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -837,7 +837,7 @@ impl Command {
|
||||||
let mut styled = StyledStr::new();
|
let mut styled = StyledStr::new();
|
||||||
let usage = Usage::new(self);
|
let usage = Usage::new(self);
|
||||||
write_help(&mut styled, self, &usage, true);
|
write_help(&mut styled, self, &usage, true);
|
||||||
write!(w, "{}", styled)?;
|
ok!(write!(w, "{}", styled));
|
||||||
w.flush()
|
w.flush()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3896,7 +3896,7 @@ impl Command {
|
||||||
}
|
}
|
||||||
let is_multicall_set = self.is_multicall_set();
|
let is_multicall_set = self.is_multicall_set();
|
||||||
|
|
||||||
let sc = self.subcommands.iter_mut().find(|s| s.name == name)?;
|
let sc = some!(self.subcommands.iter_mut().find(|s| s.name == name));
|
||||||
|
|
||||||
// Display subcommand name, short and long in usage
|
// Display subcommand name, short and long in usage
|
||||||
let mut sc_names = String::new();
|
let mut sc_names = String::new();
|
||||||
|
|
|
@ -172,10 +172,10 @@ impl From<std::ops::RangeToInclusive<usize>> for ValueRange {
|
||||||
|
|
||||||
impl std::fmt::Display for ValueRange {
|
impl std::fmt::Display for ValueRange {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||||
self.start_inclusive.fmt(f)?;
|
ok!(self.start_inclusive.fmt(f));
|
||||||
if !self.is_fixed() {
|
if !self.is_fixed() {
|
||||||
"..=".fmt(f)?;
|
ok!("..=".fmt(f));
|
||||||
self.end_inclusive.fmt(f)?;
|
ok!(self.end_inclusive.fmt(f));
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -223,9 +223,9 @@ impl StyledStr {
|
||||||
None => {}
|
None => {}
|
||||||
}
|
}
|
||||||
|
|
||||||
buffer.set_color(&color)?;
|
ok!(buffer.set_color(&color));
|
||||||
buffer.write_all(content.as_bytes())?;
|
ok!(buffer.write_all(content.as_bytes()));
|
||||||
buffer.reset()?;
|
ok!(buffer.reset());
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -291,7 +291,7 @@ fn cmp_key(c: (Option<Style>, &str)) -> (Option<usize>, &str) {
|
||||||
impl std::fmt::Display for StyledStr {
|
impl std::fmt::Display for StyledStr {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||||
for (_, content) in self.iter() {
|
for (_, content) in self.iter() {
|
||||||
std::fmt::Display::fmt(content, f)?;
|
ok!(std::fmt::Display::fmt(content, f));
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -307,12 +307,13 @@ struct AnsiDisplay<'s> {
|
||||||
impl std::fmt::Display for AnsiDisplay<'_> {
|
impl std::fmt::Display for AnsiDisplay<'_> {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||||
let mut buffer = termcolor::Buffer::ansi();
|
let mut buffer = termcolor::Buffer::ansi();
|
||||||
self.styled
|
ok!(self
|
||||||
|
.styled
|
||||||
.write_colored(&mut buffer)
|
.write_colored(&mut buffer)
|
||||||
.map_err(|_| std::fmt::Error)?;
|
.map_err(|_| std::fmt::Error));
|
||||||
let buffer = buffer.into_inner();
|
let buffer = buffer.into_inner();
|
||||||
let buffer = String::from_utf8(buffer).map_err(|_| std::fmt::Error)?;
|
let buffer = ok!(String::from_utf8(buffer).map_err(|_| std::fmt::Error));
|
||||||
std::fmt::Display::fmt(&buffer, f)?;
|
ok!(std::fmt::Display::fmt(&buffer, f));
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -569,7 +569,7 @@ where
|
||||||
arg: Option<&crate::Arg>,
|
arg: Option<&crate::Arg>,
|
||||||
value: &std::ffi::OsStr,
|
value: &std::ffi::OsStr,
|
||||||
) -> Result<AnyValue, crate::Error> {
|
) -> Result<AnyValue, crate::Error> {
|
||||||
let value = TypedValueParser::parse_ref(self, cmd, arg, value)?;
|
let value = ok!(TypedValueParser::parse_ref(self, cmd, arg, value));
|
||||||
Ok(AnyValue::new(value))
|
Ok(AnyValue::new(value))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -579,7 +579,7 @@ where
|
||||||
arg: Option<&crate::Arg>,
|
arg: Option<&crate::Arg>,
|
||||||
value: std::ffi::OsString,
|
value: std::ffi::OsString,
|
||||||
) -> Result<AnyValue, crate::Error> {
|
) -> Result<AnyValue, crate::Error> {
|
||||||
let value = TypedValueParser::parse(self, cmd, arg, value)?;
|
let value = ok!(TypedValueParser::parse(self, cmd, arg, value));
|
||||||
Ok(AnyValue::new(value))
|
Ok(AnyValue::new(value))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -694,18 +694,18 @@ where
|
||||||
arg: Option<&crate::Arg>,
|
arg: Option<&crate::Arg>,
|
||||||
value: &std::ffi::OsStr,
|
value: &std::ffi::OsStr,
|
||||||
) -> Result<Self::Value, crate::Error> {
|
) -> Result<Self::Value, crate::Error> {
|
||||||
let value = value.to_str().ok_or_else(|| {
|
let value = ok!(value.to_str().ok_or_else(|| {
|
||||||
crate::Error::invalid_utf8(
|
crate::Error::invalid_utf8(
|
||||||
cmd,
|
cmd,
|
||||||
crate::output::Usage::new(cmd).create_usage_with_title(&[]),
|
crate::output::Usage::new(cmd).create_usage_with_title(&[]),
|
||||||
)
|
)
|
||||||
})?;
|
}));
|
||||||
let value = (self)(value).map_err(|e| {
|
let value = ok!((self)(value).map_err(|e| {
|
||||||
let arg = arg
|
let arg = arg
|
||||||
.map(|a| a.to_string())
|
.map(|a| a.to_string())
|
||||||
.unwrap_or_else(|| "...".to_owned());
|
.unwrap_or_else(|| "...".to_owned());
|
||||||
crate::Error::value_validation(arg, value.to_owned(), e.into()).with_cmd(cmd)
|
crate::Error::value_validation(arg, value.to_owned(), e.into()).with_cmd(cmd)
|
||||||
})?;
|
}));
|
||||||
Ok(value)
|
Ok(value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -742,12 +742,12 @@ impl TypedValueParser for StringValueParser {
|
||||||
_arg: Option<&crate::Arg>,
|
_arg: Option<&crate::Arg>,
|
||||||
value: std::ffi::OsString,
|
value: std::ffi::OsString,
|
||||||
) -> Result<Self::Value, crate::Error> {
|
) -> Result<Self::Value, crate::Error> {
|
||||||
let value = value.into_string().map_err(|_| {
|
let value = ok!(value.into_string().map_err(|_| {
|
||||||
crate::Error::invalid_utf8(
|
crate::Error::invalid_utf8(
|
||||||
cmd,
|
cmd,
|
||||||
crate::output::Usage::new(cmd).create_usage_with_title(&[]),
|
crate::output::Usage::new(cmd).create_usage_with_title(&[]),
|
||||||
)
|
)
|
||||||
})?;
|
}));
|
||||||
Ok(value)
|
Ok(value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -939,7 +939,7 @@ impl<E: crate::ValueEnum + Clone + Send + Sync + 'static> TypedValueParser for E
|
||||||
.collect::<Vec<_>>()
|
.collect::<Vec<_>>()
|
||||||
};
|
};
|
||||||
|
|
||||||
let value = value.to_str().ok_or_else(|| {
|
let value = ok!(value.to_str().ok_or_else(|| {
|
||||||
crate::Error::invalid_value(
|
crate::Error::invalid_value(
|
||||||
cmd,
|
cmd,
|
||||||
value.to_string_lossy().into_owned(),
|
value.to_string_lossy().into_owned(),
|
||||||
|
@ -947,8 +947,8 @@ impl<E: crate::ValueEnum + Clone + Send + Sync + 'static> TypedValueParser for E
|
||||||
arg.map(ToString::to_string)
|
arg.map(ToString::to_string)
|
||||||
.unwrap_or_else(|| "...".to_owned()),
|
.unwrap_or_else(|| "...".to_owned()),
|
||||||
)
|
)
|
||||||
})?;
|
}));
|
||||||
let value = E::value_variants()
|
let value = ok!(E::value_variants()
|
||||||
.iter()
|
.iter()
|
||||||
.find(|v| {
|
.find(|v| {
|
||||||
v.to_possible_value()
|
v.to_possible_value()
|
||||||
|
@ -963,7 +963,7 @@ impl<E: crate::ValueEnum + Clone + Send + Sync + 'static> TypedValueParser for E
|
||||||
arg.map(ToString::to_string)
|
arg.map(ToString::to_string)
|
||||||
.unwrap_or_else(|| "...".to_owned()),
|
.unwrap_or_else(|| "...".to_owned()),
|
||||||
)
|
)
|
||||||
})?
|
}))
|
||||||
.clone();
|
.clone();
|
||||||
Ok(value)
|
Ok(value)
|
||||||
}
|
}
|
||||||
|
@ -1049,12 +1049,12 @@ impl TypedValueParser for PossibleValuesParser {
|
||||||
arg: Option<&crate::Arg>,
|
arg: Option<&crate::Arg>,
|
||||||
value: std::ffi::OsString,
|
value: std::ffi::OsString,
|
||||||
) -> Result<String, crate::Error> {
|
) -> Result<String, crate::Error> {
|
||||||
let value = value.into_string().map_err(|_| {
|
let value = ok!(value.into_string().map_err(|_| {
|
||||||
crate::Error::invalid_utf8(
|
crate::Error::invalid_utf8(
|
||||||
cmd,
|
cmd,
|
||||||
crate::output::Usage::new(cmd).create_usage_with_title(&[]),
|
crate::output::Usage::new(cmd).create_usage_with_title(&[]),
|
||||||
)
|
)
|
||||||
})?;
|
}));
|
||||||
|
|
||||||
let ignore_case = arg.map(|a| a.is_ignore_case_set()).unwrap_or(false);
|
let ignore_case = arg.map(|a| a.is_ignore_case_set()).unwrap_or(false);
|
||||||
if self.0.iter().any(|v| v.matches(&value, ignore_case)) {
|
if self.0.iter().any(|v| v.matches(&value, ignore_case)) {
|
||||||
|
@ -1233,13 +1233,13 @@ where
|
||||||
arg: Option<&crate::Arg>,
|
arg: Option<&crate::Arg>,
|
||||||
raw_value: &std::ffi::OsStr,
|
raw_value: &std::ffi::OsStr,
|
||||||
) -> Result<Self::Value, crate::Error> {
|
) -> Result<Self::Value, crate::Error> {
|
||||||
let value = raw_value.to_str().ok_or_else(|| {
|
let value = ok!(raw_value.to_str().ok_or_else(|| {
|
||||||
crate::Error::invalid_utf8(
|
crate::Error::invalid_utf8(
|
||||||
cmd,
|
cmd,
|
||||||
crate::output::Usage::new(cmd).create_usage_with_title(&[]),
|
crate::output::Usage::new(cmd).create_usage_with_title(&[]),
|
||||||
)
|
)
|
||||||
})?;
|
}));
|
||||||
let value = value.parse::<i64>().map_err(|err| {
|
let value = ok!(value.parse::<i64>().map_err(|err| {
|
||||||
let arg = arg
|
let arg = arg
|
||||||
.map(|a| a.to_string())
|
.map(|a| a.to_string())
|
||||||
.unwrap_or_else(|| "...".to_owned());
|
.unwrap_or_else(|| "...".to_owned());
|
||||||
|
@ -1249,7 +1249,7 @@ where
|
||||||
err.into(),
|
err.into(),
|
||||||
)
|
)
|
||||||
.with_cmd(cmd)
|
.with_cmd(cmd)
|
||||||
})?;
|
}));
|
||||||
if !self.bounds.contains(&value) {
|
if !self.bounds.contains(&value) {
|
||||||
let arg = arg
|
let arg = arg
|
||||||
.map(|a| a.to_string())
|
.map(|a| a.to_string())
|
||||||
|
@ -1263,7 +1263,7 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
let value: Result<Self::Value, _> = value.try_into();
|
let value: Result<Self::Value, _> = value.try_into();
|
||||||
let value = value.map_err(|err| {
|
let value = ok!(value.map_err(|err| {
|
||||||
let arg = arg
|
let arg = arg
|
||||||
.map(|a| a.to_string())
|
.map(|a| a.to_string())
|
||||||
.unwrap_or_else(|| "...".to_owned());
|
.unwrap_or_else(|| "...".to_owned());
|
||||||
|
@ -1273,7 +1273,7 @@ where
|
||||||
err.into(),
|
err.into(),
|
||||||
)
|
)
|
||||||
.with_cmd(cmd)
|
.with_cmd(cmd)
|
||||||
})?;
|
}));
|
||||||
|
|
||||||
Ok(value)
|
Ok(value)
|
||||||
}
|
}
|
||||||
|
@ -1431,13 +1431,13 @@ where
|
||||||
arg: Option<&crate::Arg>,
|
arg: Option<&crate::Arg>,
|
||||||
raw_value: &std::ffi::OsStr,
|
raw_value: &std::ffi::OsStr,
|
||||||
) -> Result<Self::Value, crate::Error> {
|
) -> Result<Self::Value, crate::Error> {
|
||||||
let value = raw_value.to_str().ok_or_else(|| {
|
let value = ok!(raw_value.to_str().ok_or_else(|| {
|
||||||
crate::Error::invalid_utf8(
|
crate::Error::invalid_utf8(
|
||||||
cmd,
|
cmd,
|
||||||
crate::output::Usage::new(cmd).create_usage_with_title(&[]),
|
crate::output::Usage::new(cmd).create_usage_with_title(&[]),
|
||||||
)
|
)
|
||||||
})?;
|
}));
|
||||||
let value = value.parse::<u64>().map_err(|err| {
|
let value = ok!(value.parse::<u64>().map_err(|err| {
|
||||||
let arg = arg
|
let arg = arg
|
||||||
.map(|a| a.to_string())
|
.map(|a| a.to_string())
|
||||||
.unwrap_or_else(|| "...".to_owned());
|
.unwrap_or_else(|| "...".to_owned());
|
||||||
|
@ -1447,7 +1447,7 @@ where
|
||||||
err.into(),
|
err.into(),
|
||||||
)
|
)
|
||||||
.with_cmd(cmd)
|
.with_cmd(cmd)
|
||||||
})?;
|
}));
|
||||||
if !self.bounds.contains(&value) {
|
if !self.bounds.contains(&value) {
|
||||||
let arg = arg
|
let arg = arg
|
||||||
.map(|a| a.to_string())
|
.map(|a| a.to_string())
|
||||||
|
@ -1461,7 +1461,7 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
let value: Result<Self::Value, _> = value.try_into();
|
let value: Result<Self::Value, _> = value.try_into();
|
||||||
let value = value.map_err(|err| {
|
let value = ok!(value.map_err(|err| {
|
||||||
let arg = arg
|
let arg = arg
|
||||||
.map(|a| a.to_string())
|
.map(|a| a.to_string())
|
||||||
.unwrap_or_else(|| "...".to_owned());
|
.unwrap_or_else(|| "...".to_owned());
|
||||||
|
@ -1471,7 +1471,7 @@ where
|
||||||
err.into(),
|
err.into(),
|
||||||
)
|
)
|
||||||
.with_cmd(cmd)
|
.with_cmd(cmd)
|
||||||
})?;
|
}));
|
||||||
|
|
||||||
Ok(value)
|
Ok(value)
|
||||||
}
|
}
|
||||||
|
@ -1622,12 +1622,12 @@ impl TypedValueParser for FalseyValueParser {
|
||||||
_arg: Option<&crate::Arg>,
|
_arg: Option<&crate::Arg>,
|
||||||
value: &std::ffi::OsStr,
|
value: &std::ffi::OsStr,
|
||||||
) -> Result<Self::Value, crate::Error> {
|
) -> Result<Self::Value, crate::Error> {
|
||||||
let value = value.to_str().ok_or_else(|| {
|
let value = ok!(value.to_str().ok_or_else(|| {
|
||||||
crate::Error::invalid_utf8(
|
crate::Error::invalid_utf8(
|
||||||
cmd,
|
cmd,
|
||||||
crate::output::Usage::new(cmd).create_usage_with_title(&[]),
|
crate::output::Usage::new(cmd).create_usage_with_title(&[]),
|
||||||
)
|
)
|
||||||
})?;
|
}));
|
||||||
let value = if value.is_empty() {
|
let value = if value.is_empty() {
|
||||||
false
|
false
|
||||||
} else {
|
} else {
|
||||||
|
@ -1719,19 +1719,19 @@ impl TypedValueParser for BoolishValueParser {
|
||||||
arg: Option<&crate::Arg>,
|
arg: Option<&crate::Arg>,
|
||||||
value: &std::ffi::OsStr,
|
value: &std::ffi::OsStr,
|
||||||
) -> Result<Self::Value, crate::Error> {
|
) -> Result<Self::Value, crate::Error> {
|
||||||
let value = value.to_str().ok_or_else(|| {
|
let value = ok!(value.to_str().ok_or_else(|| {
|
||||||
crate::Error::invalid_utf8(
|
crate::Error::invalid_utf8(
|
||||||
cmd,
|
cmd,
|
||||||
crate::output::Usage::new(cmd).create_usage_with_title(&[]),
|
crate::output::Usage::new(cmd).create_usage_with_title(&[]),
|
||||||
)
|
)
|
||||||
})?;
|
}));
|
||||||
let value = crate::util::str_to_bool(value).ok_or_else(|| {
|
let value = ok!(crate::util::str_to_bool(value).ok_or_else(|| {
|
||||||
let arg = arg
|
let arg = arg
|
||||||
.map(|a| a.to_string())
|
.map(|a| a.to_string())
|
||||||
.unwrap_or_else(|| "...".to_owned());
|
.unwrap_or_else(|| "...".to_owned());
|
||||||
crate::Error::value_validation(arg, value.to_owned(), "value was not a boolean".into())
|
crate::Error::value_validation(arg, value.to_owned(), "value was not a boolean".into())
|
||||||
.with_cmd(cmd)
|
.with_cmd(cmd)
|
||||||
})?;
|
}));
|
||||||
Ok(value)
|
Ok(value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1808,12 +1808,12 @@ impl TypedValueParser for NonEmptyStringValueParser {
|
||||||
.unwrap_or_else(|| "...".to_owned()),
|
.unwrap_or_else(|| "...".to_owned()),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
let value = value.to_str().ok_or_else(|| {
|
let value = ok!(value.to_str().ok_or_else(|| {
|
||||||
crate::Error::invalid_utf8(
|
crate::Error::invalid_utf8(
|
||||||
cmd,
|
cmd,
|
||||||
crate::output::Usage::new(cmd).create_usage_with_title(&[]),
|
crate::output::Usage::new(cmd).create_usage_with_title(&[]),
|
||||||
)
|
)
|
||||||
})?;
|
}));
|
||||||
Ok(value.to_owned())
|
Ok(value.to_owned())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1860,7 +1860,7 @@ where
|
||||||
arg: Option<&crate::Arg>,
|
arg: Option<&crate::Arg>,
|
||||||
value: &std::ffi::OsStr,
|
value: &std::ffi::OsStr,
|
||||||
) -> Result<Self::Value, crate::Error> {
|
) -> Result<Self::Value, crate::Error> {
|
||||||
let value = self.parser.parse_ref(cmd, arg, value)?;
|
let value = ok!(self.parser.parse_ref(cmd, arg, value));
|
||||||
let value = (self.func)(value);
|
let value = (self.func)(value);
|
||||||
Ok(value)
|
Ok(value)
|
||||||
}
|
}
|
||||||
|
@ -1871,7 +1871,7 @@ where
|
||||||
arg: Option<&crate::Arg>,
|
arg: Option<&crate::Arg>,
|
||||||
value: std::ffi::OsString,
|
value: std::ffi::OsString,
|
||||||
) -> Result<Self::Value, crate::Error> {
|
) -> Result<Self::Value, crate::Error> {
|
||||||
let value = self.parser.parse(cmd, arg, value)?;
|
let value = ok!(self.parser.parse(cmd, arg, value));
|
||||||
let value = (self.func)(value);
|
let value = (self.func)(value);
|
||||||
Ok(value)
|
Ok(value)
|
||||||
}
|
}
|
||||||
|
|
|
@ -94,7 +94,7 @@ pub trait Parser: FromArgMatches + CommandFactory + Sized {
|
||||||
|
|
||||||
/// Parse from `std::env::args_os()`, return Err on error.
|
/// Parse from `std::env::args_os()`, return Err on error.
|
||||||
fn try_parse() -> Result<Self, Error> {
|
fn try_parse() -> Result<Self, Error> {
|
||||||
let mut matches = <Self as CommandFactory>::command().try_get_matches()?;
|
let mut matches = ok!(<Self as CommandFactory>::command().try_get_matches());
|
||||||
<Self as FromArgMatches>::from_arg_matches_mut(&mut matches).map_err(format_error::<Self>)
|
<Self as FromArgMatches>::from_arg_matches_mut(&mut matches).map_err(format_error::<Self>)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -123,7 +123,7 @@ pub trait Parser: FromArgMatches + CommandFactory + Sized {
|
||||||
I: IntoIterator<Item = T>,
|
I: IntoIterator<Item = T>,
|
||||||
T: Into<OsString> + Clone,
|
T: Into<OsString> + Clone,
|
||||||
{
|
{
|
||||||
let mut matches = <Self as CommandFactory>::command().try_get_matches_from(itr)?;
|
let mut matches = ok!(<Self as CommandFactory>::command().try_get_matches_from(itr));
|
||||||
<Self as FromArgMatches>::from_arg_matches_mut(&mut matches).map_err(format_error::<Self>)
|
<Self as FromArgMatches>::from_arg_matches_mut(&mut matches).map_err(format_error::<Self>)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -150,7 +150,7 @@ pub trait Parser: FromArgMatches + CommandFactory + Sized {
|
||||||
T: Into<OsString> + Clone,
|
T: Into<OsString> + Clone,
|
||||||
{
|
{
|
||||||
let mut matches =
|
let mut matches =
|
||||||
<Self as CommandFactory>::command_for_update().try_get_matches_from(itr)?;
|
ok!(<Self as CommandFactory>::command_for_update().try_get_matches_from(itr));
|
||||||
<Self as FromArgMatches>::update_from_arg_matches_mut(self, &mut matches)
|
<Self as FromArgMatches>::update_from_arg_matches_mut(self, &mut matches)
|
||||||
.map_err(format_error::<Self>)
|
.map_err(format_error::<Self>)
|
||||||
}
|
}
|
||||||
|
|
|
@ -694,11 +694,11 @@ impl<F: ErrorFormatter> error::Error for Error<F> {
|
||||||
impl<F: ErrorFormatter> Display for Error<F> {
|
impl<F: ErrorFormatter> Display for Error<F> {
|
||||||
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
||||||
// Assuming `self.message` already has a trailing newline, from `try_help` or similar
|
// Assuming `self.message` already has a trailing newline, from `try_help` or similar
|
||||||
write!(f, "{}", self.formatted())?;
|
ok!(write!(f, "{}", self.formatted()));
|
||||||
if let Some(backtrace) = self.inner.backtrace.as_ref() {
|
if let Some(backtrace) = self.inner.backtrace.as_ref() {
|
||||||
writeln!(f)?;
|
ok!(writeln!(f));
|
||||||
writeln!(f, "Backtrace:")?;
|
ok!(writeln!(f, "Backtrace:"));
|
||||||
writeln!(f, "{}", backtrace)?;
|
ok!(writeln!(f, "{}", backtrace));
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -661,3 +661,15 @@ macro_rules! debug {
|
||||||
macro_rules! debug {
|
macro_rules! debug {
|
||||||
($($arg:tt)*) => {};
|
($($arg:tt)*) => {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
macro_rules! ok {
|
||||||
|
($expr:expr) => {
|
||||||
|
$expr?
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
macro_rules! some {
|
||||||
|
($expr:expr) => {
|
||||||
|
$expr?
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
|
@ -48,7 +48,7 @@ impl Colorizer {
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut buffer = writer.buffer();
|
let mut buffer = writer.buffer();
|
||||||
self.content.write_colored(&mut buffer)?;
|
ok!(self.content.write_colored(&mut buffer));
|
||||||
writer.print(&buffer)
|
writer.print(&buffer)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -987,7 +987,9 @@ pub(crate) fn dimensions() -> (Option<usize>, Option<usize>) {
|
||||||
|
|
||||||
#[cfg(feature = "wrap_help")]
|
#[cfg(feature = "wrap_help")]
|
||||||
fn parse_env(var: &str) -> Option<usize> {
|
fn parse_env(var: &str) -> Option<usize> {
|
||||||
std::env::var_os(var)?.to_str()?.parse::<usize>().ok()
|
some!(some!(std::env::var_os(var)).to_str())
|
||||||
|
.parse::<usize>()
|
||||||
|
.ok()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn should_show_arg(use_long: bool, arg: &Arg) -> bool {
|
fn should_show_arg(use_long: bool, arg: &Arg) -> bool {
|
||||||
|
|
|
@ -35,7 +35,7 @@ impl<'cmd> Usage<'cmd> {
|
||||||
// any subcommands have been parsed (so as to give subcommands their own usage recursively)
|
// any subcommands have been parsed (so as to give subcommands their own usage recursively)
|
||||||
pub(crate) fn create_usage_with_title(&self, used: &[Id]) -> Option<StyledStr> {
|
pub(crate) fn create_usage_with_title(&self, used: &[Id]) -> Option<StyledStr> {
|
||||||
debug!("Usage::create_usage_with_title");
|
debug!("Usage::create_usage_with_title");
|
||||||
let usage = self.create_usage_no_title(used)?;
|
let usage = some!(self.create_usage_no_title(used));
|
||||||
|
|
||||||
let mut styled = StyledStr::new();
|
let mut styled = StyledStr::new();
|
||||||
styled.header("Usage:");
|
styled.header("Usage:");
|
||||||
|
|
|
@ -63,8 +63,8 @@ where
|
||||||
|
|
||||||
let subcommand_name = subcommand.get_name();
|
let subcommand_name = subcommand.get_name();
|
||||||
|
|
||||||
let candidate = did_you_mean(arg, longs).pop()?;
|
let candidate = some!(did_you_mean(arg, longs).pop());
|
||||||
let score = remaining_args.iter().position(|x| subcommand_name == *x)?;
|
let score = some!(remaining_args.iter().position(|x| subcommand_name == *x));
|
||||||
Some((score, (candidate, Some(subcommand_name.to_string()))))
|
Some((score, (candidate, Some(subcommand_name.to_string()))))
|
||||||
})
|
})
|
||||||
.min_by_key(|(x, _)| *x)
|
.min_by_key(|(x, _)| *x)
|
||||||
|
|
|
@ -22,7 +22,7 @@ impl AnyValue {
|
||||||
pub(crate) fn downcast_into<T: std::any::Any + Clone + Send + Sync>(self) -> Result<T, Self> {
|
pub(crate) fn downcast_into<T: std::any::Any + Clone + Send + Sync>(self) -> Result<T, Self> {
|
||||||
let id = self.id;
|
let id = self.id;
|
||||||
let value =
|
let value =
|
||||||
std::sync::Arc::downcast::<T>(self.inner).map_err(|inner| Self { inner, id })?;
|
ok!(std::sync::Arc::downcast::<T>(self.inner).map_err(|inner| Self { inner, id }));
|
||||||
let value = std::sync::Arc::try_unwrap(value).unwrap_or_else(|arc| (*arc).clone());
|
let value = std::sync::Arc::try_unwrap(value).unwrap_or_else(|arc| (*arc).clone());
|
||||||
Ok(value)
|
Ok(value)
|
||||||
}
|
}
|
||||||
|
|
|
@ -454,7 +454,7 @@ impl ArgMatches {
|
||||||
#[cfg(feature = "unstable-grouped")]
|
#[cfg(feature = "unstable-grouped")]
|
||||||
#[cfg_attr(debug_assertions, track_caller)]
|
#[cfg_attr(debug_assertions, track_caller)]
|
||||||
pub fn grouped_values_of(&self, id: &str) -> Option<GroupedValues> {
|
pub fn grouped_values_of(&self, id: &str) -> Option<GroupedValues> {
|
||||||
let arg = self.get_arg(id)?;
|
let arg = some!(self.get_arg(id));
|
||||||
let v = GroupedValues {
|
let v = GroupedValues {
|
||||||
iter: arg.vals().map(|g| g.iter().map(unwrap_string).collect()),
|
iter: arg.vals().map(|g| g.iter().map(unwrap_string).collect()),
|
||||||
len: arg.vals().len(),
|
len: arg.vals().len(),
|
||||||
|
@ -636,8 +636,8 @@ impl ArgMatches {
|
||||||
/// [delimiter]: crate::Arg::value_delimiter()
|
/// [delimiter]: crate::Arg::value_delimiter()
|
||||||
#[cfg_attr(debug_assertions, track_caller)]
|
#[cfg_attr(debug_assertions, track_caller)]
|
||||||
pub fn index_of(&self, id: &str) -> Option<usize> {
|
pub fn index_of(&self, id: &str) -> Option<usize> {
|
||||||
let arg = self.get_arg(id)?;
|
let arg = some!(self.get_arg(id));
|
||||||
let i = arg.get_index(0)?;
|
let i = some!(arg.get_index(0));
|
||||||
Some(i)
|
Some(i)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -718,7 +718,7 @@ impl ArgMatches {
|
||||||
/// [delimiter]: Arg::value_delimiter()
|
/// [delimiter]: Arg::value_delimiter()
|
||||||
#[cfg_attr(debug_assertions, track_caller)]
|
#[cfg_attr(debug_assertions, track_caller)]
|
||||||
pub fn indices_of(&self, id: &str) -> Option<Indices<'_>> {
|
pub fn indices_of(&self, id: &str) -> Option<Indices<'_>> {
|
||||||
let arg = self.get_arg(id)?;
|
let arg = some!(self.get_arg(id));
|
||||||
let i = Indices {
|
let i = Indices {
|
||||||
iter: arg.indices(),
|
iter: arg.indices(),
|
||||||
len: arg.num_vals(),
|
len: arg.num_vals(),
|
||||||
|
@ -941,7 +941,7 @@ impl ArgMatches {
|
||||||
&self,
|
&self,
|
||||||
id: &str,
|
id: &str,
|
||||||
) -> Result<Option<&T>, MatchesError> {
|
) -> Result<Option<&T>, MatchesError> {
|
||||||
let arg = self.try_get_arg_t::<T>(id)?;
|
let arg = ok!(self.try_get_arg_t::<T>(id));
|
||||||
let value = match arg.and_then(|a| a.first()) {
|
let value = match arg.and_then(|a| a.first()) {
|
||||||
Some(value) => value,
|
Some(value) => value,
|
||||||
None => {
|
None => {
|
||||||
|
@ -959,7 +959,7 @@ impl ArgMatches {
|
||||||
&self,
|
&self,
|
||||||
id: &str,
|
id: &str,
|
||||||
) -> Result<Option<ValuesRef<T>>, MatchesError> {
|
) -> Result<Option<ValuesRef<T>>, MatchesError> {
|
||||||
let arg = match self.try_get_arg_t::<T>(id)? {
|
let arg = match ok!(self.try_get_arg_t::<T>(id)) {
|
||||||
Some(arg) => arg,
|
Some(arg) => arg,
|
||||||
None => return Ok(None),
|
None => return Ok(None),
|
||||||
};
|
};
|
||||||
|
@ -975,7 +975,7 @@ impl ArgMatches {
|
||||||
|
|
||||||
/// Non-panicking version of [`ArgMatches::get_raw`]
|
/// Non-panicking version of [`ArgMatches::get_raw`]
|
||||||
pub fn try_get_raw(&self, id: &str) -> Result<Option<RawValues<'_>>, MatchesError> {
|
pub fn try_get_raw(&self, id: &str) -> Result<Option<RawValues<'_>>, MatchesError> {
|
||||||
let arg = match self.try_get_arg(id)? {
|
let arg = match ok!(self.try_get_arg(id)) {
|
||||||
Some(arg) => arg,
|
Some(arg) => arg,
|
||||||
None => return Ok(None),
|
None => return Ok(None),
|
||||||
};
|
};
|
||||||
|
@ -993,7 +993,7 @@ impl ArgMatches {
|
||||||
&mut self,
|
&mut self,
|
||||||
id: &str,
|
id: &str,
|
||||||
) -> Result<Option<T>, MatchesError> {
|
) -> Result<Option<T>, MatchesError> {
|
||||||
match self.try_remove_arg_t::<T>(id)? {
|
match ok!(self.try_remove_arg_t::<T>(id)) {
|
||||||
Some(values) => Ok(values
|
Some(values) => Ok(values
|
||||||
.into_vals_flatten()
|
.into_vals_flatten()
|
||||||
// enforced by `try_get_arg_t`
|
// enforced by `try_get_arg_t`
|
||||||
|
@ -1008,7 +1008,7 @@ impl ArgMatches {
|
||||||
&mut self,
|
&mut self,
|
||||||
id: &str,
|
id: &str,
|
||||||
) -> Result<Option<Values<T>>, MatchesError> {
|
) -> Result<Option<Values<T>>, MatchesError> {
|
||||||
let arg = match self.try_remove_arg_t::<T>(id)? {
|
let arg = match ok!(self.try_remove_arg_t::<T>(id)) {
|
||||||
Some(arg) => arg,
|
Some(arg) => arg,
|
||||||
None => return Ok(None),
|
None => return Ok(None),
|
||||||
};
|
};
|
||||||
|
@ -1024,7 +1024,7 @@ impl ArgMatches {
|
||||||
|
|
||||||
/// Non-panicking version of [`ArgMatches::contains_id`]
|
/// Non-panicking version of [`ArgMatches::contains_id`]
|
||||||
pub fn try_contains_id(&self, id: &str) -> Result<bool, MatchesError> {
|
pub fn try_contains_id(&self, id: &str) -> Result<bool, MatchesError> {
|
||||||
self.verify_arg(id)?;
|
ok!(self.verify_arg(id));
|
||||||
|
|
||||||
let presence = self.args.contains_key(id);
|
let presence = self.args.contains_key(id);
|
||||||
Ok(presence)
|
Ok(presence)
|
||||||
|
@ -1035,7 +1035,7 @@ impl ArgMatches {
|
||||||
impl ArgMatches {
|
impl ArgMatches {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn try_get_arg(&self, arg: &str) -> Result<Option<&MatchedArg>, MatchesError> {
|
fn try_get_arg(&self, arg: &str) -> Result<Option<&MatchedArg>, MatchesError> {
|
||||||
self.verify_arg(arg)?;
|
ok!(self.verify_arg(arg));
|
||||||
Ok(self.args.get(arg))
|
Ok(self.args.get(arg))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1044,13 +1044,13 @@ impl ArgMatches {
|
||||||
&self,
|
&self,
|
||||||
arg: &str,
|
arg: &str,
|
||||||
) -> Result<Option<&MatchedArg>, MatchesError> {
|
) -> Result<Option<&MatchedArg>, MatchesError> {
|
||||||
let arg = match self.try_get_arg(arg)? {
|
let arg = match ok!(self.try_get_arg(arg)) {
|
||||||
Some(arg) => arg,
|
Some(arg) => arg,
|
||||||
None => {
|
None => {
|
||||||
return Ok(None);
|
return Ok(None);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
self.verify_arg_t::<T>(arg)?;
|
ok!(self.verify_arg_t::<T>(arg));
|
||||||
Ok(Some(arg))
|
Ok(Some(arg))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1059,7 +1059,7 @@ impl ArgMatches {
|
||||||
&mut self,
|
&mut self,
|
||||||
arg: &str,
|
arg: &str,
|
||||||
) -> Result<Option<MatchedArg>, MatchesError> {
|
) -> Result<Option<MatchedArg>, MatchesError> {
|
||||||
self.verify_arg(arg)?;
|
ok!(self.verify_arg(arg));
|
||||||
let (id, matched) = match self.args.remove_entry(arg) {
|
let (id, matched) = match self.args.remove_entry(arg) {
|
||||||
Some((id, matched)) => (id, matched),
|
Some((id, matched)) => (id, matched),
|
||||||
None => {
|
None => {
|
||||||
|
|
|
@ -108,7 +108,7 @@ impl<'cmd> Parser<'cmd> {
|
||||||
debug!("Parser::get_matches_with: sc={:?}", sc_name);
|
debug!("Parser::get_matches_with: sc={:?}", sc_name);
|
||||||
if let Some(sc_name) = sc_name {
|
if let Some(sc_name) = sc_name {
|
||||||
if sc_name == "help" && !self.cmd.is_disable_help_subcommand_set() {
|
if sc_name == "help" && !self.cmd.is_disable_help_subcommand_set() {
|
||||||
self.parse_help_subcommand(raw_args.remaining(&mut args_cursor))?;
|
ok!(self.parse_help_subcommand(raw_args.remaining(&mut args_cursor)));
|
||||||
unreachable!("`parse_help_subcommand` always errors");
|
unreachable!("`parse_help_subcommand` always errors");
|
||||||
} else {
|
} else {
|
||||||
subcmd_name = Some(sc_name.to_owned());
|
subcmd_name = Some(sc_name.to_owned());
|
||||||
|
@ -129,14 +129,14 @@ impl<'cmd> Parser<'cmd> {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
} else if let Some((long_arg, long_value)) = arg_os.to_long() {
|
} else if let Some((long_arg, long_value)) = arg_os.to_long() {
|
||||||
let parse_result = self.parse_long_arg(
|
let parse_result = ok!(self.parse_long_arg(
|
||||||
matcher,
|
matcher,
|
||||||
long_arg,
|
long_arg,
|
||||||
long_value,
|
long_value,
|
||||||
&parse_state,
|
&parse_state,
|
||||||
pos_counter,
|
pos_counter,
|
||||||
&mut valid_arg_found,
|
&mut valid_arg_found,
|
||||||
)?;
|
));
|
||||||
debug!(
|
debug!(
|
||||||
"Parser::get_matches_with: After parse_long_arg {:?}",
|
"Parser::get_matches_with: After parse_long_arg {:?}",
|
||||||
parse_result
|
parse_result
|
||||||
|
@ -199,13 +199,13 @@ impl<'cmd> Parser<'cmd> {
|
||||||
// Try to parse short args like normal, if allow_hyphen_values or
|
// Try to parse short args like normal, if allow_hyphen_values or
|
||||||
// AllowNegativeNumbers is set, parse_short_arg will *not* throw
|
// AllowNegativeNumbers is set, parse_short_arg will *not* throw
|
||||||
// an error, and instead return Ok(None)
|
// an error, and instead return Ok(None)
|
||||||
let parse_result = self.parse_short_arg(
|
let parse_result = ok!(self.parse_short_arg(
|
||||||
matcher,
|
matcher,
|
||||||
short_arg,
|
short_arg,
|
||||||
&parse_state,
|
&parse_state,
|
||||||
pos_counter,
|
pos_counter,
|
||||||
&mut valid_arg_found,
|
&mut valid_arg_found,
|
||||||
)?;
|
));
|
||||||
// If it's None, we then check if one of those two AppSettings was set
|
// If it's None, we then check if one of those two AppSettings was set
|
||||||
debug!(
|
debug!(
|
||||||
"Parser::get_matches_with: After parse_short_arg {:?}",
|
"Parser::get_matches_with: After parse_short_arg {:?}",
|
||||||
|
@ -388,7 +388,7 @@ impl<'cmd> Parser<'cmd> {
|
||||||
}
|
}
|
||||||
|
|
||||||
if matcher.pending_arg_id() != Some(&arg.id) || !arg.is_multiple_values_set() {
|
if matcher.pending_arg_id() != Some(&arg.id) || !arg.is_multiple_values_set() {
|
||||||
self.resolve_pending(matcher)?;
|
ok!(self.resolve_pending(matcher));
|
||||||
}
|
}
|
||||||
if let Some(_parse_result) = self.check_terminator(arg, arg_os.to_value_os()) {
|
if let Some(_parse_result) = self.check_terminator(arg, arg_os.to_value_os()) {
|
||||||
debug!(
|
debug!(
|
||||||
|
@ -432,7 +432,7 @@ impl<'cmd> Parser<'cmd> {
|
||||||
sc_m.start_occurrence_of_external(self.cmd);
|
sc_m.start_occurrence_of_external(self.cmd);
|
||||||
|
|
||||||
for raw_val in raw_args.remaining(&mut args_cursor) {
|
for raw_val in raw_args.remaining(&mut args_cursor) {
|
||||||
let val = external_parser.parse_ref(self.cmd, None, raw_val)?;
|
let val = ok!(external_parser.parse_ref(self.cmd, None, raw_val));
|
||||||
let external_id = Id::from_static_ref(Id::EXTERNAL);
|
let external_id = Id::from_static_ref(Id::EXTERNAL);
|
||||||
sc_m.add_val_to(&external_id, val, raw_val.to_os_string());
|
sc_m.add_val_to(&external_id, val, raw_val.to_os_string());
|
||||||
}
|
}
|
||||||
|
@ -442,10 +442,10 @@ impl<'cmd> Parser<'cmd> {
|
||||||
matches: sc_m.into_inner(),
|
matches: sc_m.into_inner(),
|
||||||
});
|
});
|
||||||
|
|
||||||
self.resolve_pending(matcher)?;
|
ok!(self.resolve_pending(matcher));
|
||||||
#[cfg(feature = "env")]
|
#[cfg(feature = "env")]
|
||||||
self.add_env(matcher)?;
|
ok!(self.add_env(matcher));
|
||||||
self.add_defaults(matcher)?;
|
ok!(self.add_defaults(matcher));
|
||||||
return Validator::new(self.cmd).validate(parse_state, matcher);
|
return Validator::new(self.cmd).validate(parse_state, matcher);
|
||||||
} else {
|
} else {
|
||||||
// Start error processing
|
// Start error processing
|
||||||
|
@ -461,13 +461,13 @@ impl<'cmd> Parser<'cmd> {
|
||||||
.expect(INTERNAL_ERROR_MSG)
|
.expect(INTERNAL_ERROR_MSG)
|
||||||
.get_name()
|
.get_name()
|
||||||
.to_owned();
|
.to_owned();
|
||||||
self.parse_subcommand(&sc_name, matcher, raw_args, args_cursor, keep_state)?;
|
ok!(self.parse_subcommand(&sc_name, matcher, raw_args, args_cursor, keep_state));
|
||||||
}
|
}
|
||||||
|
|
||||||
self.resolve_pending(matcher)?;
|
ok!(self.resolve_pending(matcher));
|
||||||
#[cfg(feature = "env")]
|
#[cfg(feature = "env")]
|
||||||
self.add_env(matcher)?;
|
ok!(self.add_env(matcher));
|
||||||
self.add_defaults(matcher)?;
|
ok!(self.add_defaults(matcher));
|
||||||
Validator::new(self.cmd).validate(parse_state, matcher)
|
Validator::new(self.cmd).validate(parse_state, matcher)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -540,7 +540,7 @@ impl<'cmd> Parser<'cmd> {
|
||||||
valid_arg_found: bool,
|
valid_arg_found: bool,
|
||||||
) -> Option<&str> {
|
) -> Option<&str> {
|
||||||
debug!("Parser::possible_subcommand: arg={:?}", arg);
|
debug!("Parser::possible_subcommand: arg={:?}", arg);
|
||||||
let arg = arg.ok()?;
|
let arg = some!(arg.ok());
|
||||||
|
|
||||||
if !(self.cmd.is_args_conflicts_with_subcommands_set() && valid_arg_found) {
|
if !(self.cmd.is_args_conflicts_with_subcommands_set() && valid_arg_found) {
|
||||||
if self.cmd.is_infer_subcommands_set() {
|
if self.cmd.is_infer_subcommands_set() {
|
||||||
|
@ -910,14 +910,14 @@ impl<'cmd> Parser<'cmd> {
|
||||||
if !arg.is_takes_value_set() {
|
if !arg.is_takes_value_set() {
|
||||||
let arg_values = Vec::new();
|
let arg_values = Vec::new();
|
||||||
let trailing_idx = None;
|
let trailing_idx = None;
|
||||||
ret = self.react(
|
ret = ok!(self.react(
|
||||||
Some(ident),
|
Some(ident),
|
||||||
ValueSource::CommandLine,
|
ValueSource::CommandLine,
|
||||||
arg,
|
arg,
|
||||||
arg_values,
|
arg_values,
|
||||||
trailing_idx,
|
trailing_idx,
|
||||||
matcher,
|
matcher,
|
||||||
)?;
|
));
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -943,7 +943,7 @@ impl<'cmd> Parser<'cmd> {
|
||||||
} else {
|
} else {
|
||||||
(val, false)
|
(val, false)
|
||||||
};
|
};
|
||||||
match self.parse_opt_value(ident, val, arg, matcher, has_eq)? {
|
match ok!(self.parse_opt_value(ident, val, arg, matcher, has_eq)) {
|
||||||
ParseResult::AttachedValueNotConsumed => continue,
|
ParseResult::AttachedValueNotConsumed => continue,
|
||||||
x => return Ok(x),
|
x => return Ok(x),
|
||||||
}
|
}
|
||||||
|
@ -952,7 +952,7 @@ impl<'cmd> Parser<'cmd> {
|
||||||
return if let Some(sc_name) = self.cmd.find_short_subcmd(c) {
|
return if let Some(sc_name) = self.cmd.find_short_subcmd(c) {
|
||||||
debug!("Parser::parse_short_arg:iter:{}: subcommand={}", c, sc_name);
|
debug!("Parser::parse_short_arg:iter:{}: subcommand={}", c, sc_name);
|
||||||
// Make sure indices get updated before reading `self.cur_idx`
|
// Make sure indices get updated before reading `self.cur_idx`
|
||||||
self.resolve_pending(matcher)?;
|
ok!(self.resolve_pending(matcher));
|
||||||
self.cur_idx.set(self.cur_idx.get() + 1);
|
self.cur_idx.set(self.cur_idx.get() + 1);
|
||||||
debug!("Parser::parse_short_arg: cur_idx:={}", self.cur_idx.get());
|
debug!("Parser::parse_short_arg: cur_idx:={}", self.cur_idx.get());
|
||||||
|
|
||||||
|
@ -999,14 +999,14 @@ impl<'cmd> Parser<'cmd> {
|
||||||
debug!("Requires equals, but min_vals == 0");
|
debug!("Requires equals, but min_vals == 0");
|
||||||
let arg_values = Vec::new();
|
let arg_values = Vec::new();
|
||||||
let trailing_idx = None;
|
let trailing_idx = None;
|
||||||
let react_result = self.react(
|
let react_result = ok!(self.react(
|
||||||
Some(ident),
|
Some(ident),
|
||||||
ValueSource::CommandLine,
|
ValueSource::CommandLine,
|
||||||
arg,
|
arg,
|
||||||
arg_values,
|
arg_values,
|
||||||
trailing_idx,
|
trailing_idx,
|
||||||
matcher,
|
matcher,
|
||||||
)?;
|
));
|
||||||
debug_assert_eq!(react_result, ParseResult::ValuesDone);
|
debug_assert_eq!(react_result, ParseResult::ValuesDone);
|
||||||
if attached_value.is_some() {
|
if attached_value.is_some() {
|
||||||
Ok(ParseResult::AttachedValueNotConsumed)
|
Ok(ParseResult::AttachedValueNotConsumed)
|
||||||
|
@ -1022,20 +1022,20 @@ impl<'cmd> Parser<'cmd> {
|
||||||
} else if let Some(v) = attached_value {
|
} else if let Some(v) = attached_value {
|
||||||
let arg_values = vec![v.to_os_str().into_owned()];
|
let arg_values = vec![v.to_os_str().into_owned()];
|
||||||
let trailing_idx = None;
|
let trailing_idx = None;
|
||||||
let react_result = self.react(
|
let react_result = ok!(self.react(
|
||||||
Some(ident),
|
Some(ident),
|
||||||
ValueSource::CommandLine,
|
ValueSource::CommandLine,
|
||||||
arg,
|
arg,
|
||||||
arg_values,
|
arg_values,
|
||||||
trailing_idx,
|
trailing_idx,
|
||||||
matcher,
|
matcher,
|
||||||
)?;
|
));
|
||||||
debug_assert_eq!(react_result, ParseResult::ValuesDone);
|
debug_assert_eq!(react_result, ParseResult::ValuesDone);
|
||||||
// Attached are always done
|
// Attached are always done
|
||||||
Ok(ParseResult::ValuesDone)
|
Ok(ParseResult::ValuesDone)
|
||||||
} else {
|
} else {
|
||||||
debug!("Parser::parse_opt_value: More arg vals required...");
|
debug!("Parser::parse_opt_value: More arg vals required...");
|
||||||
self.resolve_pending(matcher)?;
|
ok!(self.resolve_pending(matcher));
|
||||||
let trailing_values = false;
|
let trailing_values = false;
|
||||||
matcher.pending_values_mut(&arg.id, Some(ident), trailing_values);
|
matcher.pending_values_mut(&arg.id, Some(ident), trailing_values);
|
||||||
Ok(ParseResult::Opt(arg.id.clone()))
|
Ok(ParseResult::Opt(arg.id.clone()))
|
||||||
|
@ -1072,7 +1072,7 @@ impl<'cmd> Parser<'cmd> {
|
||||||
self.cur_idx.get()
|
self.cur_idx.get()
|
||||||
);
|
);
|
||||||
let value_parser = arg.get_value_parser();
|
let value_parser = arg.get_value_parser();
|
||||||
let val = value_parser.parse_ref(self.cmd, Some(arg), &raw_val)?;
|
let val = ok!(value_parser.parse_ref(self.cmd, Some(arg), &raw_val));
|
||||||
|
|
||||||
matcher.add_val_to(&arg.id, val, raw_val);
|
matcher.add_val_to(&arg.id, val, raw_val);
|
||||||
matcher.add_index_to(&arg.id, self.cur_idx.get());
|
matcher.add_index_to(&arg.id, self.cur_idx.get());
|
||||||
|
@ -1100,14 +1100,14 @@ impl<'cmd> Parser<'cmd> {
|
||||||
|
|
||||||
debug!("Parser::resolve_pending: id={:?}", pending.id);
|
debug!("Parser::resolve_pending: id={:?}", pending.id);
|
||||||
let arg = self.cmd.find(&pending.id).expect(INTERNAL_ERROR_MSG);
|
let arg = self.cmd.find(&pending.id).expect(INTERNAL_ERROR_MSG);
|
||||||
let _ = self.react(
|
let _ = ok!(self.react(
|
||||||
pending.ident,
|
pending.ident,
|
||||||
ValueSource::CommandLine,
|
ValueSource::CommandLine,
|
||||||
arg,
|
arg,
|
||||||
pending.raw_vals,
|
pending.raw_vals,
|
||||||
pending.trailing_idx,
|
pending.trailing_idx,
|
||||||
matcher,
|
matcher,
|
||||||
)?;
|
));
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -1121,7 +1121,7 @@ impl<'cmd> Parser<'cmd> {
|
||||||
mut trailing_idx: Option<usize>,
|
mut trailing_idx: Option<usize>,
|
||||||
matcher: &mut ArgMatcher,
|
matcher: &mut ArgMatcher,
|
||||||
) -> ClapResult<ParseResult> {
|
) -> ClapResult<ParseResult> {
|
||||||
self.resolve_pending(matcher)?;
|
ok!(self.resolve_pending(matcher));
|
||||||
|
|
||||||
debug!(
|
debug!(
|
||||||
"Parser::react action={:?}, identifier={:?}, source={:?}",
|
"Parser::react action={:?}, identifier={:?}, source={:?}",
|
||||||
|
@ -1133,7 +1133,7 @@ impl<'cmd> Parser<'cmd> {
|
||||||
// Process before `default_missing_values` to avoid it counting as values from the command
|
// Process before `default_missing_values` to avoid it counting as values from the command
|
||||||
// line
|
// line
|
||||||
if source == ValueSource::CommandLine {
|
if source == ValueSource::CommandLine {
|
||||||
self.verify_num_args(arg, &raw_vals)?;
|
ok!(self.verify_num_args(arg, &raw_vals));
|
||||||
}
|
}
|
||||||
|
|
||||||
if raw_vals.is_empty() {
|
if raw_vals.is_empty() {
|
||||||
|
@ -1181,7 +1181,7 @@ impl<'cmd> Parser<'cmd> {
|
||||||
}
|
}
|
||||||
matcher.remove(&arg.id);
|
matcher.remove(&arg.id);
|
||||||
self.start_custom_arg(matcher, arg, source);
|
self.start_custom_arg(matcher, arg, source);
|
||||||
self.push_arg_values(arg, raw_vals, matcher)?;
|
ok!(self.push_arg_values(arg, raw_vals, matcher));
|
||||||
if cfg!(debug_assertions) && matcher.needs_more_vals(arg) {
|
if cfg!(debug_assertions) && matcher.needs_more_vals(arg) {
|
||||||
debug!(
|
debug!(
|
||||||
"Parser::react not enough values passed in, leaving it to the validator to complain",
|
"Parser::react not enough values passed in, leaving it to the validator to complain",
|
||||||
|
@ -1198,7 +1198,7 @@ impl<'cmd> Parser<'cmd> {
|
||||||
debug!("Parser::react: cur_idx:={}", self.cur_idx.get());
|
debug!("Parser::react: cur_idx:={}", self.cur_idx.get());
|
||||||
}
|
}
|
||||||
self.start_custom_arg(matcher, arg, source);
|
self.start_custom_arg(matcher, arg, source);
|
||||||
self.push_arg_values(arg, raw_vals, matcher)?;
|
ok!(self.push_arg_values(arg, raw_vals, matcher));
|
||||||
if cfg!(debug_assertions) && matcher.needs_more_vals(arg) {
|
if cfg!(debug_assertions) && matcher.needs_more_vals(arg) {
|
||||||
debug!(
|
debug!(
|
||||||
"Parser::react not enough values passed in, leaving it to the validator to complain",
|
"Parser::react not enough values passed in, leaving it to the validator to complain",
|
||||||
|
@ -1215,7 +1215,7 @@ impl<'cmd> Parser<'cmd> {
|
||||||
|
|
||||||
matcher.remove(&arg.id);
|
matcher.remove(&arg.id);
|
||||||
self.start_custom_arg(matcher, arg, source);
|
self.start_custom_arg(matcher, arg, source);
|
||||||
self.push_arg_values(arg, raw_vals, matcher)?;
|
ok!(self.push_arg_values(arg, raw_vals, matcher));
|
||||||
Ok(ParseResult::ValuesDone)
|
Ok(ParseResult::ValuesDone)
|
||||||
}
|
}
|
||||||
ArgAction::SetFalse => {
|
ArgAction::SetFalse => {
|
||||||
|
@ -1227,7 +1227,7 @@ impl<'cmd> Parser<'cmd> {
|
||||||
|
|
||||||
matcher.remove(&arg.id);
|
matcher.remove(&arg.id);
|
||||||
self.start_custom_arg(matcher, arg, source);
|
self.start_custom_arg(matcher, arg, source);
|
||||||
self.push_arg_values(arg, raw_vals, matcher)?;
|
ok!(self.push_arg_values(arg, raw_vals, matcher));
|
||||||
Ok(ParseResult::ValuesDone)
|
Ok(ParseResult::ValuesDone)
|
||||||
}
|
}
|
||||||
ArgAction::Count => {
|
ArgAction::Count => {
|
||||||
|
@ -1243,7 +1243,7 @@ impl<'cmd> Parser<'cmd> {
|
||||||
|
|
||||||
matcher.remove(&arg.id);
|
matcher.remove(&arg.id);
|
||||||
self.start_custom_arg(matcher, arg, source);
|
self.start_custom_arg(matcher, arg, source);
|
||||||
self.push_arg_values(arg, raw_vals, matcher)?;
|
ok!(self.push_arg_values(arg, raw_vals, matcher));
|
||||||
Ok(ParseResult::ValuesDone)
|
Ok(ParseResult::ValuesDone)
|
||||||
}
|
}
|
||||||
ArgAction::Help => {
|
ArgAction::Help => {
|
||||||
|
@ -1366,14 +1366,14 @@ impl<'cmd> Parser<'cmd> {
|
||||||
debug!("Parser::add_env: Found an opt with value={:?}", val);
|
debug!("Parser::add_env: Found an opt with value={:?}", val);
|
||||||
let arg_values = vec![val.to_owned()];
|
let arg_values = vec![val.to_owned()];
|
||||||
let trailing_idx = None;
|
let trailing_idx = None;
|
||||||
let _ = self.react(
|
let _ = ok!(self.react(
|
||||||
None,
|
None,
|
||||||
ValueSource::EnvVariable,
|
ValueSource::EnvVariable,
|
||||||
arg,
|
arg,
|
||||||
arg_values,
|
arg_values,
|
||||||
trailing_idx,
|
trailing_idx,
|
||||||
matcher,
|
matcher,
|
||||||
)?;
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1385,7 +1385,7 @@ impl<'cmd> Parser<'cmd> {
|
||||||
|
|
||||||
for arg in self.cmd.get_arguments() {
|
for arg in self.cmd.get_arguments() {
|
||||||
debug!("Parser::add_defaults:iter:{}:", arg.get_id());
|
debug!("Parser::add_defaults:iter:{}:", arg.get_id());
|
||||||
self.add_default_value(arg, matcher)?;
|
ok!(self.add_default_value(arg, matcher));
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -1411,14 +1411,14 @@ impl<'cmd> Parser<'cmd> {
|
||||||
if let Some(default) = default {
|
if let Some(default) = default {
|
||||||
let arg_values = vec![default.to_os_string()];
|
let arg_values = vec![default.to_os_string()];
|
||||||
let trailing_idx = None;
|
let trailing_idx = None;
|
||||||
let _ = self.react(
|
let _ = ok!(self.react(
|
||||||
None,
|
None,
|
||||||
ValueSource::DefaultValue,
|
ValueSource::DefaultValue,
|
||||||
arg,
|
arg,
|
||||||
arg_values,
|
arg_values,
|
||||||
trailing_idx,
|
trailing_idx,
|
||||||
matcher,
|
matcher,
|
||||||
)?;
|
));
|
||||||
}
|
}
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
@ -1447,14 +1447,14 @@ impl<'cmd> Parser<'cmd> {
|
||||||
.map(crate::builder::OsStr::to_os_string)
|
.map(crate::builder::OsStr::to_os_string)
|
||||||
.collect();
|
.collect();
|
||||||
let trailing_idx = None;
|
let trailing_idx = None;
|
||||||
let _ = self.react(
|
let _ = ok!(self.react(
|
||||||
None,
|
None,
|
||||||
ValueSource::DefaultValue,
|
ValueSource::DefaultValue,
|
||||||
arg,
|
arg,
|
||||||
arg_values,
|
arg_values,
|
||||||
trailing_idx,
|
trailing_idx,
|
||||||
matcher,
|
matcher,
|
||||||
)?;
|
));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
debug!(
|
debug!(
|
||||||
|
|
|
@ -76,9 +76,9 @@ impl<'cmd> Validator<'cmd> {
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
self.validate_conflicts(matcher, &mut conflicts)?;
|
ok!(self.validate_conflicts(matcher, &mut conflicts));
|
||||||
if !(self.cmd.is_subcommand_negates_reqs_set() && has_subcmd) {
|
if !(self.cmd.is_subcommand_negates_reqs_set() && has_subcmd) {
|
||||||
self.validate_required(matcher, &mut conflicts)?;
|
ok!(self.validate_required(matcher, &mut conflicts));
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -91,7 +91,7 @@ impl<'cmd> Validator<'cmd> {
|
||||||
) -> ClapResult<()> {
|
) -> ClapResult<()> {
|
||||||
debug!("Validator::validate_conflicts");
|
debug!("Validator::validate_conflicts");
|
||||||
|
|
||||||
self.validate_exclusive(matcher)?;
|
ok!(self.validate_exclusive(matcher));
|
||||||
|
|
||||||
for arg_id in matcher
|
for arg_id in matcher
|
||||||
.arg_ids()
|
.arg_ids()
|
||||||
|
@ -100,7 +100,7 @@ impl<'cmd> Validator<'cmd> {
|
||||||
{
|
{
|
||||||
debug!("Validator::validate_conflicts::iter: id={:?}", arg_id);
|
debug!("Validator::validate_conflicts::iter: id={:?}", arg_id);
|
||||||
let conflicts = conflicts.gather_conflicts(self.cmd, matcher, arg_id);
|
let conflicts = conflicts.gather_conflicts(self.cmd, matcher, arg_id);
|
||||||
self.build_conflict_err(arg_id, &conflicts, matcher)?;
|
ok!(self.build_conflict_err(arg_id, &conflicts, matcher));
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -361,7 +361,7 @@ impl<'cmd> Validator<'cmd> {
|
||||||
}
|
}
|
||||||
|
|
||||||
if !missing_required.is_empty() {
|
if !missing_required.is_empty() {
|
||||||
self.missing_required_error(matcher, missing_required)?;
|
ok!(self.missing_required_error(matcher, missing_required));
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
|
@ -65,11 +65,11 @@ impl<K: PartialEq + Eq, V> FlatMap<K, V> {
|
||||||
K: Borrow<Q>,
|
K: Borrow<Q>,
|
||||||
Q: std::hash::Hash + Eq,
|
Q: std::hash::Hash + Eq,
|
||||||
{
|
{
|
||||||
let index = self
|
let index = some!(self
|
||||||
.keys
|
.keys
|
||||||
.iter()
|
.iter()
|
||||||
.enumerate()
|
.enumerate()
|
||||||
.find_map(|(i, k)| (k.borrow() == key).then(|| i))?;
|
.find_map(|(i, k)| (k.borrow() == key).then(|| i)));
|
||||||
let key = self.keys.remove(index);
|
let key = self.keys.remove(index);
|
||||||
let value = self.values.remove(index);
|
let value = self.values.remove(index);
|
||||||
Some((key, value))
|
Some((key, value))
|
||||||
|
|
Loading…
Add table
Reference in a new issue