Skip to content

Commit

Permalink
feat(cli): added base cli options
Browse files Browse the repository at this point in the history
  • Loading branch information
SalOne22 committed Feb 26, 2024
1 parent 508a63b commit a22a0aa
Show file tree
Hide file tree
Showing 7 changed files with 242 additions and 1 deletion.
169 changes: 169 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@ codegen-units = 1
opt-level = "z"

[dependencies]
clap = { version = "4.5.1", features = ["cargo", "string"] }
indoc = "2.0.4"
55 changes: 55 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use std::path::PathBuf;

use clap::{arg, command, value_parser, Command};
use indoc::indoc;

use crate::utils::threads;

pub fn cli() -> Command {
command!()
.arg_required_else_help(true)
.arg(
arg!([FILES] ... "Input file(s) to process")
.long_help(indoc! {"Input file(s) to process
If the file path contains spaces, enclose the path with double quotation marks on both sides."})
.value_parser(value_parser!(PathBuf))
.global(true)
.last(true),
)
.arg(
arg!(-d --directory <DIR> "The directory to write output file(s) to")
.long_help(indoc! {"The directory to write output file(s) to
Output files will be written without preserving the folder structure unless the --recursive flag is used."})
.value_parser(value_parser!(PathBuf)),
)
.arg(
arg!(-r --recursive "Preserves the folder structure when writing output file(s)")
.long_help(indoc! {"Preserves the folder structure when writing output file(s).
This option should be used in conjunction with the --directory option."})
.requires("directory"),
)
.arg(
arg!(-s --suffix [SUFFIX] "Adds the '@suffix' to the names of output file(s)")
.long_help(indoc! {"Adds the '@suffix' to the names of output file(s)
When '2x' is provided as the value, the resulting files will be renamed with the '@2x' suffix.
For example, a file named 'file.jpeg' will become '[email protected]'.
If no suffix is provided, the default updated suffix '@updated' will be added to the resulting files."})
.default_missing_value("updated"),
)
.arg(
arg!(-b --backup "Adds the '@backup' to the names of input file(s)")
)
.arg(
arg!(-t --threads <NUM> "The number of threads for concurrent processing")
.long_help(indoc! {"The number of threads for concurrent processing
Usage of multiple threads can speed up the execution of tasks, especially on multi-core processors.
By default, the number of available threads is utilized"})
.value_parser(value_parser!(u8).range(1..=threads::num_threads() as i64)),
)
}
Empty file removed src/lib.rs
Empty file.
9 changes: 8 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
fn main() {}
use cli::cli;

mod cli;
mod utils;

fn main() {
let matches = cli().get_matches();
}
1 change: 1 addition & 0 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod threads;
7 changes: 7 additions & 0 deletions src/utils/threads.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use std::{num::NonZeroUsize, thread};

pub fn num_threads() -> usize {
thread::available_parallelism()
.unwrap_or(NonZeroUsize::new(4).unwrap())
.get()
}

0 comments on commit a22a0aa

Please sign in to comment.