Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ pub struct Cli {
#[arg(short = 'R', long, conflicts_with = "tree")]
pub recursive: bool,

/// For ls compatibility purposes ONLY, currently set by default
#[arg(short, long)]
human_readable: bool,
/// Display size in a human readable format, equivalent to `--size default`
#[arg(short, long, overrides_with = "size")]
pub human_readable: bool,

/// Recurse into directories and present the result as a tree
#[arg(long)]
Expand All @@ -77,7 +77,7 @@ pub struct Cli {
pub permission: Option<String>,

/// How to display size [default: default]
#[arg(long, value_name = "MODE", value_parser = ["default", "short", "bytes"])]
#[arg(long, value_name = "MODE", value_parser = ["default", "short", "bytes"], overrides_with = "human_readable")]
pub size: Option<String>,

/// Display the total size of directories
Expand Down Expand Up @@ -199,7 +199,7 @@ pub struct Cli {

/// Print help information
#[arg(long, action = ArgAction::Help)]
help: (),
pub help: (),
}

fn validate_date_argument(arg: &str) -> Result<String, String> {
Expand Down
33 changes: 33 additions & 0 deletions src/flags/size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,14 @@ impl Configurable<Self> for SizeFlag {
/// If any of the "default", "short" or "bytes" arguments is passed, the corresponding
/// `SizeFlag` variant is returned in a [Some]. If neither of them is passed, this returns
/// [None].
///
/// The `--human-readable` flag is equivalent to `--size default`. It and `--size` override
/// each other, so whichever is passed last on the command line wins.
fn from_cli(cli: &Cli) -> Option<Self> {
if cli.classic {
Some(Self::Bytes)
} else if cli.human_readable {
Some(Self::Default)
} else {
cli.size.as_deref().map(Self::from_arg_str)
}
Expand Down Expand Up @@ -117,6 +122,34 @@ mod test {
assert_eq!(Some(SizeFlag::Short), SizeFlag::from_cli(&cli));
}

#[test]
fn test_from_cli_human_readable() {
let argv = ["lsd", "--human-readable"];
let cli = Cli::try_parse_from(argv).unwrap();
assert_eq!(Some(SizeFlag::Default), SizeFlag::from_cli(&cli));
}

#[test]
fn test_from_cli_human_readable_overrides_size() {
let argv = ["lsd", "--size", "bytes", "-h"];
let cli = Cli::try_parse_from(argv).unwrap();
assert_eq!(Some(SizeFlag::Default), SizeFlag::from_cli(&cli));
}

#[test]
fn test_from_cli_size_overrides_human_readable() {
let argv = ["lsd", "-h", "--size", "bytes"];
let cli = Cli::try_parse_from(argv).unwrap();
assert_eq!(Some(SizeFlag::Bytes), SizeFlag::from_cli(&cli));
}

#[test]
fn test_from_cli_human_readable_classic() {
let argv = ["lsd", "-h", "--classic"];
let cli = Cli::try_parse_from(argv).unwrap();
assert_eq!(Some(SizeFlag::Bytes), SizeFlag::from_cli(&cli));
}

#[test]
fn test_from_cli_size_classic() {
let argv = ["lsd", "--size", "short", "--classic"];
Expand Down