Skip to content

Commit

Permalink
convert Result<(), OsString> -> Result<(), String>
Browse files Browse the repository at this point in the history
  • Loading branch information
little-dude committed Apr 21, 2018
1 parent 9981bac commit 5ebd15d
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 52 deletions.
2 changes: 1 addition & 1 deletion src/app/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ impl<'a, 'b, 'c, 'z> Validator<'a, 'b, 'c, 'z> {
sdebugln!("error");
return Err(Error::value_validation(
Some(arg),
(*e).to_string_lossy().to_string(),
(*e).to_string(),
self.0.app.color(),
));
} else {
Expand Down
106 changes: 55 additions & 51 deletions src/args/arg.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
#[cfg(any(target_os = "windows", target_arch = "wasm32"))]
use osstringext::OsStrExt3;
use std::borrow::Cow;
use std::cmp::{Ord, Ordering};
#[cfg(feature = "yaml")]
use std::collections::BTreeMap;
use std::rc::Rc;
use std::borrow::Cow;
use std::fmt::{self, Display, Formatter};
use std::env;
use std::ffi::{OsStr, OsString};
#[cfg(any(target_os = "windows", target_arch = "wasm32"))]
use osstringext::OsStrExt3;
use std::fmt::{self, Display, Formatter};
#[cfg(not(any(target_os = "windows", target_arch = "wasm32")))]
use std::os::unix::ffi::OsStrExt;
use std::env;
use std::cmp::{Ord, Ordering};
use std::rc::Rc;
use std::str;

use map::VecMap;
#[cfg(feature = "yaml")]
use yaml_rust::Yaml;
use map::VecMap;

use usage_parser::UsageParser;
use args::settings::{ArgFlags, ArgSettings};
use usage_parser::UsageParser;
use INTERNAL_ERROR_MSG;

/// The abstract representation of a command line argument. Used to set all the options and
Expand Down Expand Up @@ -89,7 +89,7 @@ where
#[doc(hidden)]
pub validator: Option<Rc<Fn(String) -> Result<(), String>>>,
#[doc(hidden)]
pub validator_os: Option<Rc<Fn(&OsStr) -> Result<(), OsString>>>,
pub validator_os: Option<Rc<Fn(&OsStr) -> Result<(), String>>>,
#[doc(hidden)]
pub val_delim: Option<char>,
#[doc(hidden)]
Expand Down Expand Up @@ -1497,7 +1497,6 @@ impl<'a, 'b> Arg<'a, 'b> {
self
}


/// Specifies a value that *stops* parsing multiple values of a give argument. By default when
/// one sets [`multiple(true)`] on an argument, clap will continue parsing values for that
/// argument until it reaches another valid argument, or one of the other more specific settings
Expand Down Expand Up @@ -1831,10 +1830,13 @@ impl<'a, 'b> Arg<'a, 'b> {
/// [`Err(String)`]: https://doc.rust-lang.org/std/result/enum.Result.html#variant.Err
/// [`Rc`]: https://doc.rust-lang.org/std/rc/struct.Rc.html
pub fn validator<F, O, E>(mut self, f: F) -> Self
where F: Fn(String) -> Result<O, E> + 'static,
E: ToString
where
F: Fn(String) -> Result<O, E> + 'static,
E: ToString,
{
self.validator = Some(Rc::new(move |s| f(s).map(|_| ()).map_err(|e| e.to_string())));
self.validator = Some(Rc::new(move |s| {
f(s).map(|_| ()).map_err(|e| e.to_string())
}));
self
}

Expand All @@ -1848,9 +1850,9 @@ impl<'a, 'b> Arg<'a, 'b> {
/// # use clap::{App, Arg};
/// # use std::ffi::{OsStr, OsString};
/// # use std::os::unix::ffi::OsStrExt;
/// fn has_ampersand(v: &OsStr) -> Result<(), OsString> {
/// fn has_ampersand(v: &OsStr) -> Result<(), String> {
/// if v.as_bytes().iter().any(|b| *b == b'&') { return Ok(()); }
/// Err(OsString::from("The value did not contain the required & sigil"))
/// Err(String::from("The value did not contain the required & sigil"))
/// }
/// let res = App::new("prog")
/// .arg(Arg::with_name("file")
Expand All @@ -1869,7 +1871,8 @@ impl<'a, 'b> Arg<'a, 'b> {
/// [`Err(String)`]: https://doc.rust-lang.org/std/result/enum.Result.html#variant.Err
/// [`Rc`]: https://doc.rust-lang.org/std/rc/struct.Rc.html
pub fn validator_os<F, O>(mut self, f: F) -> Self
where F: Fn(&OsStr) -> Result<O, OsString> + 'static
where
F: Fn(&OsStr) -> Result<O, String> + 'static,
{
self.validator_os = Some(Rc::new(move |s| f(s).map(|_| ())));
self
Expand Down Expand Up @@ -3175,7 +3178,10 @@ impl<'a, 'b> Arg<'a, 'b> {
}

/// **Deprecated**
#[deprecated(since="2.30.0", note="Use `Arg::setting(ArgSettings::AllowEmptyValues)` instead. Will be removed in v3.0-beta")]
#[deprecated(
since = "2.30.0",
note = "Use `Arg::setting(ArgSettings::AllowEmptyValues)` instead. Will be removed in v3.0-beta"
)]
pub fn empty_values(mut self, ev: bool) -> Self {
if ev {
self.setting(ArgSettings::AllowEmptyValues)
Expand Down Expand Up @@ -3747,7 +3753,7 @@ impl<'a, 'b> Arg<'a, 'b> {
self.unset_setting(ArgSettings::MultipleOccurrences)
}
}

/// Indicates that all parameters passed after this should not be parsed
/// individually, but rather passed in their entirety. It is worth noting
/// that setting this requires all values to come after a `--` to indicate they
Expand All @@ -3767,17 +3773,15 @@ impl<'a, 'b> Arg<'a, 'b> {
/// [`Arg::allow_hyphen_values(true)`]: ./struct.Arg.html#method.allow_hyphen_values
/// [`Arg::last(true)`]: ./struct.Arg.html#method.last
/// [`AppSettings::TrailingVarArg`]: ./enum.AppSettings.html#variant.TrailingVarArg
pub fn raw(self, raw: bool) -> Self {
self.multiple(raw).allow_hyphen_values(raw).last(raw)
}
pub fn raw(self, raw: bool) -> Self { self.multiple(raw).allow_hyphen_values(raw).last(raw) }

/// Hides an argument from short help message output.
///
/// **NOTE:** This does **not** hide the argument from usage strings on error
///
/// **NOTE:** Setting this option will cause next-line-help output style to be used
/// when long help (`--help`) is called.
///
///
/// # Examples
///
/// ```rust
Expand Down Expand Up @@ -3812,9 +3816,9 @@ impl<'a, 'b> Arg<'a, 'b> {
/// -h, --help Prints help information
/// -V, --version Prints version information
/// ```
///
///
/// However, when --help is called
///
///
/// ```rust
/// # use clap::{App, Arg};
/// let m = App::new("prog")
Expand All @@ -3826,24 +3830,24 @@ impl<'a, 'b> Arg<'a, 'b> {
/// "prog", "--help"
/// ]);
/// ```
///
///
/// Then the following would be displayed
///
///
/// ```notrust
/// helptest
///
///
/// USAGE:
/// helptest [FLAGS]
///
///
/// FLAGS:
/// --config Some help text describing the --config arg
/// -h, --help Prints help information
/// -V, --version Prints version information
/// ```
pub fn hidden_short_help(self, hide: bool) -> Self {
if hide {
if hide {
self.set(ArgSettings::HiddenShortHelp)
} else {
} else {
self.unset(ArgSettings::HiddenShortHelp)
}
}
Expand All @@ -3854,7 +3858,7 @@ impl<'a, 'b> Arg<'a, 'b> {
///
/// **NOTE:** Setting this option will cause next-line-help output style to be used
/// when long help (`--help`) is called.
///
///
/// # Examples
///
/// ```rust
Expand Down Expand Up @@ -3889,9 +3893,9 @@ impl<'a, 'b> Arg<'a, 'b> {
/// -h, --help Prints help information
/// -V, --version Prints version information
/// ```
///
///
/// However, when -h is called
///
///
/// ```rust
/// # use clap::{App, Arg};
/// let m = App::new("prog")
Expand All @@ -3903,15 +3907,15 @@ impl<'a, 'b> Arg<'a, 'b> {
/// "prog", "-h"
/// ]);
/// ```
///
///
/// Then the following would be displayed
///
///
/// ```notrust
/// helptest
///
///
/// USAGE:
/// helptest [FLAGS]
///
///
/// FLAGS:
/// --config Some help text describing the --config arg
/// -h, --help Prints help information
Expand Down Expand Up @@ -3963,7 +3967,8 @@ impl<'a, 'b> Arg<'a, 'b> {
#[doc(hidden)]
pub fn _build(&mut self) {
if (self.is_set(ArgSettings::UseValueDelimiter)
|| self.is_set(ArgSettings::RequireDelimiter)) && self.val_delim.is_none() {
|| self.is_set(ArgSettings::RequireDelimiter)) && self.val_delim.is_none()
{
self.val_delim = Some(',');
}
if self.index.is_some() || (self.short.is_none() && self.long.is_none()) {
Expand Down Expand Up @@ -4004,8 +4009,9 @@ impl<'a, 'b> Arg<'a, 'b> {
let mult_vals = self.val_names
.as_ref()
.map_or(true, |names| names.len() < 2);
if (self.is_set(ArgSettings::MultipleValues) || self.is_set(ArgSettings::MultipleOccurrences))
&& mult_vals {
if (self.is_set(ArgSettings::MultipleValues)
|| self.is_set(ArgSettings::MultipleOccurrences)) && mult_vals
{
"..."
} else {
""
Expand Down Expand Up @@ -4045,29 +4051,28 @@ impl<'a, 'b> Arg<'a, 'b> {
// Deprecations
// @TODO @v3-beta: remove
impl<'a, 'b> Arg<'a, 'b> {

/// **Deprecated**
#[deprecated(since="2.30.0", note="Renamed to `Arg::setting`. Will be removed in v3.0-beta")]
#[deprecated(since = "2.30.0", note = "Renamed to `Arg::setting`. Will be removed in v3.0-beta")]
pub fn set(mut self, s: ArgSettings) -> Self {
self.setb(s);
self
}

/// **Deprecated**
#[deprecated(since="2.30.0", note="Renamed to `Arg::unset_setting`. Will be removed in v3.0-beta")]
#[deprecated(
since = "2.30.0", note = "Renamed to `Arg::unset_setting`. Will be removed in v3.0-beta"
)]
pub fn unset(mut self, s: ArgSettings) -> Self {
self.unsetb(s);
self
}


/// **Deprecated**
#[deprecated(since="2.30.0", note="Use `Arg::from` instead. Will be removed in v3.0-beta")]
#[deprecated(since = "2.30.0", note = "Use `Arg::from` instead. Will be removed in v3.0-beta")]
pub fn from_usage(u: &'a str) -> Self {
let parser = UsageParser::from_usage(u);
parser.parse()
}

}

impl<'a, 'b, 'z> From<&'z Arg<'a, 'b>> for Arg<'a, 'b> {
Expand Down Expand Up @@ -4103,8 +4108,7 @@ impl<'n, 'e> Display for Arg<'n, 'e> {
}
if self.settings.is_set(ArgSettings::MultipleValues)
&& (self.val_names.is_none()
|| (self.val_names.is_some()
&& self.val_names.as_ref().unwrap().len() == 1))
|| (self.val_names.is_some() && self.val_names.as_ref().unwrap().len() == 1))
{
write!(f, "...")?;
}
Expand Down Expand Up @@ -4234,9 +4238,9 @@ impl<'n, 'e> fmt::Debug for Arg<'n, 'e> {
// Flags
#[cfg(test)]
mod test {
use map::VecMap;
use args::settings::ArgSettings;
use super::Arg;
use args::settings::ArgSettings;
use map::VecMap;

#[test]
fn flag_display() {
Expand Down

0 comments on commit 5ebd15d

Please sign in to comment.