mirror of
https://github.com/clap-rs/clap
synced 2024-12-13 14:22:34 +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 usage = Usage::new(self);
|
||||
write_help(&mut styled, self, &usage, false);
|
||||
write!(w, "{}", styled)?;
|
||||
ok!(write!(w, "{}", styled));
|
||||
w.flush()
|
||||
}
|
||||
|
||||
|
@ -837,7 +837,7 @@ impl Command {
|
|||
let mut styled = StyledStr::new();
|
||||
let usage = Usage::new(self);
|
||||
write_help(&mut styled, self, &usage, true);
|
||||
write!(w, "{}", styled)?;
|
||||
ok!(write!(w, "{}", styled));
|
||||
w.flush()
|
||||
}
|
||||
|
||||
|
@ -3896,7 +3896,7 @@ impl Command {
|
|||
}
|
||||
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
|
||||
let mut sc_names = String::new();
|
||||
|
|
|
@ -172,10 +172,10 @@ impl From<std::ops::RangeToInclusive<usize>> for ValueRange {
|
|||
|
||||
impl std::fmt::Display for ValueRange {
|
||||
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() {
|
||||
"..=".fmt(f)?;
|
||||
self.end_inclusive.fmt(f)?;
|
||||
ok!("..=".fmt(f));
|
||||
ok!(self.end_inclusive.fmt(f));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -223,9 +223,9 @@ impl StyledStr {
|
|||
None => {}
|
||||
}
|
||||
|
||||
buffer.set_color(&color)?;
|
||||
buffer.write_all(content.as_bytes())?;
|
||||
buffer.reset()?;
|
||||
ok!(buffer.set_color(&color));
|
||||
ok!(buffer.write_all(content.as_bytes()));
|
||||
ok!(buffer.reset());
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
@ -291,7 +291,7 @@ fn cmp_key(c: (Option<Style>, &str)) -> (Option<usize>, &str) {
|
|||
impl std::fmt::Display for StyledStr {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
for (_, content) in self.iter() {
|
||||
std::fmt::Display::fmt(content, f)?;
|
||||
ok!(std::fmt::Display::fmt(content, f));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
@ -307,12 +307,13 @@ struct AnsiDisplay<'s> {
|
|||
impl std::fmt::Display for AnsiDisplay<'_> {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
let mut buffer = termcolor::Buffer::ansi();
|
||||
self.styled
|
||||
ok!(self
|
||||
.styled
|
||||
.write_colored(&mut buffer)
|
||||
.map_err(|_| std::fmt::Error)?;
|
||||
.map_err(|_| std::fmt::Error));
|
||||
let buffer = buffer.into_inner();
|
||||
let buffer = String::from_utf8(buffer).map_err(|_| std::fmt::Error)?;
|
||||
std::fmt::Display::fmt(&buffer, f)?;
|
||||
let buffer = ok!(String::from_utf8(buffer).map_err(|_| std::fmt::Error));
|
||||
ok!(std::fmt::Display::fmt(&buffer, f));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -569,7 +569,7 @@ where
|
|||
arg: Option<&crate::Arg>,
|
||||
value: &std::ffi::OsStr,
|
||||
) -> 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))
|
||||
}
|
||||
|
||||
|
@ -579,7 +579,7 @@ where
|
|||
arg: Option<&crate::Arg>,
|
||||
value: std::ffi::OsString,
|
||||
) -> 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))
|
||||
}
|
||||
|
||||
|
@ -694,18 +694,18 @@ where
|
|||
arg: Option<&crate::Arg>,
|
||||
value: &std::ffi::OsStr,
|
||||
) -> 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(
|
||||
cmd,
|
||||
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
|
||||
.map(|a| a.to_string())
|
||||
.unwrap_or_else(|| "...".to_owned());
|
||||
crate::Error::value_validation(arg, value.to_owned(), e.into()).with_cmd(cmd)
|
||||
})?;
|
||||
}));
|
||||
Ok(value)
|
||||
}
|
||||
}
|
||||
|
@ -742,12 +742,12 @@ impl TypedValueParser for StringValueParser {
|
|||
_arg: Option<&crate::Arg>,
|
||||
value: std::ffi::OsString,
|
||||
) -> Result<Self::Value, crate::Error> {
|
||||
let value = value.into_string().map_err(|_| {
|
||||
let value = ok!(value.into_string().map_err(|_| {
|
||||
crate::Error::invalid_utf8(
|
||||
cmd,
|
||||
crate::output::Usage::new(cmd).create_usage_with_title(&[]),
|
||||
)
|
||||
})?;
|
||||
}));
|
||||
Ok(value)
|
||||
}
|
||||
}
|
||||
|
@ -939,7 +939,7 @@ impl<E: crate::ValueEnum + Clone + Send + Sync + 'static> TypedValueParser for E
|
|||
.collect::<Vec<_>>()
|
||||
};
|
||||
|
||||
let value = value.to_str().ok_or_else(|| {
|
||||
let value = ok!(value.to_str().ok_or_else(|| {
|
||||
crate::Error::invalid_value(
|
||||
cmd,
|
||||
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)
|
||||
.unwrap_or_else(|| "...".to_owned()),
|
||||
)
|
||||
})?;
|
||||
let value = E::value_variants()
|
||||
}));
|
||||
let value = ok!(E::value_variants()
|
||||
.iter()
|
||||
.find(|v| {
|
||||
v.to_possible_value()
|
||||
|
@ -963,7 +963,7 @@ impl<E: crate::ValueEnum + Clone + Send + Sync + 'static> TypedValueParser for E
|
|||
arg.map(ToString::to_string)
|
||||
.unwrap_or_else(|| "...".to_owned()),
|
||||
)
|
||||
})?
|
||||
}))
|
||||
.clone();
|
||||
Ok(value)
|
||||
}
|
||||
|
@ -1049,12 +1049,12 @@ impl TypedValueParser for PossibleValuesParser {
|
|||
arg: Option<&crate::Arg>,
|
||||
value: std::ffi::OsString,
|
||||
) -> Result<String, crate::Error> {
|
||||
let value = value.into_string().map_err(|_| {
|
||||
let value = ok!(value.into_string().map_err(|_| {
|
||||
crate::Error::invalid_utf8(
|
||||
cmd,
|
||||
crate::output::Usage::new(cmd).create_usage_with_title(&[]),
|
||||
)
|
||||
})?;
|
||||
}));
|
||||
|
||||
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)) {
|
||||
|
@ -1233,13 +1233,13 @@ where
|
|||
arg: Option<&crate::Arg>,
|
||||
raw_value: &std::ffi::OsStr,
|
||||
) -> 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(
|
||||
cmd,
|
||||
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
|
||||
.map(|a| a.to_string())
|
||||
.unwrap_or_else(|| "...".to_owned());
|
||||
|
@ -1249,7 +1249,7 @@ where
|
|||
err.into(),
|
||||
)
|
||||
.with_cmd(cmd)
|
||||
})?;
|
||||
}));
|
||||
if !self.bounds.contains(&value) {
|
||||
let arg = arg
|
||||
.map(|a| a.to_string())
|
||||
|
@ -1263,7 +1263,7 @@ where
|
|||
}
|
||||
|
||||
let value: Result<Self::Value, _> = value.try_into();
|
||||
let value = value.map_err(|err| {
|
||||
let value = ok!(value.map_err(|err| {
|
||||
let arg = arg
|
||||
.map(|a| a.to_string())
|
||||
.unwrap_or_else(|| "...".to_owned());
|
||||
|
@ -1273,7 +1273,7 @@ where
|
|||
err.into(),
|
||||
)
|
||||
.with_cmd(cmd)
|
||||
})?;
|
||||
}));
|
||||
|
||||
Ok(value)
|
||||
}
|
||||
|
@ -1431,13 +1431,13 @@ where
|
|||
arg: Option<&crate::Arg>,
|
||||
raw_value: &std::ffi::OsStr,
|
||||
) -> 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(
|
||||
cmd,
|
||||
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
|
||||
.map(|a| a.to_string())
|
||||
.unwrap_or_else(|| "...".to_owned());
|
||||
|
@ -1447,7 +1447,7 @@ where
|
|||
err.into(),
|
||||
)
|
||||
.with_cmd(cmd)
|
||||
})?;
|
||||
}));
|
||||
if !self.bounds.contains(&value) {
|
||||
let arg = arg
|
||||
.map(|a| a.to_string())
|
||||
|
@ -1461,7 +1461,7 @@ where
|
|||
}
|
||||
|
||||
let value: Result<Self::Value, _> = value.try_into();
|
||||
let value = value.map_err(|err| {
|
||||
let value = ok!(value.map_err(|err| {
|
||||
let arg = arg
|
||||
.map(|a| a.to_string())
|
||||
.unwrap_or_else(|| "...".to_owned());
|
||||
|
@ -1471,7 +1471,7 @@ where
|
|||
err.into(),
|
||||
)
|
||||
.with_cmd(cmd)
|
||||
})?;
|
||||
}));
|
||||
|
||||
Ok(value)
|
||||
}
|
||||
|
@ -1622,12 +1622,12 @@ impl TypedValueParser for FalseyValueParser {
|
|||
_arg: Option<&crate::Arg>,
|
||||
value: &std::ffi::OsStr,
|
||||
) -> 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(
|
||||
cmd,
|
||||
crate::output::Usage::new(cmd).create_usage_with_title(&[]),
|
||||
)
|
||||
})?;
|
||||
}));
|
||||
let value = if value.is_empty() {
|
||||
false
|
||||
} else {
|
||||
|
@ -1719,19 +1719,19 @@ impl TypedValueParser for BoolishValueParser {
|
|||
arg: Option<&crate::Arg>,
|
||||
value: &std::ffi::OsStr,
|
||||
) -> 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(
|
||||
cmd,
|
||||
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
|
||||
.map(|a| a.to_string())
|
||||
.unwrap_or_else(|| "...".to_owned());
|
||||
crate::Error::value_validation(arg, value.to_owned(), "value was not a boolean".into())
|
||||
.with_cmd(cmd)
|
||||
})?;
|
||||
}));
|
||||
Ok(value)
|
||||
}
|
||||
|
||||
|
@ -1808,12 +1808,12 @@ impl TypedValueParser for NonEmptyStringValueParser {
|
|||
.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(
|
||||
cmd,
|
||||
crate::output::Usage::new(cmd).create_usage_with_title(&[]),
|
||||
)
|
||||
})?;
|
||||
}));
|
||||
Ok(value.to_owned())
|
||||
}
|
||||
}
|
||||
|
@ -1860,7 +1860,7 @@ where
|
|||
arg: Option<&crate::Arg>,
|
||||
value: &std::ffi::OsStr,
|
||||
) -> 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);
|
||||
Ok(value)
|
||||
}
|
||||
|
@ -1871,7 +1871,7 @@ where
|
|||
arg: Option<&crate::Arg>,
|
||||
value: std::ffi::OsString,
|
||||
) -> 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);
|
||||
Ok(value)
|
||||
}
|
||||
|
|
|
@ -94,7 +94,7 @@ pub trait Parser: FromArgMatches + CommandFactory + Sized {
|
|||
|
||||
/// Parse from `std::env::args_os()`, return Err on 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>)
|
||||
}
|
||||
|
||||
|
@ -123,7 +123,7 @@ pub trait Parser: FromArgMatches + CommandFactory + Sized {
|
|||
I: IntoIterator<Item = T>,
|
||||
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>)
|
||||
}
|
||||
|
||||
|
@ -150,7 +150,7 @@ pub trait Parser: FromArgMatches + CommandFactory + Sized {
|
|||
T: Into<OsString> + Clone,
|
||||
{
|
||||
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)
|
||||
.map_err(format_error::<Self>)
|
||||
}
|
||||
|
|
|
@ -694,11 +694,11 @@ impl<F: ErrorFormatter> error::Error for Error<F> {
|
|||
impl<F: ErrorFormatter> Display for Error<F> {
|
||||
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
||||
// 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() {
|
||||
writeln!(f)?;
|
||||
writeln!(f, "Backtrace:")?;
|
||||
writeln!(f, "{}", backtrace)?;
|
||||
ok!(writeln!(f));
|
||||
ok!(writeln!(f, "Backtrace:"));
|
||||
ok!(writeln!(f, "{}", backtrace));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -661,3 +661,15 @@ macro_rules! debug {
|
|||
macro_rules! debug {
|
||||
($($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();
|
||||
self.content.write_colored(&mut buffer)?;
|
||||
ok!(self.content.write_colored(&mut buffer));
|
||||
writer.print(&buffer)
|
||||
}
|
||||
|
||||
|
|
|
@ -987,7 +987,9 @@ pub(crate) fn dimensions() -> (Option<usize>, Option<usize>) {
|
|||
|
||||
#[cfg(feature = "wrap_help")]
|
||||
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 {
|
||||
|
|
|
@ -35,7 +35,7 @@ impl<'cmd> Usage<'cmd> {
|
|||
// 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> {
|
||||
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();
|
||||
styled.header("Usage:");
|
||||
|
|
|
@ -63,8 +63,8 @@ where
|
|||
|
||||
let subcommand_name = subcommand.get_name();
|
||||
|
||||
let candidate = did_you_mean(arg, longs).pop()?;
|
||||
let score = remaining_args.iter().position(|x| subcommand_name == *x)?;
|
||||
let candidate = some!(did_you_mean(arg, longs).pop());
|
||||
let score = some!(remaining_args.iter().position(|x| subcommand_name == *x));
|
||||
Some((score, (candidate, Some(subcommand_name.to_string()))))
|
||||
})
|
||||
.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> {
|
||||
let id = self.id;
|
||||
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());
|
||||
Ok(value)
|
||||
}
|
||||
|
|
|
@ -454,7 +454,7 @@ impl ArgMatches {
|
|||
#[cfg(feature = "unstable-grouped")]
|
||||
#[cfg_attr(debug_assertions, track_caller)]
|
||||
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 {
|
||||
iter: arg.vals().map(|g| g.iter().map(unwrap_string).collect()),
|
||||
len: arg.vals().len(),
|
||||
|
@ -636,8 +636,8 @@ impl ArgMatches {
|
|||
/// [delimiter]: crate::Arg::value_delimiter()
|
||||
#[cfg_attr(debug_assertions, track_caller)]
|
||||
pub fn index_of(&self, id: &str) -> Option<usize> {
|
||||
let arg = self.get_arg(id)?;
|
||||
let i = arg.get_index(0)?;
|
||||
let arg = some!(self.get_arg(id));
|
||||
let i = some!(arg.get_index(0));
|
||||
Some(i)
|
||||
}
|
||||
|
||||
|
@ -718,7 +718,7 @@ impl ArgMatches {
|
|||
/// [delimiter]: Arg::value_delimiter()
|
||||
#[cfg_attr(debug_assertions, track_caller)]
|
||||
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 {
|
||||
iter: arg.indices(),
|
||||
len: arg.num_vals(),
|
||||
|
@ -941,7 +941,7 @@ impl ArgMatches {
|
|||
&self,
|
||||
id: &str,
|
||||
) -> 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()) {
|
||||
Some(value) => value,
|
||||
None => {
|
||||
|
@ -959,7 +959,7 @@ impl ArgMatches {
|
|||
&self,
|
||||
id: &str,
|
||||
) -> 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,
|
||||
None => return Ok(None),
|
||||
};
|
||||
|
@ -975,7 +975,7 @@ impl ArgMatches {
|
|||
|
||||
/// Non-panicking version of [`ArgMatches::get_raw`]
|
||||
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,
|
||||
None => return Ok(None),
|
||||
};
|
||||
|
@ -993,7 +993,7 @@ impl ArgMatches {
|
|||
&mut self,
|
||||
id: &str,
|
||||
) -> 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
|
||||
.into_vals_flatten()
|
||||
// enforced by `try_get_arg_t`
|
||||
|
@ -1008,7 +1008,7 @@ impl ArgMatches {
|
|||
&mut self,
|
||||
id: &str,
|
||||
) -> 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,
|
||||
None => return Ok(None),
|
||||
};
|
||||
|
@ -1024,7 +1024,7 @@ impl ArgMatches {
|
|||
|
||||
/// Non-panicking version of [`ArgMatches::contains_id`]
|
||||
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);
|
||||
Ok(presence)
|
||||
|
@ -1035,7 +1035,7 @@ impl ArgMatches {
|
|||
impl ArgMatches {
|
||||
#[inline]
|
||||
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))
|
||||
}
|
||||
|
||||
|
@ -1044,13 +1044,13 @@ impl ArgMatches {
|
|||
&self,
|
||||
arg: &str,
|
||||
) -> Result<Option<&MatchedArg>, MatchesError> {
|
||||
let arg = match self.try_get_arg(arg)? {
|
||||
let arg = match ok!(self.try_get_arg(arg)) {
|
||||
Some(arg) => arg,
|
||||
None => {
|
||||
return Ok(None);
|
||||
}
|
||||
};
|
||||
self.verify_arg_t::<T>(arg)?;
|
||||
ok!(self.verify_arg_t::<T>(arg));
|
||||
Ok(Some(arg))
|
||||
}
|
||||
|
||||
|
@ -1059,7 +1059,7 @@ impl ArgMatches {
|
|||
&mut self,
|
||||
arg: &str,
|
||||
) -> Result<Option<MatchedArg>, MatchesError> {
|
||||
self.verify_arg(arg)?;
|
||||
ok!(self.verify_arg(arg));
|
||||
let (id, matched) = match self.args.remove_entry(arg) {
|
||||
Some((id, matched)) => (id, matched),
|
||||
None => {
|
||||
|
|
|
@ -108,7 +108,7 @@ impl<'cmd> Parser<'cmd> {
|
|||
debug!("Parser::get_matches_with: sc={:?}", sc_name);
|
||||
if let Some(sc_name) = sc_name {
|
||||
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");
|
||||
} else {
|
||||
subcmd_name = Some(sc_name.to_owned());
|
||||
|
@ -129,14 +129,14 @@ impl<'cmd> Parser<'cmd> {
|
|||
continue;
|
||||
}
|
||||
} 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,
|
||||
long_arg,
|
||||
long_value,
|
||||
&parse_state,
|
||||
pos_counter,
|
||||
&mut valid_arg_found,
|
||||
)?;
|
||||
));
|
||||
debug!(
|
||||
"Parser::get_matches_with: After parse_long_arg {:?}",
|
||||
parse_result
|
||||
|
@ -199,13 +199,13 @@ impl<'cmd> Parser<'cmd> {
|
|||
// Try to parse short args like normal, if allow_hyphen_values or
|
||||
// AllowNegativeNumbers is set, parse_short_arg will *not* throw
|
||||
// an error, and instead return Ok(None)
|
||||
let parse_result = self.parse_short_arg(
|
||||
let parse_result = ok!(self.parse_short_arg(
|
||||
matcher,
|
||||
short_arg,
|
||||
&parse_state,
|
||||
pos_counter,
|
||||
&mut valid_arg_found,
|
||||
)?;
|
||||
));
|
||||
// If it's None, we then check if one of those two AppSettings was set
|
||||
debug!(
|
||||
"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() {
|
||||
self.resolve_pending(matcher)?;
|
||||
ok!(self.resolve_pending(matcher));
|
||||
}
|
||||
if let Some(_parse_result) = self.check_terminator(arg, arg_os.to_value_os()) {
|
||||
debug!(
|
||||
|
@ -432,7 +432,7 @@ impl<'cmd> Parser<'cmd> {
|
|||
sc_m.start_occurrence_of_external(self.cmd);
|
||||
|
||||
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);
|
||||
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(),
|
||||
});
|
||||
|
||||
self.resolve_pending(matcher)?;
|
||||
ok!(self.resolve_pending(matcher));
|
||||
#[cfg(feature = "env")]
|
||||
self.add_env(matcher)?;
|
||||
self.add_defaults(matcher)?;
|
||||
ok!(self.add_env(matcher));
|
||||
ok!(self.add_defaults(matcher));
|
||||
return Validator::new(self.cmd).validate(parse_state, matcher);
|
||||
} else {
|
||||
// Start error processing
|
||||
|
@ -461,13 +461,13 @@ impl<'cmd> Parser<'cmd> {
|
|||
.expect(INTERNAL_ERROR_MSG)
|
||||
.get_name()
|
||||
.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")]
|
||||
self.add_env(matcher)?;
|
||||
self.add_defaults(matcher)?;
|
||||
ok!(self.add_env(matcher));
|
||||
ok!(self.add_defaults(matcher));
|
||||
Validator::new(self.cmd).validate(parse_state, matcher)
|
||||
}
|
||||
|
||||
|
@ -540,7 +540,7 @@ impl<'cmd> Parser<'cmd> {
|
|||
valid_arg_found: bool,
|
||||
) -> Option<&str> {
|
||||
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_infer_subcommands_set() {
|
||||
|
@ -910,14 +910,14 @@ impl<'cmd> Parser<'cmd> {
|
|||
if !arg.is_takes_value_set() {
|
||||
let arg_values = Vec::new();
|
||||
let trailing_idx = None;
|
||||
ret = self.react(
|
||||
ret = ok!(self.react(
|
||||
Some(ident),
|
||||
ValueSource::CommandLine,
|
||||
arg,
|
||||
arg_values,
|
||||
trailing_idx,
|
||||
matcher,
|
||||
)?;
|
||||
));
|
||||
continue;
|
||||
}
|
||||
|
||||
|
@ -943,7 +943,7 @@ impl<'cmd> Parser<'cmd> {
|
|||
} else {
|
||||
(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,
|
||||
x => return Ok(x),
|
||||
}
|
||||
|
@ -952,7 +952,7 @@ impl<'cmd> Parser<'cmd> {
|
|||
return if let Some(sc_name) = self.cmd.find_short_subcmd(c) {
|
||||
debug!("Parser::parse_short_arg:iter:{}: subcommand={}", c, sc_name);
|
||||
// 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);
|
||||
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");
|
||||
let arg_values = Vec::new();
|
||||
let trailing_idx = None;
|
||||
let react_result = self.react(
|
||||
let react_result = ok!(self.react(
|
||||
Some(ident),
|
||||
ValueSource::CommandLine,
|
||||
arg,
|
||||
arg_values,
|
||||
trailing_idx,
|
||||
matcher,
|
||||
)?;
|
||||
));
|
||||
debug_assert_eq!(react_result, ParseResult::ValuesDone);
|
||||
if attached_value.is_some() {
|
||||
Ok(ParseResult::AttachedValueNotConsumed)
|
||||
|
@ -1022,20 +1022,20 @@ impl<'cmd> Parser<'cmd> {
|
|||
} else if let Some(v) = attached_value {
|
||||
let arg_values = vec![v.to_os_str().into_owned()];
|
||||
let trailing_idx = None;
|
||||
let react_result = self.react(
|
||||
let react_result = ok!(self.react(
|
||||
Some(ident),
|
||||
ValueSource::CommandLine,
|
||||
arg,
|
||||
arg_values,
|
||||
trailing_idx,
|
||||
matcher,
|
||||
)?;
|
||||
));
|
||||
debug_assert_eq!(react_result, ParseResult::ValuesDone);
|
||||
// Attached are always done
|
||||
Ok(ParseResult::ValuesDone)
|
||||
} else {
|
||||
debug!("Parser::parse_opt_value: More arg vals required...");
|
||||
self.resolve_pending(matcher)?;
|
||||
ok!(self.resolve_pending(matcher));
|
||||
let trailing_values = false;
|
||||
matcher.pending_values_mut(&arg.id, Some(ident), trailing_values);
|
||||
Ok(ParseResult::Opt(arg.id.clone()))
|
||||
|
@ -1072,7 +1072,7 @@ impl<'cmd> Parser<'cmd> {
|
|||
self.cur_idx.get()
|
||||
);
|
||||
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_index_to(&arg.id, self.cur_idx.get());
|
||||
|
@ -1100,14 +1100,14 @@ impl<'cmd> Parser<'cmd> {
|
|||
|
||||
debug!("Parser::resolve_pending: id={:?}", pending.id);
|
||||
let arg = self.cmd.find(&pending.id).expect(INTERNAL_ERROR_MSG);
|
||||
let _ = self.react(
|
||||
let _ = ok!(self.react(
|
||||
pending.ident,
|
||||
ValueSource::CommandLine,
|
||||
arg,
|
||||
pending.raw_vals,
|
||||
pending.trailing_idx,
|
||||
matcher,
|
||||
)?;
|
||||
));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
@ -1121,7 +1121,7 @@ impl<'cmd> Parser<'cmd> {
|
|||
mut trailing_idx: Option<usize>,
|
||||
matcher: &mut ArgMatcher,
|
||||
) -> ClapResult<ParseResult> {
|
||||
self.resolve_pending(matcher)?;
|
||||
ok!(self.resolve_pending(matcher));
|
||||
|
||||
debug!(
|
||||
"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
|
||||
// line
|
||||
if source == ValueSource::CommandLine {
|
||||
self.verify_num_args(arg, &raw_vals)?;
|
||||
ok!(self.verify_num_args(arg, &raw_vals));
|
||||
}
|
||||
|
||||
if raw_vals.is_empty() {
|
||||
|
@ -1181,7 +1181,7 @@ impl<'cmd> Parser<'cmd> {
|
|||
}
|
||||
matcher.remove(&arg.id);
|
||||
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) {
|
||||
debug!(
|
||||
"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());
|
||||
}
|
||||
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) {
|
||||
debug!(
|
||||
"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);
|
||||
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)
|
||||
}
|
||||
ArgAction::SetFalse => {
|
||||
|
@ -1227,7 +1227,7 @@ impl<'cmd> Parser<'cmd> {
|
|||
|
||||
matcher.remove(&arg.id);
|
||||
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)
|
||||
}
|
||||
ArgAction::Count => {
|
||||
|
@ -1243,7 +1243,7 @@ impl<'cmd> Parser<'cmd> {
|
|||
|
||||
matcher.remove(&arg.id);
|
||||
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)
|
||||
}
|
||||
ArgAction::Help => {
|
||||
|
@ -1366,14 +1366,14 @@ impl<'cmd> Parser<'cmd> {
|
|||
debug!("Parser::add_env: Found an opt with value={:?}", val);
|
||||
let arg_values = vec![val.to_owned()];
|
||||
let trailing_idx = None;
|
||||
let _ = self.react(
|
||||
let _ = ok!(self.react(
|
||||
None,
|
||||
ValueSource::EnvVariable,
|
||||
arg,
|
||||
arg_values,
|
||||
trailing_idx,
|
||||
matcher,
|
||||
)?;
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1385,7 +1385,7 @@ impl<'cmd> Parser<'cmd> {
|
|||
|
||||
for arg in self.cmd.get_arguments() {
|
||||
debug!("Parser::add_defaults:iter:{}:", arg.get_id());
|
||||
self.add_default_value(arg, matcher)?;
|
||||
ok!(self.add_default_value(arg, matcher));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
@ -1411,14 +1411,14 @@ impl<'cmd> Parser<'cmd> {
|
|||
if let Some(default) = default {
|
||||
let arg_values = vec![default.to_os_string()];
|
||||
let trailing_idx = None;
|
||||
let _ = self.react(
|
||||
let _ = ok!(self.react(
|
||||
None,
|
||||
ValueSource::DefaultValue,
|
||||
arg,
|
||||
arg_values,
|
||||
trailing_idx,
|
||||
matcher,
|
||||
)?;
|
||||
));
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
|
@ -1447,14 +1447,14 @@ impl<'cmd> Parser<'cmd> {
|
|||
.map(crate::builder::OsStr::to_os_string)
|
||||
.collect();
|
||||
let trailing_idx = None;
|
||||
let _ = self.react(
|
||||
let _ = ok!(self.react(
|
||||
None,
|
||||
ValueSource::DefaultValue,
|
||||
arg,
|
||||
arg_values,
|
||||
trailing_idx,
|
||||
matcher,
|
||||
)?;
|
||||
));
|
||||
}
|
||||
} else {
|
||||
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) {
|
||||
self.validate_required(matcher, &mut conflicts)?;
|
||||
ok!(self.validate_required(matcher, &mut conflicts));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
@ -91,7 +91,7 @@ impl<'cmd> Validator<'cmd> {
|
|||
) -> ClapResult<()> {
|
||||
debug!("Validator::validate_conflicts");
|
||||
|
||||
self.validate_exclusive(matcher)?;
|
||||
ok!(self.validate_exclusive(matcher));
|
||||
|
||||
for arg_id in matcher
|
||||
.arg_ids()
|
||||
|
@ -100,7 +100,7 @@ impl<'cmd> Validator<'cmd> {
|
|||
{
|
||||
debug!("Validator::validate_conflicts::iter: id={:?}", 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(())
|
||||
|
@ -361,7 +361,7 @@ impl<'cmd> Validator<'cmd> {
|
|||
}
|
||||
|
||||
if !missing_required.is_empty() {
|
||||
self.missing_required_error(matcher, missing_required)?;
|
||||
ok!(self.missing_required_error(matcher, missing_required));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
|
|
@ -65,11 +65,11 @@ impl<K: PartialEq + Eq, V> FlatMap<K, V> {
|
|||
K: Borrow<Q>,
|
||||
Q: std::hash::Hash + Eq,
|
||||
{
|
||||
let index = self
|
||||
let index = some!(self
|
||||
.keys
|
||||
.iter()
|
||||
.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 value = self.values.remove(index);
|
||||
Some((key, value))
|
||||
|
|
Loading…
Reference in a new issue