pay-respects/core/src/args.rs

140 lines
3.1 KiB
Rust
Raw Normal View History

2024-12-07 17:50:38 +01:00
use crate::shell::{initialization, Init};
2024-12-07 01:01:04 +01:00
use colored::Colorize;
2023-07-31 21:12:57 +02:00
2024-12-06 19:12:40 +01:00
pub enum Status {
Continue,
Exit, // version, help, etc.
Error,
}
pub fn handle_args(args: impl IntoIterator<Item = String>) -> Status {
let mut iter = args.into_iter().peekable();
let mut init = Init::new();
if let Some(binary_path) = iter.next() {
init.binary_path = binary_path;
}
if iter.peek().is_none() {
return Status::Continue;
2023-08-04 15:40:45 +02:00
}
while let Some(arg) = iter.next() {
match arg.as_str() {
2023-08-04 15:40:45 +02:00
"-h" | "--help" => {
print_help();
return Status::Exit;
2023-08-04 15:40:45 +02:00
}
2024-11-16 15:54:42 +01:00
"-v" | "--version" => {
print_version();
return Status::Exit;
2024-11-16 15:54:42 +01:00
}
2023-08-04 15:40:45 +02:00
"-a" | "--alias" => {
match iter.peek() {
Some(next_arg) if !next_arg.starts_with('-') => {
init.alias = next_arg.to_string();
iter.next();
2023-08-04 15:40:45 +02:00
}
_ => init.alias = String::from("f"),
2023-08-04 15:40:45 +02:00
}
2024-12-07 17:50:38 +01:00
init.auto_alias = true;
2023-08-04 15:40:45 +02:00
}
"--nocnf" => init.cnf = false,
_ => init.shell = arg,
2023-08-04 15:40:45 +02:00
}
}
2024-12-07 17:50:38 +01:00
if init.shell.is_empty() {
2024-09-25 17:55:55 +02:00
eprintln!("{}", t!("no-shell"));
return Status::Error;
}
2023-08-04 15:40:45 +02:00
2024-12-07 17:50:38 +01:00
initialization(&mut init);
Status::Exit
2023-08-04 15:40:45 +02:00
}
fn print_help() {
2024-09-25 17:55:55 +02:00
println!(
"{}",
t!(
"help",
2024-12-08 20:29:34 +01:00
usage = "pay-respects <shell> [--alias [<alias>]] [--nocnf]",
2024-12-07 01:01:04 +01:00
eval = "Bash / Zsh / Fish".bold(),
eval_examples = r#"
eval "$(pay-respects bash --alias)"
eval "$(pay-respects zsh --alias)"
pay-respects fish --alias | source
"#,
manual = "Nushell / PowerShell".bold(),
manual_examples = r#"
2024-12-11 17:48:07 +01:00
pay-respects nushell --alias
2024-12-09 16:21:49 +01:00
pay-respects pwsh --alias
2024-12-07 01:01:04 +01:00
"#
2024-09-25 17:55:55 +02:00
)
2023-08-04 15:40:45 +02:00
);
}
2024-11-16 15:54:42 +01:00
fn print_version() {
2024-11-16 17:36:16 +01:00
println!(
"version: {}",
option_env!("CARGO_PKG_VERSION").unwrap_or("unknown")
);
2024-12-12 16:38:42 +01:00
let lib = option_env!("_DEF_PR_LIB").map(|dir| dir.to_string());
2024-12-10 15:00:55 +01:00
if lib.is_some() {
2024-12-12 16:38:42 +01:00
println!("Default lib directory: {}", lib.unwrap());
2024-12-10 15:00:55 +01:00
}
2024-11-16 15:54:42 +01:00
}
#[cfg(test)]
mod tests {
use super::{handle_args, Status};
#[test]
fn test_handle_args() {
assert!(matches!(
handle_args([String::from("pay-respects")]),
Status::Continue
));
for args in [
[String::new(), String::from("-h")],
[String::new(), String::from("--help")],
[String::new(), String::from("-v")],
[String::new(), String::from("--version")],
[String::new(), String::from("zsh")],
] {
println!("Arguments {:?} should return Exit", args);
assert!(matches!(handle_args(args), Status::Exit));
}
for args in [
[String::new(), String::from("fish"), String::from("--alias")],
[String::new(), String::from("bash"), String::from("--nocnf")],
] {
println!("Arguments {:?} should return Exit", args);
assert!(matches!(handle_args(args), Status::Exit));
}
for args in [
[String::new(), String::from("-a")],
[String::new(), String::from("--alias")],
[String::new(), String::from("--nocnf")],
] {
println!("Arguments {:?} should return Error", args);
assert!(matches!(handle_args(args), Status::Error));
}
for args in [
[String::new(), String::from("-a"), String::from("--nocnf")],
[
String::new(),
String::from("--alias"),
String::from("--nocnf"),
],
] {
println!("Argument {:?} should return Error", args);
assert!(matches!(handle_args(args), Status::Error));
}
}
}