Skip to content

Commit

Permalink
Use clap for command parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
lukstei committed Feb 23, 2022
1 parent 2d7bd9c commit 0fc4e34
Show file tree
Hide file tree
Showing 5 changed files with 259 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/target
disk-report.iml
.idea
229 changes: 229 additions & 0 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
itertools = "0.10.3"
itertools = "0.10.3"
clap = { version = "3.1.1", features = ["derive"] }
17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
# disk-report-rust

A simple utility for displaying the relative size of the contents of a directory tree

## Usage

`disk-report <directory>`
```
An application for displaying the relative size of the file contents of a directory tree
USAGE:
disk-report [DIRECTORY]
ARGS:
<DIRECTORY> Directory to scan [default: .]
OPTIONS:
-h, --help Print help information
-V, --version Print version information
```

## Example

Expand Down Expand Up @@ -46,3 +56,4 @@ aoc-2021 - 100%, 773MB total / 0MB own
build - 2%, 14MB total / 0MB own
js - 3%, 27MB total / 0MB own```
node_modules - 3%, 27MB total / 0MB own
```
21 changes: 13 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,22 @@ fn format_tree(t: &Tree, depth: i32, total_size: u64) -> String {
)
}

use clap::Parser;

/// An application for displaying the relative size of the file contents of a directory tree
#[derive(Parser, Debug)]
#[clap(author, version, about, long_about = None)]
struct Args {
/// Directory to scan
#[clap(default_value = ".")]
directory: String,
}

fn main() -> Result<(), Error> {
let mut args: Vec<String> = std::env::args().collect();
if args.is_empty() {
let program = args.pop().unwrap();
eprintln!("Usage: {} <path>", program);
std::process::exit(1);
}
let path = args.pop().unwrap();
let args = Args::parse();

let mut file_count = 0;
let path = fs::canonicalize(&path).map_err(Error::IO)?;
let path = fs::canonicalize(&args.directory).map_err(Error::IO)?;

println!("Disk report for {}\n", path.to_str().unwrap_or("??"));
let tree = read_tree(path, &mut file_count)?;
Expand Down

0 comments on commit 0fc4e34

Please sign in to comment.