mirror of
https://github.com/clap-rs/clap
synced 2024-12-15 07:12:32 +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)]
|
#[doc(hidden)]
|
||||||
#[allow(non_snake_case)]
|
#[allow(non_snake_case)]
|
||||||
pub mod error_builder {
|
pub mod error_builder {
|
||||||
|
use suggestions;
|
||||||
use super::ClapError;
|
use super::ClapError;
|
||||||
use super::ClapErrorType as cet;
|
use super::ClapErrorType as cet;
|
||||||
use fmt::Format;
|
use fmt::Format;
|
||||||
|
@ -48,19 +49,29 @@ pub mod error_builder {
|
||||||
Format::Warning(arg.as_ref()),
|
Format::Warning(arg.as_ref()),
|
||||||
usage.as_ref(),
|
usage.as_ref(),
|
||||||
Format::Good("--help")),
|
Format::Good("--help")),
|
||||||
error_type: cet::InvalidValue,
|
error_type: cet::EmptyValue,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Error occurs when some possible values were set, but clap found unexpected value
|
/// Error occurs when some possible values were set, but clap found unexpected value
|
||||||
pub fn InvalidValue<S>(bad_val: S,
|
pub fn InvalidValue<S>(bad_val: S,
|
||||||
|
good_vals: &[S],
|
||||||
arg: S,
|
arg: S,
|
||||||
valid_vals: S,
|
|
||||||
did_you_mean: S,
|
|
||||||
usage: S)
|
usage: S)
|
||||||
-> ClapError
|
-> ClapError
|
||||||
where S: AsRef<str>
|
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 {
|
ClapError {
|
||||||
error: format!("{} '{}' isn't a valid value for '{}'\n\t\
|
error: format!("{} '{}' isn't a valid value for '{}'\n\t\
|
||||||
[valid values:{}]\n\
|
[valid values:{}]\n\
|
||||||
|
@ -70,8 +81,8 @@ pub mod error_builder {
|
||||||
Format::Error("error:"),
|
Format::Error("error:"),
|
||||||
Format::Warning(bad_val.as_ref()),
|
Format::Warning(bad_val.as_ref()),
|
||||||
Format::Warning(arg.as_ref()),
|
Format::Warning(arg.as_ref()),
|
||||||
valid_vals.as_ref(),
|
valid_values,
|
||||||
did_you_mean.as_ref(),
|
suffix.0,
|
||||||
usage.as_ref(),
|
usage.as_ref(),
|
||||||
Format::Good("--help")),
|
Format::Good("--help")),
|
||||||
error_type: cet::InvalidValue,
|
error_type: cet::InvalidValue,
|
||||||
|
@ -280,7 +291,6 @@ pub mod error_builder {
|
||||||
error_type: cet::UnexpectedMultipleUsage,
|
error_type: cet::UnexpectedMultipleUsage,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Command line argument parser error types
|
/// Command line argument parser error types
|
|
@ -671,8 +671,9 @@ extern crate vec_map;
|
||||||
#[cfg(feature = "yaml")]
|
#[cfg(feature = "yaml")]
|
||||||
pub use yaml_rust::YamlLoader;
|
pub use yaml_rust::YamlLoader;
|
||||||
pub use args::{Arg, ArgGroup, ArgMatches, SubCommand};
|
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 fmt::Format;
|
||||||
|
pub use errors::{ClapError, ClapErrorType};
|
||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
mod macros;
|
mod macros;
|
||||||
|
@ -680,3 +681,5 @@ mod app;
|
||||||
mod args;
|
mod args;
|
||||||
mod usageparser;
|
mod usageparser;
|
||||||
mod fmt;
|
mod fmt;
|
||||||
|
mod suggestions;
|
||||||
|
mod errors;
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
#[cfg(feature = "suggestions")]
|
#[cfg(feature = "suggestions")]
|
||||||
use strsim;
|
use strsim;
|
||||||
|
|
||||||
|
use fmt::Format;
|
||||||
|
|
||||||
/// Produces a string from a given list of possible values which is similar to
|
/// Produces a string from a given list of possible values which is similar to
|
||||||
/// the passed in value `v` with a certain confidence.
|
/// the passed in value `v` with a certain confidence.
|
||||||
/// Thus in a list of possible values like ["foo", "bar"], the value "fop" will yield
|
/// 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
|
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
|
/// A helper to determine message formatting
|
||||||
pub enum DidYouMeanMessageStyle {
|
pub enum DidYouMeanMessageStyle {
|
||||||
/// Suggested value is a long flag
|
/// Suggested value is a long flag
|
Loading…
Reference in a new issue