Skip to content

Commit

Permalink
move the CmdArgParser out to a separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
seamile committed Jan 17, 2023
1 parent 42fbcfb commit d2e3bf1
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 64 deletions.
65 changes: 65 additions & 0 deletions src/cmdargs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use std::path::PathBuf;
use std::process::exit;

use clap::Parser;
use num_cpus;

use crate::output as op;

#[derive(Parser)]
#[command(name = "fcnt")]
#[command(version = "0.2.1")]
#[command(about = "Count the total number of files in given directories.")]
pub struct CmdArgParser {
/// the directories (default: ./)
pub directories: Vec<String>,

/// count all regular and hidden files.
#[arg(short = 'a')]
pub all_files: bool,

/// count the total size of files.
#[arg(short = 's')]
pub with_size: bool,

/// non-recursive mode (files in sub-directories will be ignored).
#[arg(short = 'R')]
pub non_recursive: bool,

/// the number of threads for traversal (invalid in `non_recursive` mode).
#[arg(short = 't')]
pub n_thread: Option<usize>,
}

impl CmdArgParser {
pub fn get_threads_num(&self) -> usize {
match self.n_thread {
Some(num) => return num,
None => {
let n_cpu = num_cpus::get();
return if n_cpu >= 4 { n_cpu } else { 4 };
}
}
}

pub fn get_directories(&self) -> Vec<PathBuf> {
let mut directories: Vec<PathBuf> = vec![];
if self.directories.is_empty() {
directories.push(PathBuf::from("./"));
} else {
for dir in self.directories.iter().map(|p| PathBuf::from(p)) {
if dir.is_dir() {
directories.push(dir);
} else {
let msg = format!("{:?} is not a directory.", dir);
println!("{}", op::warn(&msg));
}
}
if directories.is_empty() {
println!("{}", op::err(&"fcnt: no directory found."));
exit(1);
}
}
return directories;
}
}
67 changes: 3 additions & 64 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
mod cmdargs;
mod output;
mod walker;

use std::path::PathBuf;
use std::process::exit;

use clap::Parser;
use num_cpus::get as cpu_count;

use cmdargs::CmdArgParser;
use walker::Counter;

fn main() {
// parse cmd-line args and get directories
let args = CmdLineArgs::parse();
let args = CmdArgParser::parse();

// walk all files
let directories = args.get_directories();
Expand All @@ -35,61 +32,3 @@ fn main() {

Counter::output(&counters, args.with_size);
}

#[derive(Parser)]
#[command(name = "fcnt")]
#[command(version = "0.2.1")]
#[command(about = "Count the total number of files in given directories.")]
struct CmdLineArgs {
/// the directories (default: ./)
directories: Vec<String>,

/// count all regular and hidden files.
#[arg(short = 'a')]
all_files: bool,

/// count the total size of files.
#[arg(short = 's')]
with_size: bool,

/// non-recursive mode (files in sub-directories will be ignored).
#[arg(short = 'R')]
non_recursive: bool,

/// the number of threads for traversal (invalid in `non_recursive` mode).
#[arg(short = 't')]
n_thread: Option<usize>,
}

impl CmdLineArgs {
pub fn get_threads_num(&self) -> usize {
match self.n_thread {
Some(num) => num,
None => {
let n_cpu = cpu_count();
return if n_cpu >= 4 { n_cpu } else { 4 };
}
}
}

pub fn get_directories(&self) -> Vec<PathBuf> {
let mut directories: Vec<PathBuf> = vec![];
if self.directories.is_empty() {
directories.push(PathBuf::from("./"));
} else {
for dir in self.directories.iter().map(|p| PathBuf::from(p)) {
if dir.is_dir() {
directories.push(dir);
} else {
let msg = format!("{:?} is not a directory.", dir);
println!("{}", output::warn(&msg));
}
}
if directories.is_empty() {
println!("{}", output::err(&"fcnt: no directory found."));
exit(1);
}
}
return directories;
}
}

0 comments on commit d2e3bf1

Please sign in to comment.