2834: fix: Allow unicode-aware case insensitivity with ArgValue r=pksunkara a=epage



Co-authored-by: Ed Page <eopage@gmail.com>
This commit is contained in:
bors[bot] 2021-10-09 12:37:33 +00:00 committed by GitHub
commit 5afa640e3b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 33 additions and 5 deletions

View file

@ -8,7 +8,8 @@ TODO: `YamlLoader`
#### BREAKING CHANGES
* **Renamed Features**
* `unicode_help` to `unicode` to encompass more functionality
<a name="v3.0.0-beta.4"></a>
## v3.0.0-beta.4 (2021-08-14)

View file

@ -63,6 +63,7 @@ path = "benches/06_rustup.rs"
clap_derive = { path = "./clap_derive", version = "=3.0.0-beta.4", optional = true }
bitflags = "1.2"
textwrap = { version = "0.14.0", default-features = false, features = [] }
unicase = { version = "2.6", optional = true }
indexmap = "1.0"
os_str_bytes = "4.1"
strsim = { version = "0.10", optional = true }
@ -88,7 +89,7 @@ default = [
"color",
"env",
"suggestions",
"unicode_help",
"unicode",
]
debug = ["clap_derive/debug", "backtrace"] # Enables debug messages
@ -99,7 +100,7 @@ cargo = ["lazy_static"] # Disable if you're not using Cargo, enables Cargo-env-v
color = ["atty", "termcolor"]
env = [] # Use environment variables during arg parsing
suggestions = ["strsim"]
unicode_help = ["textwrap/unicode-width"] # Enable if any unicode in help message
unicode = ["textwrap/unicode-width", "unicase"] # Support for unicode characters in arguments and help messages
# Optional
wrap_help = ["terminal_size", "textwrap/terminal_size"]

View file

@ -475,7 +475,7 @@ Disabling optional features can decrease the binary size of `clap` and decrease
* **color**: Turns on colored error messages. You still have to turn on colored help by setting `AppSettings::ColoredHelp`. (builds dependency `termcolor`)
* **env**: Turns on the usage of environment variables during parsing.
* **suggestions**: Turns on the `Did you mean '--myoption'?` feature for when users make typos. (builds dependency `strsim`)
* **unicode_help**: Turns on support for unicode characters in help messages. (builds dependency `textwrap`)
* **unicode**: Turns on support for unicode characters in arguments and help messages. (builds dependency `textwrap`, `unicase`)
To disable these, add this to your `Cargo.toml`:

View file

@ -1,5 +1,7 @@
use std::iter;
use crate::util::eq_ignore_case;
/// The representation of a possible value of an argument.
///
/// This is used for specifying [possible values] of [Args].
@ -90,7 +92,7 @@ impl<'help> ArgValue<'help> {
pub fn matches(&self, value: &str, ignore_case: bool) -> bool {
if ignore_case {
self.get_name_and_aliases()
.any(|name| name.eq_ignore_ascii_case(value))
.any(|name| eq_ignore_case(name, value))
} else {
self.get_name_and_aliases().any(|name| name == value)
}

View file

@ -34,3 +34,10 @@ pub(crate) fn safe_exit(code: i32) -> ! {
std::process::exit(code)
}
#[cfg(not(feature = "unicode"))]
pub(crate) fn eq_ignore_case(left: &str, right: &str) -> bool {
left.eq_ignore_ascii_case(right)
}
#[cfg(feature = "unicode")]
pub(crate) use unicase::eq as eq_ignore_case;

17
tests/unicode.rs Normal file
View file

@ -0,0 +1,17 @@
#[test]
#[cfg(feature = "unicode")]
fn possible_values_case_insensitive() {
let m = clap::App::new("pv")
.arg(
clap::Arg::new("option")
.short('o')
.long("--option")
.takes_value(true)
.possible_value("ä")
.case_insensitive(true),
)
.try_get_matches_from(vec!["pv", "--option", "Ä"]);
assert!(m.is_ok());
assert!(m.unwrap().value_of("option").is_some());
}