new arguments
parent
852f28e4b3
commit
60f0e550fa
79
src/main.rs
79
src/main.rs
|
@ -1,13 +1,15 @@
|
|||
#![feature(io_error_more, fs_try_exists)]
|
||||
|
||||
use clap::Parser;
|
||||
use clap::ArgAction;
|
||||
use clap::{Parser, ArgAction};
|
||||
|
||||
mod directory;
|
||||
mod unit;
|
||||
use directory::Directory;
|
||||
use unit::Unit;
|
||||
|
||||
use std::process::ExitCode;
|
||||
|
||||
#[allow(clippy::struct_excessive_bools)]
|
||||
#[derive(Parser, Debug, Clone)]
|
||||
pub struct Args {
|
||||
#[arg(
|
||||
|
@ -16,43 +18,61 @@ pub struct Args {
|
|||
long_help = "keep going if an error occurs (ex. unreadable subdirectories in a readable directory)"
|
||||
)]
|
||||
persistant: bool,
|
||||
|
||||
|
||||
#[arg(
|
||||
short, long,
|
||||
help = "minimize output",
|
||||
long_help = "like -t, but does not print \"total: \" before the summary or the newline after. It also surpresses all error messages",
|
||||
conflicts_with = "total_only",
|
||||
long_help = "print nothing but the total size for all directories, without a newline. Also supresses all error messages",
|
||||
conflicts_with = "total",
|
||||
default_value_t = false,
|
||||
)]
|
||||
minimal: bool,
|
||||
|
||||
#[arg(
|
||||
short='T', long,
|
||||
help = "display in tree",
|
||||
default_value_t = false,
|
||||
conflicts_with = "minimal",
|
||||
)]
|
||||
tree: bool,
|
||||
|
||||
#[arg(
|
||||
short, long,
|
||||
help = "only display the total size",
|
||||
help = "display the total size",
|
||||
conflicts_with = "minimal",
|
||||
default_value_t = false,
|
||||
)]
|
||||
total_only: bool,
|
||||
total: bool,
|
||||
|
||||
#[arg(
|
||||
short='2', long,
|
||||
help = "print sizes in powers of 1024",
|
||||
help = "alias for --unit 1024",
|
||||
default_value_t = false,
|
||||
conflicts_with = "si"
|
||||
conflicts_with_all = ["si","unit"],
|
||||
)]
|
||||
base_two: bool,
|
||||
|
||||
#[arg(
|
||||
short='0', long,
|
||||
help = "print sizes in powers of 1000",
|
||||
help = "alias for --unit 1000",
|
||||
default_value_t = false,
|
||||
conflicts_with = "base_two"
|
||||
conflicts_with_all = ["base_two","unit"],
|
||||
)]
|
||||
si: bool,
|
||||
|
||||
#[arg(
|
||||
short, long,
|
||||
help = "unit to print in",
|
||||
long_help = "printing unit (case insensitive): b = bytes, kb = kilobytes, ki = kibibytes, gb = gigabytes, gi = gibibytes, tb = terabytes, ti = tibibytes",
|
||||
value_parser = Unit::parse,
|
||||
default_value_t = Unit::Byte,
|
||||
conflicts_with_all = ["base_two","si"],
|
||||
)]
|
||||
unit: Unit,
|
||||
|
||||
#[arg(
|
||||
value_parser = validate_path,
|
||||
help = "directories to summate",
|
||||
help = "items to summate",
|
||||
action = ArgAction::Append,
|
||||
num_args = 1..
|
||||
)]
|
||||
|
@ -68,8 +88,14 @@ fn validate_path(s: &str) -> Result<String, String> {
|
|||
}
|
||||
|
||||
fn main() -> ExitCode {
|
||||
let args = Args::parse();
|
||||
let mut args = Args::parse();
|
||||
|
||||
if args.base_two {
|
||||
args.unit = Unit::Kibi;
|
||||
} else if args.si {
|
||||
args.unit = Unit::Kilo;
|
||||
}
|
||||
|
||||
let mut total = 0;
|
||||
for path in args.path {
|
||||
let dir_structure = match Directory::new(path) {
|
||||
|
@ -81,24 +107,33 @@ fn main() -> ExitCode {
|
|||
return ExitCode::FAILURE;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
total += dir_structure.size();
|
||||
|
||||
if !args.minimal {
|
||||
if args.total_only {
|
||||
println!("{}: {}", dir_structure.path().to_str().unwrap(), dir_structure.size());
|
||||
} else {
|
||||
print!("{}", dir_structure.display());
|
||||
}
|
||||
if args.minimal {
|
||||
// skip printing (this is a matter of indentation)
|
||||
continue;
|
||||
}
|
||||
|
||||
if args.tree {
|
||||
println!("{}", dir_structure.display(args.unit));
|
||||
} else {
|
||||
println!(
|
||||
"{}: {}",
|
||||
dir_structure.path().display(),
|
||||
dir_structure.scale(args.unit),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if args.total_only {
|
||||
let total = args.unit.convert(total);
|
||||
|
||||
if args.total {
|
||||
println!("total: {total}");
|
||||
}
|
||||
else if args.minimal {
|
||||
print!("{total}");
|
||||
}
|
||||
|
||||
|
||||
ExitCode::SUCCESS
|
||||
}
|
Loading…
Reference in New Issue