Skip to content

Commit

Permalink
First version of options struct based on Argh
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Jul 1, 2020
1 parent 8040d5c commit d787a9c
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 0 deletions.
30 changes: 30 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ include = ["src/**/*", "Cargo.*", "LICENSE", "README.md", "CHANGELOG.md", "!**/*
failure = "0.1.1"
failure-tools = "4.0.2"
structopt = "0.3"
argh = "0.1.3"
jwalk = "0.5.0"
byte-unit = "4"
termion = "1.5.2"
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use tui_react::Terminal;

mod interactive;
mod options;
mod options_argh;

fn run() -> Result<(), Error> {
use options::Command::*;
Expand Down
130 changes: 130 additions & 0 deletions src/options_argh.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
use std::path::PathBuf;

use argh::{FromArgValue, FromArgs};
use dua::ByteFormat;

pub enum CliByteFormat {
Metric,
Binary,
Bytes,
GB,
GiB,
MB,
MiB,
}

impl FromArgValue for CliByteFormat {
fn from_arg_value(value: &str) -> Result<Self, String> {
use CliByteFormat::*;
let value_lc = value.to_ascii_lowercase();
Ok(match value_lc.as_str() {
"metric" => Metric,
"binary" => Binary,
"bytes" => Bytes,
"gb" => GB,
"gib" => GiB,
"mb" => MB,
"mib" => MiB,
_ => return Err(format!("Invalid byte format: {}", value)),
})
}
}

impl From<CliByteFormat> for ByteFormat {
fn from(input: CliByteFormat) -> Self {
use CliByteFormat::*;
match input {
Metric => ByteFormat::Metric,
Binary => ByteFormat::Binary,
Bytes => ByteFormat::Bytes,
GB => ByteFormat::GB,
GiB => ByteFormat::GiB,
MB => ByteFormat::MB,
MiB => ByteFormat::MiB,
}
}
}

/// a tool to learn about disk usage, fast!
#[derive(FromArgs)]
#[argh(name = "dua")]
pub struct Args {
#[argh(subcommand)]
pub command: Option<Command>,

/// the amount of threads to use. Defaults to the amount of logical processors.
/// Set to 1 to use only a single thread.
#[argh(option, short = 't')]
pub threads: Option<usize>,

/// the format with which to print byte counts.
/// Metric - uses 1000 as base (default)
/// Binary - uses 1024 as base
/// Bytes - plain bytes without any formatting
/// GB - only gigabytes
/// GiB - only gibibytes
/// MB - only megabytes
/// MiB - only mebibytes
#[argh(option, short = 'f')]
pub format: Option<CliByteFormat>,

/// display apparent size instead of disk usage.
#[argh(switch, short = 'A')]
pub apparent_size: bool,

/// count hard-linked files each time they are seen
#[argh(switch, short = 'l')]
pub count_hard_links: bool,

/// if set, we will not cross filesystems or traverse mount points
#[argh(switch, short = 'x')]
pub stay_on_filesystem: bool,

/// one or more input files or directories. If unset, we will use all entries in the current working directory.
#[argh(positional)]
pub input: Vec<PathBuf>,
}

#[derive(FromArgs)]
#[argh(subcommand)]
pub enum Command {
Interactive(Interactive),
InteractiveAlias(InteractiveAlias),
Aggregate(Aggregate),
}

/// Launch the terminal user interface
#[derive(FromArgs)]
#[argh(subcommand, name = "interactive")]
pub struct Interactive {
/// one or more input files or directories. If unset, we will use all entries in the current working directory.
#[argh(positional)]
input: Vec<PathBuf>,
}

/// Alias for 'interactive'
#[derive(FromArgs)]
#[argh(subcommand, name = "i")]
pub struct InteractiveAlias {
#[argh(positional)]
input: Vec<PathBuf>,
}

/// Aggregate the consumed space of one or more directories or files
#[derive(FromArgs)]
#[argh(subcommand, name = "aggregate")]
pub struct Aggregate {
/// if set, print additional statistics about the file traversal to stderr
#[argh(switch)]
stats: bool,
/// if set, paths will be printed in their order of occurrence on the command-line.
/// Otherwise they are sorted by their size in bytes, ascending.
#[argh(switch)]
no_sort: bool,
/// if set, no total column will be computed for multiple inputs
#[argh(switch)]
no_total: bool,
/// one or more input files or directories. If unset, we will use all entries in the current working directory.
#[argh(positional)]
input: Vec<PathBuf>,
}

0 comments on commit d787a9c

Please sign in to comment.