168 lines
4.0 KiB
Rust
168 lines
4.0 KiB
Rust
use std::env;
|
|||
|
|
|
||
|
|
#[derive(Debug, PartialEq, Eq)]
|
||
|
|
pub struct CliConfig {
|
||
|
|
pub work_mins: u32,
|
||
|
|
pub break_mins: u32,
|
||
|
|
pub enable_notifications: bool,
|
||
|
|
pub enable_visualizer: bool,
|
||
|
|
}
|
||
|
|
|
||
|
|
#[derive(Debug, PartialEq, Eq)]
|
||
|
|
pub enum CliAction {
|
||
|
|
Run(CliConfig),
|
||
|
|
PrintHelp,
|
||
|
|
PrintVersion,
|
||
|
|
Error(String),
|
||
|
|
}
|
||
|
|
|
||
|
|
pub struct Cli;
|
||
|
|
|
||
|
|
impl Cli {
|
||
|
|
pub fn parse() -> CliAction {
|
||
|
|
Self::parse_args(env::args().collect())
|
||
|
|
}
|
||
|
|
|
||
|
|
pub fn parse_args(args: Vec<String>) -> CliAction {
|
||
|
|
let mut enable_notifications = false;
|
||
|
|
let mut enable_visualizer = false;
|
||
|
|
let mut positional = Vec::new();
|
||
|
|
|
||
|
|
for arg in args.into_iter().skip(1) {
|
||
|
|
match arg.as_str() {
|
||
|
|
"-h" | "--help" => return CliAction::PrintHelp,
|
||
|
|
"-v" | "--version" => return CliAction::PrintVersion,
|
||
|
|
"-n" | "--notify" => enable_notifications = true,
|
||
|
|
"-z" | "--visualizer" => enable_visualizer = true,
|
||
|
|
"--" => {}
|
||
|
|
s if s.starts_with('-') => {
|
||
|
|
return CliAction::Error(format!("Unknown option: '{}'", s));
|
||
|
|
}
|
||
|
|
s => positional.push(s.to_string()),
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
let numbers: Vec<u32> = positional
|
||
|
|
.iter()
|
||
|
|
.filter_map(|s| s.parse::<u32>().ok())
|
||
|
|
.collect();
|
||
|
|
|
||
|
|
let work_mins = numbers.first().copied().unwrap_or(25);
|
||
|
|
let break_mins = numbers.get(1).copied().unwrap_or(5);
|
||
|
|
|
||
|
|
if numbers.len() > 2 {
|
||
|
|
return CliAction::Error(format!("Unexpected argument: '{}'", positional[2]));
|
||
|
|
}
|
||
|
|
|
||
|
|
CliAction::Run(CliConfig {
|
||
|
|
work_mins,
|
||
|
|
break_mins,
|
||
|
|
enable_notifications,
|
||
|
|
enable_visualizer,
|
||
|
|
})
|
||
|
|
}
|
||
|
|
|
||
|
|
pub fn print_help() {
|
||
|
|
println!("Usage: termato [options] [work_minutes] [break_minutes]");
|
||
|
|
println!();
|
||
|
|
println!("Options:");
|
||
|
|
println!(" -n, --notify Enable desktop notifications");
|
||
|
|
println!(" -z, --visualizer Display visualizer of playing audio");
|
||
|
|
println!(" -h, --help Print this help message");
|
||
|
|
println!(" -v, --version Print the version number");
|
||
|
|
println!();
|
||
|
|
println!("Defaults: work_minutes = 25, break_minutes = 5");
|
||
|
|
}
|
||
|
|
|
||
|
|
pub fn print_version() {
|
||
|
|
println!("termato version {}", env!("CARGO_PKG_VERSION"));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#[cfg(test)]
|
||
|
|
mod tests {
|
||
|
|
use super::*;
|
||
|
|
|
||
|
|
#[test]
|
||
|
|
fn test_defaults() {
|
||
|
|
let args = vec!["termato".to_string()];
|
||
|
|
assert_eq!(
|
||
|
|
Cli::parse_args(args),
|
||
|
|
CliAction::Run(CliConfig {
|
||
|
|
work_mins: 25,
|
||
|
|
break_mins: 5,
|
||
|
|
enable_notifications: false,
|
||
|
|
enable_visualizer: false,
|
||
|
|
})
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
#[test]
|
||
|
|
fn test_cargo_run_syntax_with_dot_and_dash_dash() {
|
||
|
|
let args = vec![
|
||
|
|
"target/debug/termato.exe".to_string(),
|
||
|
|
".".to_string(),
|
||
|
|
"--".to_string(),
|
||
|
|
"-z".to_string(),
|
||
|
|
];
|
||
|
|
assert_eq!(
|
||
|
|
Cli::parse_args(args),
|
||
|
|
CliAction::Run(CliConfig {
|
||
|
|
work_mins: 25,
|
||
|
|
break_mins: 5,
|
||
|
|
enable_notifications: false,
|
||
|
|
enable_visualizer: true,
|
||
|
|
})
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
#[test]
|
||
|
|
fn test_custom_time_and_flags() {
|
||
|
|
let args = vec![
|
||
|
|
"termato".to_string(),
|
||
|
|
"-n".to_string(),
|
||
|
|
"-z".to_string(),
|
||
|
|
"50".to_string(),
|
||
|
|
"10".to_string(),
|
||
|
|
];
|
||
|
|
assert_eq!(
|
||
|
|
Cli::parse_args(args),
|
||
|
|
CliAction::Run(CliConfig {
|
||
|
|
work_mins: 50,
|
||
|
|
break_mins: 10,
|
||
|
|
enable_notifications: true,
|
||
|
|
enable_visualizer: true,
|
||
|
|
})
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
#[test]
|
||
|
|
fn test_help_flag() {
|
||
|
|
let args = vec!["termato".to_string(), "--help".to_string()];
|
||
|
|
assert_eq!(Cli::parse_args(args), CliAction::PrintHelp);
|
||
|
|
}
|
||
|
|
|
||
|
|
#[test]
|
||
|
|
fn test_unknown_flag() {
|
||
|
|
let args = vec!["termato".to_string(), "--foo".to_string()];
|
||
|
|
assert_eq!(
|
||
|
|
Cli::parse_args(args),
|
||
|
|
CliAction::Error("Unknown option: '--foo'".to_string())
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
#[test]
|
||
|
|
fn test_non_numeric_positional_defaults() {
|
||
|
|
let args = vec!["termato".to_string(), "abc".to_string()];
|
||
|
|
assert_eq!(
|
||
|
|
Cli::parse_args(args),
|
||
|
|
CliAction::Run(CliConfig {
|
||
|
|
work_mins: 25,
|
||
|
|
break_mins: 5,
|
||
|
|
enable_notifications: false,
|
||
|
|
enable_visualizer: false,
|
||
|
|
})
|
||
|
|
);
|
||
|
|
}
|
||
|
|
}
|