mirror of
https://github.com/clap-rs/clap
synced 2024-12-14 14:52:33 +00:00
refactor: moves code for ergonomics
This commit is contained in:
parent
bf4d8417b8
commit
1b69ca4bee
5 changed files with 3428 additions and 3483 deletions
3471
src/app/app.rs
3471
src/app/app.rs
File diff suppressed because it is too large
Load diff
3383
src/app/mod.rs
3383
src/app/mod.rs
File diff suppressed because it is too large
Load diff
|
@ -11,6 +11,7 @@ pub type ClapResult<T> = Result<T, ClapError>;
|
|||
#[doc(hidden)]
|
||||
#[allow(non_snake_case)]
|
||||
pub mod error_builder {
|
||||
use suggestions;
|
||||
use super::ClapError;
|
||||
use super::ClapErrorType as cet;
|
||||
use fmt::Format;
|
||||
|
@ -48,19 +49,29 @@ pub mod error_builder {
|
|||
Format::Warning(arg.as_ref()),
|
||||
usage.as_ref(),
|
||||
Format::Good("--help")),
|
||||
error_type: cet::InvalidValue,
|
||||
error_type: cet::EmptyValue,
|
||||
}
|
||||
}
|
||||
|
||||
/// Error occurs when some possible values were set, but clap found unexpected value
|
||||
pub fn InvalidValue<S>(bad_val: S,
|
||||
good_vals: &[S],
|
||||
arg: S,
|
||||
valid_vals: S,
|
||||
did_you_mean: S,
|
||||
usage: S)
|
||||
-> ClapError
|
||||
where S: AsRef<str>
|
||||
{
|
||||
let suffix = suggestions::did_you_mean_suffix(arg.as_ref(),
|
||||
good_vals.iter(),
|
||||
suggestions::DidYouMeanMessageStyle::EnumValue);
|
||||
|
||||
let mut sorted = vec![];
|
||||
for v in good_vals {
|
||||
sorted.push(v.as_ref());
|
||||
}
|
||||
sorted.sort();
|
||||
let valid_values = sorted.iter()
|
||||
.fold(String::new(), |a, name| a + &format!( " {}", name)[..]);
|
||||
ClapError {
|
||||
error: format!("{} '{}' isn't a valid value for '{}'\n\t\
|
||||
[valid values:{}]\n\
|
||||
|
@ -70,8 +81,8 @@ pub mod error_builder {
|
|||
Format::Error("error:"),
|
||||
Format::Warning(bad_val.as_ref()),
|
||||
Format::Warning(arg.as_ref()),
|
||||
valid_vals.as_ref(),
|
||||
did_you_mean.as_ref(),
|
||||
valid_values,
|
||||
suffix.0,
|
||||
usage.as_ref(),
|
||||
Format::Good("--help")),
|
||||
error_type: cet::InvalidValue,
|
||||
|
@ -280,7 +291,6 @@ pub mod error_builder {
|
|||
error_type: cet::UnexpectedMultipleUsage,
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// Command line argument parser error types
|
|
@ -671,8 +671,9 @@ extern crate vec_map;
|
|||
#[cfg(feature = "yaml")]
|
||||
pub use yaml_rust::YamlLoader;
|
||||
pub use args::{Arg, ArgGroup, ArgMatches, SubCommand};
|
||||
pub use app::{App, AppSettings, ClapError, ClapErrorType};
|
||||
pub use app::{App, AppSettings};
|
||||
pub use fmt::Format;
|
||||
pub use errors::{ClapError, ClapErrorType};
|
||||
|
||||
#[macro_use]
|
||||
mod macros;
|
||||
|
@ -680,3 +681,5 @@ mod app;
|
|||
mod args;
|
||||
mod usageparser;
|
||||
mod fmt;
|
||||
mod suggestions;
|
||||
mod errors;
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
#[cfg(feature = "suggestions")]
|
||||
use strsim;
|
||||
|
||||
use fmt::Format;
|
||||
|
||||
/// Produces a string from a given list of possible values which is similar to
|
||||
/// the passed in value `v` with a certain confidence.
|
||||
/// Thus in a list of possible values like ["foo", "bar"], the value "fop" will yield
|
||||
|
@ -34,6 +36,34 @@ pub fn did_you_mean<'a, T, I>(_: &str, _: I) -> Option<&'a str>
|
|||
None
|
||||
}
|
||||
|
||||
/// Returns a suffix that can be empty, or is the standard 'did you mean phrase
|
||||
#[cfg_attr(feature = "lints", allow(needless_lifetimes))]
|
||||
pub fn did_you_mean_suffix<'z, T, I>(arg: &str,
|
||||
values: I,
|
||||
style: DidYouMeanMessageStyle)
|
||||
-> (String, Option<&'z str>)
|
||||
where T: AsRef<str> + 'z,
|
||||
I: IntoIterator<Item = &'z T>
|
||||
{
|
||||
match did_you_mean(arg, values) {
|
||||
Some(candidate) => {
|
||||
let mut suffix = "\n\tDid you mean ".to_owned();
|
||||
match style {
|
||||
DidYouMeanMessageStyle::LongFlag =>
|
||||
suffix.push_str(&*format!("{}", Format::Good("--"))),
|
||||
DidYouMeanMessageStyle::EnumValue => suffix.push('\''),
|
||||
}
|
||||
suffix.push_str(&Format::Good(candidate).to_string()[..]);
|
||||
if let DidYouMeanMessageStyle::EnumValue = style {
|
||||
suffix.push('\'');
|
||||
}
|
||||
suffix.push_str(" ?");
|
||||
(suffix, Some(candidate))
|
||||
}
|
||||
None => (String::new(), None),
|
||||
}
|
||||
}
|
||||
|
||||
/// A helper to determine message formatting
|
||||
pub enum DidYouMeanMessageStyle {
|
||||
/// Suggested value is a long flag
|
Loading…
Reference in a new issue