bin: Refactor binaries on Clap

This has been an idea that has been on my mind for a bit. My argument
parser was very complex and hard to maintain. Therefore, I've decided
to refactor them using the Clap crate, which should make this more
straight forward.
This commit is contained in:
Lux Aliaga 2023-08-21 13:34:01 -04:00
parent 52a830331c
commit a841ba974c
5 changed files with 340 additions and 163 deletions

215
Cargo.lock generated
View File

@ -13,6 +13,55 @@ dependencies = [
"version_check",
]
[[package]]
name = "anstream"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0ca84f3628370c59db74ee214b3263d58f9aadd9b4fe7e711fd87dc452b7f163"
dependencies = [
"anstyle",
"anstyle-parse",
"anstyle-query",
"anstyle-wincon",
"colorchoice",
"is-terminal",
"utf8parse",
]
[[package]]
name = "anstyle"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3a30da5c5f2d5e72842e00bcb57657162cdabef0931f40e2deb9b4140440cecd"
[[package]]
name = "anstyle-parse"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "938874ff5980b03a87c5524b3ae5b59cf99b1d6bc836848df7bc5ada9643c333"
dependencies = [
"utf8parse",
]
[[package]]
name = "anstyle-query"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5ca11d4be1bab0c8bc8734a9aa7bf4ee8316d462a08c6ac5052f888fef5b494b"
dependencies = [
"windows-sys 0.48.0",
]
[[package]]
name = "anstyle-wincon"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c677ab05e09154296dd37acecd46420c17b9713e8366facafa8fc0885167cf4c"
dependencies = [
"anstyle",
"windows-sys 0.48.0",
]
[[package]]
name = "anyhow"
version = "1.0.65"
@ -27,7 +76,7 @@ checksum = "76464446b8bc32758d7e88ee1a804d9914cd9b1cb264c029899680b0be29826f"
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn 1.0.99",
]
[[package]]
@ -36,7 +85,7 @@ version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
dependencies = [
"hermit-abi",
"hermit-abi 0.1.19",
"libc",
"winapi",
]
@ -59,6 +108,12 @@ version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "bitflags"
version = "2.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b4682ae6287fcf752ecaabbfcc7b6f9b72aa33933dc23a554d853aea8eea8635"
[[package]]
name = "block-buffer"
version = "0.10.3"
@ -80,6 +135,53 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "clap"
version = "4.3.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "03aef18ddf7d879c15ce20f04826ef8418101c7e528014c3eeea13321047dca3"
dependencies = [
"clap_builder",
"clap_derive",
"once_cell",
]
[[package]]
name = "clap_builder"
version = "4.3.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f8ce6fffb678c9b80a70b6b6de0aad31df727623a70fd9a842c30cd573e2fa98"
dependencies = [
"anstream",
"anstyle",
"clap_lex",
"strsim",
]
[[package]]
name = "clap_derive"
version = "4.3.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "54a9bb5758fc5dfe728d1019941681eccaf0cf8a4189b692a0ee2f2ecf90a050"
dependencies = [
"heck",
"proc-macro2",
"quote",
"syn 2.0.29",
]
[[package]]
name = "clap_lex"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2da6da31387c7e4ef160ffab6d5e7f00c42626fe39aea70a7b0f1773f7dd6c1b"
[[package]]
name = "colorchoice"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7"
[[package]]
name = "colored"
version = "2.0.0"
@ -166,6 +268,27 @@ version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0688c2a7f92e427f44895cd63841bff7b29f8d7a1648b9e7e07a4a365b2e1257"
[[package]]
name = "errno"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6b30f669a7961ef1631673d2766cc92f52d64f7ef354d4fe0ddfd30ed52f0f4f"
dependencies = [
"errno-dragonfly",
"libc",
"windows-sys 0.48.0",
]
[[package]]
name = "errno-dragonfly"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aa68f1b12764fab894d2755d2518754e71b4fd80ecfb822714a1206c2aab39bf"
dependencies = [
"cc",
"libc",
]
[[package]]
name = "filetime"
version = "0.2.20"
@ -223,6 +346,12 @@ dependencies = [
"ahash",
]
[[package]]
name = "heck"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8"
[[package]]
name = "hermit-abi"
version = "0.1.19"
@ -232,6 +361,23 @@ dependencies = [
"libc",
]
[[package]]
name = "hermit-abi"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "443144c8cdadd93ebf52ddb4056d257f5b52c04d3c804e657d19eb73fc33668b"
[[package]]
name = "is-terminal"
version = "0.4.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cb0889898416213fab133e1d33a0e5858a48177452750691bde3666d0fdbaf8b"
dependencies = [
"hermit-abi 0.3.2",
"rustix",
"windows-sys 0.48.0",
]
[[package]]
name = "itoa"
version = "1.0.3"
@ -257,9 +403,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "libc"
version = "0.2.132"
version = "0.2.147"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8371e4e5341c3a96db127eb2465ac681ced4c433e01dd0e938adbef26ba93ba5"
checksum = "b4668fb0ea861c1df094127ac5f1da3409a82116a4ba74fca2e58ef927159bb3"
[[package]]
name = "linked-hash-map"
@ -267,6 +413,12 @@ version = "0.5.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0717cef1bc8b636c6e1c1bbdefc09e6322da8a9321966e8928ef80d20f7f770f"
[[package]]
name = "linux-raw-sys"
version = "0.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "57bcfdad1b858c2db7c38303a6d2ad4dfaf5eb53dfeb0910128b2c26d6158503"
[[package]]
name = "lzma-sys"
version = "0.1.20"
@ -431,7 +583,7 @@ dependencies = [
"pest_meta",
"proc-macro2",
"quote",
"syn",
"syn 1.0.99",
]
[[package]]
@ -453,18 +605,18 @@ checksum = "6ac9a59f73473f1b8d852421e59e64809f025994837ef743615c6d0c5b305160"
[[package]]
name = "proc-macro2"
version = "1.0.43"
version = "1.0.66"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0a2ca2c61bc9f3d74d2886294ab7b9853abd9c1ad903a3ac7815c58989bb7bab"
checksum = "18fb31db3f9bddb2ea821cde30a9f70117e3f119938b5ee630b7403aa6e2ead9"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.21"
version = "1.0.33"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbe448f377a7d6961e30f5955f9b8d106c3f5e449d493ee1b125c1d43c2b5179"
checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae"
dependencies = [
"proc-macro2",
]
@ -475,7 +627,7 @@ version = "0.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fb5a58c1855b4b6819d59012155603f0b22ad30cad752600aadfcb695265519a"
dependencies = [
"bitflags",
"bitflags 1.3.2",
]
[[package]]
@ -502,7 +654,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "88073939a61e5b7680558e6be56b419e208420c2adb92be54921fa6b72283f1a"
dependencies = [
"base64",
"bitflags",
"bitflags 1.3.2",
"serde",
]
@ -516,6 +668,19 @@ dependencies = [
"ordered-multimap",
]
[[package]]
name = "rustix"
version = "0.38.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "19ed4fa021d81c8392ce04db050a3da9a60299050b7ae1cf482d862b54a7218f"
dependencies = [
"bitflags 2.4.0",
"errno",
"libc",
"linux-raw-sys",
"windows-sys 0.48.0",
]
[[package]]
name = "ryu"
version = "1.0.11"
@ -539,7 +704,7 @@ checksum = "81fa1584d3d1bcacd84c277a0dfe21f5b0f6accf4a23d04d4c6d61f1af522b4c"
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn 1.0.99",
]
[[package]]
@ -574,6 +739,12 @@ dependencies = [
"num",
]
[[package]]
name = "strsim"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
[[package]]
name = "syn"
version = "1.0.99"
@ -585,6 +756,17 @@ dependencies = [
"unicode-ident",
]
[[package]]
name = "syn"
version = "2.0.29"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c324c494eba9d92503e6f1ef2e6df781e78f6a7705a0202d9801b198807d518a"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "tar"
version = "0.4.38"
@ -613,7 +795,7 @@ checksum = "e8f2591983642de85c921015f3f070c665a197ed69e417af436115e3a1407487"
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn 1.0.99",
]
[[package]]
@ -643,11 +825,18 @@ version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c4f5b37a154999a8f3f98cc23a628d850e154479cd94decf3414696e12e31aaf"
[[package]]
name = "utf8parse"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a"
[[package]]
name = "vento"
version = "1.3.0-alpha"
dependencies = [
"anyhow",
"clap",
"colored",
"config",
"dirs",

View File

@ -24,6 +24,7 @@ size_format = "1.0.2"
config = "0.13"
xz2 = "0.1"
tar = "0.4"
clap = { version = "4.3.23", features = ["derive"] }
[build-dependencies]
man = "0.3.0"

View File

@ -18,63 +18,33 @@
*/
use anyhow::Result;
use std::env;
use std::path::Path;
use vento::{
common::get_current_dir,
common::override_color,
help, item,
message::{throw_error, ErrorType},
};
use clap::Parser;
use std::path::PathBuf;
use vento::{common::get_current_dir, item};
#[derive(Parser)]
#[command(name = "Drop")]
#[command(about = "A file dropper for Vento", long_about = None)]
#[command(author, version)]
struct Cli {
/// Pick a slot to drop the file from
#[arg(short, long)]
slot: Option<String>,
/// File to drop from inventory
file: String,
/// Location to drop file onto
output: Option<PathBuf>,
}
fn main() -> Result<()> {
// Handles args in Drop
override_color()?;
let args: Vec<String> = env::args().collect();
if args.len() >= 2 {
if args[1].contains("--slot=") {
// Checks if the user has provided the long argument "--slot="
match args.len() {
4 => item::drop(
&args[2],
&args[1].as_str().replace("--slot=", ""),
Path::new(&args[4]).to_path_buf(),
true,
)?,
3 => item::drop(
&args[2],
&args[1].as_str().replace("--slot=", ""),
get_current_dir()?,
true,
)?,
2 => throw_error(ErrorType::SpecifyFile)?,
_ => throw_error(ErrorType::TooManyArgs)?,
};
} else {
match args[1].as_str() {
"--help" | "-h" => help::drop()?,
"-s" => match args.len() {
5 => item::drop(&args[3], &args[2], Path::new(&args[4]).to_path_buf(), true)?,
4 => item::drop(&args[3], &args[2], get_current_dir()?, true)?,
3 => throw_error(ErrorType::SpecifyFile)?,
2 => throw_error(ErrorType::SpecifySlot)?,
_ => throw_error(ErrorType::TooManyArgs)?,
},
_ => match args.len() {
3 => item::drop(
&args[1],
&String::from("active"),
Path::new(&args[2]).to_path_buf(),
true,
)?,
2 => item::drop(&args[1], &String::from("active"), get_current_dir()?, true)?,
_ => throw_error(ErrorType::TooManyArgs)?,
},
}
}
} else {
// If the user provides no arguments, Drop will display the help message.
help::drop()?;
}
let cli = Cli::parse();
let unwrapped_slot = cli.slot.unwrap_or(String::from("active"));
let slot = unwrapped_slot.as_str();
let out = cli.output.unwrap_or(get_current_dir()?);
item::drop(&cli.file, slot, out, true)?;
Ok(())
}

View File

@ -18,43 +18,28 @@
*/
use anyhow::Result;
use std::env;
use vento::{
common::override_color,
help, item,
message::{throw_error, ErrorType},
};
use clap::Parser;
use vento::{common::override_color, item};
#[derive(Parser)]
#[command(name = "Take")]
#[command(about = "A file grabber for Vento", long_about = None)]
#[command(author, version)]
struct Cli {
/// Pick a slot to take the file into
#[arg(short, long)]
slot: Option<String>,
/// File to take
file: String,
}
fn main() -> Result<()> {
// Handles args in Vento
override_color()?;
let args: Vec<String> = env::args().collect();
if args.len() >= 2 {
if args[1].contains("--slot=") {
// Checks if the user has provided the long argument "--slot="
match args.len() {
3 => item::take(&args[2], &args[1].replace("--slot=", ""), true)?,
2 => throw_error(ErrorType::SpecifyFile)?,
_ => throw_error(ErrorType::TooManyArgs)?,
};
} else {
match args[1].as_str() {
"--help" | "-h" => help::take()?,
"-s" => match args.len() {
4 => item::take(&args[3], &args[2], true)?,
3 => throw_error(ErrorType::SpecifyFile)?,
2 => throw_error(ErrorType::SpecifySlot)?,
_ => throw_error(ErrorType::TooManyArgs)?,
},
_ => match args.len() {
2 => item::take(&args[1], &String::from("active"), true)?,
_ => throw_error(ErrorType::TooManyArgs)?,
},
}
}
} else {
// If the user provides no arguments, Take will display the help message.
help::take()?;
}
let cli = Cli::parse();
let slot = cli.slot.unwrap_or(String::from("active"));
item::take(&cli.file, &slot, true)?;
Ok(())
}

View File

@ -18,78 +18,110 @@
*/
use anyhow::Result;
use std::{env, path::PathBuf};
use clap::Parser;
use std::path::PathBuf;
use vento::{
archive,
common::override_color,
help, history, inv,
history, inv,
message::{throw_error, ErrorType},
};
#[derive(Parser)]
#[command(name = "Vento")]
#[command(author, version, about, long_about = None)]
struct Cli {
/// Pick slot to list
#[arg(short, long)]
slot: Option<String>,
/// Switch slots
#[arg(short = 'c', long)]
switch: bool,
/// Undo the last action
#[arg(short, long)]
undo: bool,
/// Export an inventory
#[arg(short, long, value_names = &["SLOT", "ARCHIVE"], num_args = ..=2)]
export_inv: Option<Vec<String>>,
/// Export the Vento directory
#[arg(short = 'E', long, default_missing_value = "vento.tar.xz", value_name = "ARCHIVE", num_args = ..=1)]
export_dir: Option<PathBuf>,
/// Import an inventory archive
#[arg(short = 'g', long, num_args = 1..=2, value_names = &["ARCHIVE", "SLOT"])]
import_inv: Option<Vec<String>>,
/// Import a Vento directory archive
#[arg(short = 'G', long, value_name = "ARCHIVE")]
import_dir: Option<PathBuf>,
/// Initialize Vento
#[arg(short, long)]
init: bool,
directory: Option<String>,
}
fn main() -> Result<()> {
// Handles args in Vento
override_color()?;
let args: Vec<String> = env::args().collect();
if args.len() >= 2 {
// If the vector for the arguments the command is taking is larger than 2, it most likely means the user has provided an argument
if args[1].contains("--slot=") {
// Checks if the user has provided the long argument "--slot="
match args.len() {
3 => inv::list(&args[1].replace("--slot=", ""), &args[2])?,
2 => inv::list(&args[1].replace("--slot=", ""), "")?,
_ => throw_error(ErrorType::TooManyArgs)?,
};
} else {
match args[1].as_str() {
"-h" | "--help" => help::vento()?,
"-i" | "--init" => inv::init()?,
"-c" | "--switch" => inv::switch(true)?,
"-u" | "--undo" => history::undo()?,
"-e" | "--export-inv" => match args.len() {
4 => archive::export_inv(&args[2], PathBuf::from(&args[3]), true)?,
3 => match args[2].as_str() {
"active" | "a" | "inactive" | "i" => {
let mut path = PathBuf::from(match args[2].as_str() {
"a" => "active",
"i" => "inactive",
_ => &args[2],
});
path.set_extension("tar.xz");
archive::export_inv(&args[2], path, true)?
}
_ => archive::export_inv("active", PathBuf::from(&args[2]), true)?,
let cli = Cli::parse();
let unwrapped_dir = cli.directory.unwrap_or(String::new());
let dir = unwrapped_dir.as_str();
if cli.switch {
inv::switch(true)?
} else if cli.undo {
history::undo()?
} else if cli.init {
inv::init()?
} else if cli.export_inv.is_some() {
let unwrapped_export_inv = cli.export_inv.unwrap();
let export_inv_values = match unwrapped_export_inv.len() {
0 => vec![String::from("active"), String::from("active.tar.xz")],
_ => unwrapped_export_inv,
};
archive::export_inv(
match export_inv_values[0].as_str() {
"active" | "inactive" | "a" | "i" => export_inv_values[0].as_str(),
_ => "active",
},
PathBuf::from(match export_inv_values[0].as_str() {
"active" | "inactive" | "a" | "i" => export_inv_values[1].as_str(),
_ => export_inv_values[0].as_str(),
}),
true,
)?
} else if cli.export_dir.is_some() {
archive::export_dir(cli.export_dir.unwrap(), true)?
} else if cli.import_inv.is_some() {
let import_inv_values = &cli
.import_inv
.unwrap_or(vec![String::new(), String::from("active")]);
match import_inv_values[0].as_str() {
"" | "active" | "inactive" | "a" | "i" => throw_error(ErrorType::SpecifyFile)?,
_ => archive::import_inv(
PathBuf::from(&import_inv_values[0]),
match import_inv_values.len() {
2 => match import_inv_values[1].as_str() {
"active" | "inactive" | "a" | "i" => import_inv_values[1].as_str(),
_ => "active",
},
2 => archive::export_inv("active", PathBuf::from("active.tar.xz"), true)?,
_ => throw_error(ErrorType::TooManyArgs)?,
_ => "active",
},
"-E" | "--export-dir" => match args.len() {
3 => archive::export_dir(PathBuf::from(&args[2]), true)?,
2 => archive::export_dir(PathBuf::from("vento.tar.xz"), true)?,
_ => throw_error(ErrorType::TooManyArgs)?,
},
"-g" | "--import-inv" => match args.len() {
4 => archive::import_inv(PathBuf::from(&args[2]), &args[3], true)?,
3 => archive::import_inv(PathBuf::from(&args[2]), "active", true)?,
2 => throw_error(ErrorType::SpecifyFile)?,
_ => throw_error(ErrorType::TooManyArgs)?,
},
"-G" | "--import-dir" => match args.len() {
3 => archive::import_dir(PathBuf::from(&args[2]), true)?,
2 => throw_error(ErrorType::SpecifyFile)?,
_ => throw_error(ErrorType::TooManyArgs)?,
},
"-s" => match args.len() {
4 => inv::list(&args[2], &args[3])?,
3 => inv::list(&args[2], "")?,
2 => throw_error(ErrorType::SpecifySlot)?,
_ => throw_error(ErrorType::TooManyArgs)?,
},
_ => inv::list("active", args[1].as_str())?,
}
}
true,
)?,
};
} else if cli.import_dir.is_some() {
archive::import_dir(cli.import_dir.unwrap(), true)?
} else {
// If the user provides no arguments, Vento will display the files in the active slot.
inv::list("active", "")?;
inv::list(cli.slot.unwrap_or(String::from("active")).as_str(), dir)?
}
Ok(())
}