diff options
author | Mikihiro SUDA <s@sudame.net> | 2023-01-12 14:35:51 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-01-12 13:35:51 +0800 |
commit | da61909835b281d7f7950ec3f763cd077b5b8f02 (patch) | |
tree | 41b6a17dce158291894e30407f62c34a433e1cb8 /src | |
parent | f22ad5b2ef5e4910995cbc40dd666098b995b85e (diff) |
Avoid using clap deprecated features (#787)
The PR will fix #786 .
In order to minimize future security risks and maintain ease of use, it
is recommended to avoid using deprecated features of clap.
I have refactored the code so that no warning appears when the following
command is executed:
```sh
cargo check --features clap/deprecated
```
Diffstat (limited to 'src')
-rw-r--r-- | src/app.rs | 196 | ||||
-rw-r--r-- | src/display.rs | 12 | ||||
-rw-r--r-- | src/flags/blocks.rs | 51 | ||||
-rw-r--r-- | src/flags/color.rs | 24 | ||||
-rw-r--r-- | src/flags/date.rs | 32 | ||||
-rw-r--r-- | src/flags/dereference.rs | 6 | ||||
-rw-r--r-- | src/flags/display.rs | 18 | ||||
-rw-r--r-- | src/flags/header.rs | 6 | ||||
-rw-r--r-- | src/flags/hyperlink.rs | 21 | ||||
-rw-r--r-- | src/flags/icons.rs | 37 | ||||
-rw-r--r-- | src/flags/ignore_globs.rs | 14 | ||||
-rw-r--r-- | src/flags/indicators.rs | 6 | ||||
-rw-r--r-- | src/flags/layout.rs | 22 | ||||
-rw-r--r-- | src/flags/permission.rs | 21 | ||||
-rw-r--r-- | src/flags/recursion.rs | 32 | ||||
-rw-r--r-- | src/flags/size.rs | 26 | ||||
-rw-r--r-- | src/flags/sorting.rs | 71 | ||||
-rw-r--r-- | src/flags/symlink_arrow.rs | 4 | ||||
-rw-r--r-- | src/flags/symlinks.rs | 6 | ||||
-rw-r--r-- | src/flags/total_size.rs | 6 | ||||
-rw-r--r-- | src/main.rs | 10 | ||||
-rw-r--r-- | src/meta/symlink.rs | 6 |
22 files changed, 325 insertions, 302 deletions
@@ -1,178 +1,173 @@ -use clap::{App, Arg, ValueHint}; +use clap::{Arg, ArgAction, Command, ValueHint}; -pub fn build() -> App<'static> { - App::new("lsd") +pub fn build() -> Command<'static> { + Command::new("lsd") .version(env!("CARGO_PKG_VERSION")) .about(env!("CARGO_PKG_DESCRIPTION")) .arg( - Arg::with_name("FILE") - .multiple(true) + Arg::new("FILE") + .action(ArgAction::Append) + .multiple_values(true) .default_value(".") .value_hint(ValueHint::AnyPath), ) .arg( - Arg::with_name("all") + Arg::new("all") .short('a') .overrides_with("almost-all") .long("all") - .multiple_occurrences(true) + .action(ArgAction::SetTrue) .help("Do not ignore entries starting with ."), ) .arg( - Arg::with_name("almost-all") + Arg::new("almost-all") .short('A') .overrides_with("all") .long("almost-all") - .multiple_occurrences(true) + .action(ArgAction::SetTrue) .help("Do not list implied . and .."), ) .arg( - Arg::with_name("color") + Arg::new("color") .long("color") - .possible_value("always") - .possible_value("auto") - .possible_value("never") + .value_parser(["always", "auto", "never"]) .default_value("auto") - .multiple_occurrences(true) + .action(ArgAction::Append) .takes_value(true) .number_of_values(1) .help("When to use terminal colours"), ) .arg( - Arg::with_name("icon") + Arg::new("icon") .long("icon") - .possible_value("always") - .possible_value("auto") - .possible_value("never") + .value_parser(["always", "auto", "never"]) .default_value("auto") - .multiple_occurrences(true) + .action(ArgAction::Append) .takes_value(true) .number_of_values(1) .help("When to print the icons"), ) .arg( - Arg::with_name("icon-theme") + Arg::new("icon-theme") .long("icon-theme") .default_value("fancy") - .possible_value("fancy") - .possible_value("unicode") - .multiple_occurrences(true) + .value_parser(["fancy", "unicode"]) + .action(ArgAction::Append) .takes_value(true) .number_of_values(1) .help("Whether to use fancy or unicode icons"), ) .arg( - Arg::with_name("indicators") + Arg::new("indicators") .short('F') .long("classify") - .multiple_occurrences(true) + .action(ArgAction::SetTrue) .help("Append indicator (one of */=>@|) at the end of the file names"), ) .arg( - Arg::with_name("long") + Arg::new("long") .short('l') .long("long") - .multiple_occurrences(true) + .action(ArgAction::SetTrue) .help("Display extended file metadata as a table"), ) .arg( - Arg::with_name("ignore-config") + Arg::new("ignore-config") .long("ignore-config") + .action(ArgAction::SetTrue) .help("Ignore the configuration file"), ) .arg( - Arg::with_name("config-file") + Arg::new("config-file") .long("config-file") .help("Provide a custom lsd configuration file") .value_name("config-file") .takes_value(true) ) .arg( - Arg::with_name("oneline") + Arg::new("oneline") .short('1') .long("oneline") - .multiple_occurrences(true) + .action(ArgAction::SetTrue) .help("Display one entry per line"), ) .arg( - Arg::with_name("recursive") + Arg::new("recursive") .short('R') .long("recursive") - .multiple_occurrences(true) + .action(ArgAction::SetTrue) .conflicts_with("tree") .help("Recurse into directories"), ) .arg( - Arg::with_name("human_readable") + Arg::new("human_readable") .short('h') .long("human-readable") - .multiple_occurrences(true) + .action(ArgAction::SetTrue) .help("For ls compatibility purposes ONLY, currently set by default"), ) .arg( - Arg::with_name("tree") + Arg::new("tree") .long("tree") - .multiple_occurrences(true) + .action(ArgAction::SetTrue) .conflicts_with("recursive") .help("Recurse into directories and present the result as a tree"), ) .arg( - Arg::with_name("depth") + Arg::new("depth") .long("depth") - .multiple_occurrences(true) + .action(ArgAction::Append) .takes_value(true) .value_name("num") .help("Stop recursing into directories after reaching specified depth"), ) .arg( - Arg::with_name("directory-only") + Arg::new("directory-only") .short('d') .long("directory-only") + .action(ArgAction::SetTrue) .conflicts_with("depth") .conflicts_with("recursive") .help("Display directories themselves, and not their contents (recursively when used with --tree)"), ) .arg( - Arg::with_name("permission") + Arg::new("permission") .long("permission") .default_value("rwx") - .possible_value("rwx") - .possible_value("octal") - .multiple_occurrences(true) + .value_parser(["rwx", "octal"]) + .action(ArgAction::Append) .takes_value(true) .number_of_values(1) .help("How to display permissions"), ) .arg( - Arg::with_name("size") + Arg::new("size") .long("size") - .possible_value("default") - .possible_value("short") - .possible_value("bytes") + .value_parser(["default", "short", "bytes"]) .default_value("default") - .multiple_occurrences(true) + .action(ArgAction::Append) .takes_value(true) .number_of_values(1) .help("How to display size"), ) .arg( - Arg::with_name("total-size") + Arg::new("total-size") .long("total-size") - .multiple_occurrences(true) + .action(ArgAction::SetTrue) .help("Display the total size of directories"), ) .arg( - Arg::with_name("date") + Arg::new("date") .long("date") - .validator(validate_date_argument) + .value_parser(validate_date_argument) .default_value("date") - .multiple_occurrences(true) + .action(ArgAction::Append) .takes_value(true) .number_of_values(1) .help("How to display date [possible values: date, relative, +date-time-format]"), ) .arg( - Arg::with_name("timesort") + Arg::new("timesort") .short('t') .long("timesort") .overrides_with("sizesort") @@ -180,11 +175,11 @@ pub fn build() -> App<'static> { .overrides_with("versionsort") .overrides_with("sort") .overrides_with("no-sort") - .multiple_occurrences(true) + .action(ArgAction::SetTrue) .help("Sort by time modified"), ) .arg( - Arg::with_name("sizesort") + Arg::new("sizesort") .short('S') .long("sizesort") .overrides_with("timesort") @@ -192,11 +187,11 @@ pub fn build() -> App<'static> { .overrides_with("versionsort") .overrides_with("sort") .overrides_with("no-sort") - .multiple_occurrences(true) + .action(ArgAction::SetTrue) .help("Sort by size"), ) .arg( - Arg::with_name("extensionsort") + Arg::new("extensionsort") .short('X') .long("extensionsort") .overrides_with("sizesort") @@ -204,14 +199,14 @@ pub fn build() -> App<'static> { .overrides_with("versionsort") .overrides_with("sort") .overrides_with("no-sort") - .multiple_occurrences(true) + .action(ArgAction::SetTrue) .help("Sort by file extension"), ) .arg( - Arg::with_name("versionsort") + Arg::new("versionsort") .short('v') .long("versionsort") - .multiple_occurrences(true) + .action(ArgAction::SetTrue) .overrides_with("timesort") .overrides_with("sizesort") .overrides_with("extensionsort") @@ -220,10 +215,10 @@ pub fn build() -> App<'static> { .help("Natural sort of (version) numbers within text"), ) .arg( - Arg::with_name("sort") + Arg::new("sort") .long("sort") - .multiple_occurrences(true) - .possible_values(&["size", "time", "version", "extension", "none"]) + .action(ArgAction::Append) + .value_parser(["size", "time", "version", "extension", "none"]) .takes_value(true) .value_name("WORD") .overrides_with("timesort") @@ -234,10 +229,10 @@ pub fn build() -> App<'static> { .help("sort by WORD instead of name") ) .arg( - Arg::with_name("no-sort") + Arg::new("no-sort") .short('U') .long("no-sort") - .multiple_occurrences(true) + .action(ArgAction::SetTrue) .overrides_with("timesort") .overrides_with("sizesort") .overrides_with("extensionsort") @@ -246,36 +241,35 @@ pub fn build() -> App<'static> { .help("Do not sort. List entries in directory order") ) .arg( - Arg::with_name("reverse") + Arg::new("reverse") .short('r') .long("reverse") - .multiple_occurrences(true) + .action(ArgAction::SetTrue) .help("Reverse the order of the sort"), ) .arg( - Arg::with_name("group-dirs") + Arg::new("group-dirs") .long("group-dirs") - .possible_value("none") - .possible_value("first") - .possible_value("last") - .multiple_occurrences(true) + .value_parser(["none", "first", "last"]) + .action(ArgAction::Append) .number_of_values(1) .help("Sort the directories then the files"), ) .arg( - Arg::with_name("group-directories-first") + Arg::new("group-directories-first") .long("group-directories-first") + .action(ArgAction::SetTrue) .help("Groups the directories at the top before the files. Same as --group-dirs=first") ) .arg( - Arg::with_name("blocks") + Arg::new("blocks") .long("blocks") - .multiple_occurrences(true) + .action(ArgAction::Append) .multiple_values(true) .takes_value(true) .use_value_delimiter(true) - .require_delimiter(true) - .possible_values(&[ + .require_value_delimiter(true) + .value_parser([ "permission", "user", "group", @@ -289,84 +283,86 @@ pub fn build() -> App<'static> { .help("Specify the blocks that will be displayed and in what order"), ) .arg( - Arg::with_name("classic") + Arg::new("classic") .long("classic") + .action(ArgAction::SetTrue) .help("Enable classic mode (display output similar to ls)"), ) .arg( - Arg::with_name("no-symlink") + Arg::new("no-symlink") .long("no-symlink") - .multiple_occurrences(true) + .action(ArgAction::SetTrue) .help("Do not display symlink target"), ) .arg( - Arg::with_name("ignore-glob") + Arg::new("ignore-glob") .short('I') .long("ignore-glob") - .multiple_occurrences(true) + .action(ArgAction::Append) .number_of_values(1) .value_name("pattern") .default_value("") .help("Do not display files/directories with names matching the glob pattern(s). More than one can be specified by repeating the argument"), ) .arg( - Arg::with_name("inode") + Arg::new("inode") .short('i') .long("inode") - .multiple_occurrences(true) + .action(ArgAction::SetTrue) .help("Display the index number of each file"), ) .arg( - Arg::with_name("dereference") + Arg::new("dereference") .short('L') .long("dereference") - .multiple_occurrences(true) + .action(ArgAction::SetTrue) .help("When showing file information for a symbolic link, show information for the file the link references rather than for the link itself"), ) .arg( - Arg::with_name("context") + Arg::new("context") .short('Z') .long("context") .required(false) .takes_value(false) + .action(ArgAction::SetTrue) .help("Print security context (label) of each file"), ) .arg( - Arg::with_name("hyperlink") + Arg::new("hyperlink") .long("hyperlink") - .possible_value("always") - .possible_value("auto") - .possible_value("never") + .value_parser(["always", "auto", "never"]) .default_value("never") - .multiple_occurrences(true) + .action(ArgAction::Append) .takes_value(true) .number_of_values(1) .help("Attach hyperlink to filenames"), ) .arg( - Arg::with_name("header") + Arg::new("header") .long("header") + .action(ArgAction::SetTrue) .help("Display block headers"), ) .arg( - Arg::with_name("system-protected") + Arg::new("system-protected") .long("system-protected") + .action(ArgAction::SetTrue) .help("Includes files with the windows system protection flag set. This is the same as --all on other platforms") .hide(!cfg!(windows)), ) } -fn validate_date_argument(arg: &str) -> Result<(), String> { +fn validate_date_argument(arg: &str) -> Result<String, String> { if arg.starts_with('+') { validate_time_format(arg) } else if arg == "date" || arg == "relative" { - Result::Ok(()) + Result::Ok(arg.to_owned()) } else { Result::Err("possible values: date, relative, +date-time-format".to_owned()) } } -pub fn validate_time_format(formatter: &str) -> Result<(), String> { +pub fn validate_time_format(formatter: &str) -> Result<String, String> { let mut chars = formatter.chars(); loop { match chars.next() { @@ -412,5 +408,5 @@ pub fn validate_time_format(formatter: &str) -> Result<(), String> { _ => continue, } } - Ok(()) + Ok(formatter.to_owned()) } diff --git a/src/display.rs b/src/display.rs index c57b3f6..e55bebe 100644 --- a/src/display.rs +++ b/src/display.rs @@ -635,7 +635,7 @@ mod tests { #[test] fn test_display_tree_with_all() { let argv = ["lsd", "--tree", "--all"]; - let matches = app::build().get_matches_from_safe(argv).unwrap(); + let matches = app::build().try_get_matches_from(argv).unwrap(); let flags = Flags::configure_from(&matches, &Config::with_none()).unwrap(); let dir = assert_fs::TempDir::new().unwrap(); @@ -668,7 +668,7 @@ mod tests { #[test] fn test_tree_align_subfolder() { let argv = ["lsd", "--tree", "--blocks", "size,name"]; - let matches = app::build().get_matches_from_safe(argv).unwrap(); + let matches = app::build().try_get_matches_from(argv).unwrap(); let flags = Flags::configure_from(&matches, &Config::with_none()).unwrap(); let dir = assert_fs::TempDir::new().unwrap(); @@ -708,7 +708,7 @@ mod tests { #[cfg(unix)] fn test_tree_size_first_without_name() { let argv = ["lsd", "--tree", "--blocks", "size,permission"]; - let matches = app::build().get_matches_from_safe(argv).unwrap(); + let matches = app::build().try_get_matches_from(argv).unwrap(); let flags = Flags::configure_from(&matches, &Config::with_none()).unwrap(); let dir = assert_fs::TempDir::new().unwrap(); @@ -747,7 +747,7 @@ mod tests { #[test] fn test_tree_edge_before_name() { let argv = ["lsd", "--tree", "--long"]; - let matches = app::build().get_matches_from_safe(argv).unwrap(); + let matches = app::build().try_get_matches_from(argv).unwrap(); let flags = Flags::configure_from(&matches, &Config::with_none()).unwrap(); let dir = assert_fs::TempDir::new().unwrap(); @@ -777,7 +777,7 @@ mod tests { "--blocks", "permission,user,group,size,date,name,inode,links", ]; - let matches = app::build().get_matches_from_safe(argv).unwrap(); + let matches = app::build().try_get_matches_from(argv).unwrap(); let flags = Flags::configure_from(&matches, &Config::with_none()).unwrap(); let dir = assert_fs::TempDir::new().unwrap(); @@ -811,7 +811,7 @@ mod tests { #[test] fn test_grid_no_header_with_empty_meta() { let argv = ["lsd", "--header", "-l"]; - let matches = app::build().get_matches_from_safe(argv).unwrap(); + let matches = app::build().try_get_matches_from(argv).unwrap(); let flags = Flags::configure_from(&matches, &Config::with_none()).unwrap(); let dir = assert_fs::TempDir::new().unwrap(); diff --git a/src/flags/blocks.rs b/src/flags/blocks.rs index c355836..dc2e2c6 100644 --- a/src/flags/blocks.rs +++ b/src/flags/blocks.rs @@ -81,13 +81,13 @@ impl Configurable<Self> for Blocks { /// `Blocks` does not contain a [Block] of variant [INode](Block::INode) yet, one is prepended /// to the returned value. fn configure_from(matches: &ArgMatches, config: &Config) -> Self { - let mut blocks = if matches.is_present("long") { + let mut blocks = if matches.get_one("long") == Some(&true) { Self::long() } else { Default::default() }; - if matches.is_present("long") { + if matches.get_one("long") == Some(&true) { if let Some(value) = Self::from_config(config) { blocks = value; } @@ -97,10 +97,10 @@ impl Configurable<Self> for Blocks { blocks = value; } - if matches.is_present("context") { + if matches.get_one("context") == Some(&true) { blocks.optional_insert_context(); } - if matches.is_present("inode") { + if matches.get_one("inode") == Some(&true) { blocks.optional_prepend_inode(); } @@ -113,11 +113,14 @@ impl Configurable<Self> for Blocks { /// values in a [Some]. Otherwise if the "long" argument is passed, this returns /// [Blocks::long]. Finally if none of the previous happened, this returns [None]. fn from_arg_matches(matches: &ArgMatches) -> Option<Self> { - if matches.occurrences_of("blocks") == 0 { + if matches.get_many::<String>("blocks")?.len() == 0 { return None; } - if let Some(values) = matches.values_of("blocks") { + if let Some(values) = matches + .get_many::<String>("blocks") + .map(|values| values.map(String::as_str)) + { let mut blocks: Vec<Block> = Vec::with_capacity(values.len()); for value in values { blocks.push(Block::try_from(value).unwrap_or_else(|_| { @@ -229,7 +232,7 @@ mod test_blocks { let argv = ["lsd"]; let target = Blocks::default(); - let matches = app::build().get_matches_from_safe(argv).unwrap(); + let matches = app::build().try_get_matches_from(argv).unwrap(); let result = Blocks::configure_from(&matches, &Config::with_none()); assert_eq!(result, target); @@ -240,7 +243,7 @@ mod test_blocks { let argv = ["lsd", "--long"]; let target = Blocks::long(); - let matches = app::build().get_matches_from_safe(argv).unwrap(); + let matches = app::build().try_get_matches_from(argv).unwrap(); let result = Blocks::configure_from(&matches, &Config::with_none()); assert_eq!(result, target); @@ -251,7 +254,7 @@ mod test_blocks { let argv = ["lsd", "--blocks", "permission"]; let target = Blocks(vec![Block::Permission]); - let matches = app::build().get_matches_from_safe(argv).unwrap(); + let matches = app::build().try_get_matches_from(argv).unwrap(); let result = Blocks::configure_from(&matches, &Config::with_none()); assert_eq!(result, target); @@ -262,7 +265,7 @@ mod test_blocks { let argv = ["lsd", "--long", "--blocks", "permission"]; let target = Blocks(vec![Block::Permission]); - let matches = app::build().get_matches_from_safe(argv).unwrap(); + let matches = app::build().try_get_matches_from(argv).unwrap(); let result = Blocks::configure_from(&matches, &Config::with_none()); assert_eq!(result, target); @@ -273,7 +276,7 @@ mod test_blocks { let argv = ["lsd", "--inode"]; let target = Blocks(vec![Block::INode, Block::Name]); - let matches = app::build().get_matches_from_safe(argv).unwrap(); + let matches = app::build().try_get_matches_from(argv).unwrap(); let result = Blocks::configure_from(&matches, &Config::with_none()); assert_eq!(result, target); @@ -284,7 +287,7 @@ mod test_blocks { let argv = ["lsd", "--blocks", "permission", "--inode"]; let target = Blocks(vec![Block::INode, Block::Permission]); - let matches = app::build().get_matches_from_safe(argv).unwrap(); + let matches = app::build().try_get_matches_from(argv).unwrap(); let result = Blocks::configure_from(&matches, &Config::with_none()); assert_eq!(result, target); @@ -295,7 +298,7 @@ mod test_blocks { let argv = ["lsd", "--long", "--blocks", "permission", "--inode"]; let target = Blocks(vec![Block::INode, Block::Permission]); - let matches = app::build().get_matches_from_safe(argv).unwrap(); + let matches = app::build().try_get_matches_from(argv).unwrap(); let result = Blocks::configure_from(&matches, &Config::with_none()); assert_eq!(result, target); @@ -306,7 +309,7 @@ mod test_blocks { let argv = ["lsd", "--blocks", "permission,inode", "--inode"]; let target = Blocks(vec![Block::Permission, Block::INode]); - let matches = app::build().get_matches_from_safe(argv).unwrap(); + let matches = app::build().try_get_matches_from(argv).unwrap(); let result = Blocks::configure_from(&matches, &Config::with_none()); assert_eq!(result, target); @@ -317,7 +320,7 @@ mod test_blocks { let argv = ["lsd", "--long", "--blocks", "permission,inode", "--inode"]; let target = Blocks(vec![Block::Permission, Block::INode]); - let matches = app::build().get_matches_from_safe(argv).unwrap(); + let matches = app::build().try_get_matches_from(argv).unwrap(); let result = Blocks::configure_from(&matches, &Config::with_none()); assert_eq!(result, target); @@ -326,14 +329,14 @@ mod test_blocks { #[test] fn test_from_arg_matches_none() { let argv = ["lsd"]; - let matches = app::build().get_matches_from_safe(argv).unwrap(); + let matches = app::build().try_get_matches_from(argv).unwrap(); assert!(matches!(Blocks::from_arg_matches(&matches), None)); } #[test] fn test_from_arg_matches_one() { let argv = ["lsd", "--blocks", "permission"]; - let matches = app::build().get_matches_from_safe(argv).unwrap(); + let matches = app::build().try_get_matches_from(argv).unwrap(); let test_blocks = Blocks(vec![Block::Permission]); assert_eq!(Blocks::from_arg_matches(&matches), Some(test_blocks)); } @@ -341,7 +344,7 @@ mod test_blocks { #[test] fn test_from_arg_matches_multi_occurences() { let argv = ["lsd", "--blocks", "permission", "--blocks", "name"]; - let matches = app::build().get_matches_from_safe(argv).unwrap(); + let matches = app::build().try_get_matches_from(argv).unwrap(); let test_blocks = Blocks(vec![Block::Permission, Block::Name]); assert_eq!(Blocks::from_arg_matches(&matches), Some(test_blocks)); } @@ -349,7 +352,7 @@ mod test_blocks { #[test] fn test_from_arg_matches_multi_values() { let argv = ["lsd", "--blocks", "permission,name"]; - let matches = app::build().get_matches_from_safe(argv).unwrap(); + let matches = app::build().try_get_matches_from(argv).unwrap(); let test_blocks = Blocks(vec![Block::Permission, Block::Name]); assert_eq!(Blocks::from_arg_matches(&matches), Some(test_blocks)); } @@ -357,7 +360,7 @@ mod test_blocks { #[test] fn test_from_arg_matches_reversed_default() { let argv = ["lsd", "--blocks", "name,date,size,group,user,permission"]; - let matches = app::build().get_matches_from_safe(argv).unwrap(); + let matches = app::build().try_get_matches_from(argv).unwrap(); let test_blocks = Blocks(vec![ Block::Name, Block::Date, @@ -372,7 +375,7 @@ mod test_blocks { #[test] fn test_from_arg_matches_every_second_one() { let argv = ["lsd", "--blocks", "permission,group,date"]; - let matches = app::build().get_matches_from_safe(argv).unwrap(); + let matches = app::build().try_get_matches_from(argv).unwrap(); let test_blocks = Blocks(vec![Block::Permission, Block::Group, Block::Date]); assert_eq!(Blocks::from_arg_matches(&matches), Some(test_blocks)); } @@ -433,7 +436,7 @@ mod test_blocks { #[test] fn test_context_not_present_on_cli() { let argv = ["lsd", "--long"]; - let matches = app::build().get_matches_from_safe(argv).unwrap(); + let matches = app::build().try_get_matches_from(argv).unwrap(); let parsed_blocks = Blocks::configure_from(&matches, &Config::with_none()); let it = parsed_blocks.0.iter(); assert_eq!(it.filter(|&x| *x == Block::Context).count(), 0); @@ -442,7 +445,7 @@ mod test_blocks { #[test] fn test_context_present_if_context_on() { let argv = ["lsd", "--context"]; - let matches = app::build().get_matches_from_safe(argv).unwrap(); + let matches = app::build().try_get_matches_from(argv).unwrap(); let parsed_blocks = Blocks::configure_from(&matches, &Config::with_none()); let it = parsed_blocks.0.iter(); assert_eq!(it.filter(|&x| *x == Block::Context).count(), 1); @@ -456,7 +459,7 @@ mod test_blocks { "--blocks", "name,date,size,context,group,user,permission", ]; - let matches = app::build().get_matches_from_safe(argv).unwrap(); + let matches = app::build().try_get_matches_from(argv).unwrap(); let test_blocks = Blocks(vec![ Block::Name, Block::Date, diff --git a/src/flags/color.rs b/src/flags/color.rs index 3e7a127..eac9c3f 100644 --- a/src/flags/color.rs +++ b/src/flags/color.rs @@ -5,7 +5,7 @@ use super::Configurable; use crate::config_file::Config; -use clap::ArgMatches; +use clap::{ArgMatches, ValueSource}; use serde::de::{self, Deserializer, Visitor}; use serde::Deserialize; use std::env; @@ -115,10 +115,14 @@ impl Configurable<Self> for ColorOption { /// a [Some]. Otherwise if the argument is passed, this returns the variant corresponding to /// its parameter in a [Some]. Otherwise this returns [None]. fn from_arg_matches(matches: &ArgMatches) -> Option<Self> { - if matches.is_present("classic") { + if matches.get_one("classic") == Some(&true) { Some(Self::Never) - } else if matches.occurrences_of("color") > 0 { - matches.values_of("color")?.last().map(Self::from_arg_str) + } else if matches.value_source("color") == Some(ValueSource::CommandLine) { + matches + .get_many::<String>("color")? + .last() + .map(String::as_str) + .map(Self::from_arg_str) } else { None } @@ -159,14 +163,14 @@ mod test_color_option { #[test] fn test_from_arg_matches_none() { let argv = ["lsd"]; - let matches = app::build().get_matches_from_safe(argv).unwrap(); + let matches = app::build().try_get_matches_from(argv).unwrap(); assert_eq!(None, ColorOption::from_arg_matches(&matches)); } #[test] fn test_from_arg_matches_always() { let argv = ["lsd", "--color", "always"]; - let matches = app::build().get_matches_from_safe(argv).unwrap(); + let matches = app::build().try_get_matches_from(argv).unwrap(); assert_eq!( Some(ColorOption::Always), ColorOption::from_arg_matches(&matches) @@ -176,7 +180,7 @@ mod test_color_option { #[test] fn test_from_arg_matches_auto() { let argv = ["lsd", "--color", "auto"]; - let matches = app::build().get_matches_from_safe(argv).unwrap(); + let matches = app::build().try_get_matches_from(argv).unwrap(); assert_eq!( |