Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d787a9c

Browse files
committed
First version of options struct based on Argh
1 parent 8040d5c commit d787a9c

4 files changed

Lines changed: 162 additions & 0 deletions

File tree

Cargo.lock

Lines changed: 30 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ include = ["src/**/*", "Cargo.*", "LICENSE", "README.md", "CHANGELOG.md", "!**/*
1313
failure = "0.1.1"
1414
failure-tools = "4.0.2"
1515
structopt = "0.3"
16+
argh = "0.1.3"
1617
jwalk = "0.5.0"
1718
byte-unit = "4"
1819
termion = "1.5.2"

src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use tui_react::Terminal;
1616

1717
mod interactive;
1818
mod options;
19+
mod options_argh;
1920

2021
fn run() -> Result<(), Error> {
2122
use options::Command::*;

src/options_argh.rs

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
use std::path::PathBuf;
2+
3+
use argh::{FromArgValue, FromArgs};
4+
use dua::ByteFormat;
5+
6+
pub enum CliByteFormat {
7+
Metric,
8+
Binary,
9+
Bytes,
10+
GB,
11+
GiB,
12+
MB,
13+
MiB,
14+
}
15+
16+
impl FromArgValue for CliByteFormat {
17+
fn from_arg_value(value: &str) -> Result<Self, String> {
18+
use CliByteFormat::*;
19+
let value_lc = value.to_ascii_lowercase();
20+
Ok(match value_lc.as_str() {
21+
"metric" => Metric,
22+
"binary" => Binary,
23+
"bytes" => Bytes,
24+
"gb" => GB,
25+
"gib" => GiB,
26+
"mb" => MB,
27+
"mib" => MiB,
28+
_ => return Err(format!("Invalid byte format: {}", value)),
29+
})
30+
}
31+
}
32+
33+
impl From<CliByteFormat> for ByteFormat {
34+
fn from(input: CliByteFormat) -> Self {
35+
use CliByteFormat::*;
36+
match input {
37+
Metric => ByteFormat::Metric,
38+
Binary => ByteFormat::Binary,
39+
Bytes => ByteFormat::Bytes,
40+
GB => ByteFormat::GB,
41+
GiB => ByteFormat::GiB,
42+
MB => ByteFormat::MB,
43+
MiB => ByteFormat::MiB,
44+
}
45+
}
46+
}
47+
48+
/// a tool to learn about disk usage, fast!
49+
#[derive(FromArgs)]
50+
#[argh(name = "dua")]
51+
pub struct Args {
52+
#[argh(subcommand)]
53+
pub command: Option<Command>,
54+
55+
/// the amount of threads to use. Defaults to the amount of logical processors.
56+
/// Set to 1 to use only a single thread.
57+
#[argh(option, short = 't')]
58+
pub threads: Option<usize>,
59+
60+
/// the format with which to print byte counts.
61+
/// Metric - uses 1000 as base (default)
62+
/// Binary - uses 1024 as base
63+
/// Bytes - plain bytes without any formatting
64+
/// GB - only gigabytes
65+
/// GiB - only gibibytes
66+
/// MB - only megabytes
67+
/// MiB - only mebibytes
68+
#[argh(option, short = 'f')]
69+
pub format: Option<CliByteFormat>,
70+
71+
/// display apparent size instead of disk usage.
72+
#[argh(switch, short = 'A')]
73+
pub apparent_size: bool,
74+
75+
/// count hard-linked files each time they are seen
76+
#[argh(switch, short = 'l')]
77+
pub count_hard_links: bool,
78+
79+
/// if set, we will not cross filesystems or traverse mount points
80+
#[argh(switch, short = 'x')]
81+
pub stay_on_filesystem: bool,
82+
83+
/// one or more input files or directories. If unset, we will use all entries in the current working directory.
84+
#[argh(positional)]
85+
pub input: Vec<PathBuf>,
86+
}
87+
88+
#[derive(FromArgs)]
89+
#[argh(subcommand)]
90+
pub enum Command {
91+
Interactive(Interactive),
92+
InteractiveAlias(InteractiveAlias),
93+
Aggregate(Aggregate),
94+
}
95+
96+
/// Launch the terminal user interface
97+
#[derive(FromArgs)]
98+
#[argh(subcommand, name = "interactive")]
99+
pub struct Interactive {
100+
/// one or more input files or directories. If unset, we will use all entries in the current working directory.
101+
#[argh(positional)]
102+
input: Vec<PathBuf>,
103+
}
104+
105+
/// Alias for 'interactive'
106+
#[derive(FromArgs)]
107+
#[argh(subcommand, name = "i")]
108+
pub struct InteractiveAlias {
109+
#[argh(positional)]
110+
input: Vec<PathBuf>,
111+
}
112+
113+
/// Aggregate the consumed space of one or more directories or files
114+
#[derive(FromArgs)]
115+
#[argh(subcommand, name = "aggregate")]
116+
pub struct Aggregate {
117+
/// if set, print additional statistics about the file traversal to stderr
118+
#[argh(switch)]
119+
stats: bool,
120+
/// if set, paths will be printed in their order of occurrence on the command-line.
121+
/// Otherwise they are sorted by their size in bytes, ascending.
122+
#[argh(switch)]
123+
no_sort: bool,
124+
/// if set, no total column will be computed for multiple inputs
125+
#[argh(switch)]
126+
no_total: bool,
127+
/// one or more input files or directories. If unset, we will use all entries in the current working directory.
128+
#[argh(positional)]
129+
input: Vec<PathBuf>,
130+
}

0 commit comments

Comments
 (0)