Adding clap for parameter parsing

This commit is contained in:
rzuasti
2026-01-27 09:21:00 -05:00
parent f7513c329e
commit 9a2227bb07
3 changed files with 75 additions and 5 deletions
Generated
+53
View File
@@ -222,6 +222,46 @@ dependencies = [
"windows-link",
]
[[package]]
name = "clap"
version = "4.5.54"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c6e6ff9dcd79cff5cd969a17a545d79e84ab086e444102a591e288a8aa3ce394"
dependencies = [
"clap_builder",
"clap_derive",
]
[[package]]
name = "clap_builder"
version = "4.5.54"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fa42cf4d2b7a41bc8f663a7cab4031ebafa1bf3875705bfaf8466dc60ab52c00"
dependencies = [
"anstream",
"anstyle",
"clap_lex",
"strsim",
]
[[package]]
name = "clap_derive"
version = "4.5.49"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2a0b5487afeab2deb2ff4e03a807ad1a03ac532ff5a2cee5d86884440c7f7671"
dependencies = [
"heck",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "clap_lex"
version = "0.7.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c3e64b0cc0439b12df2fa678eae89a1c56a529fd067a9115f7827f1fffd22b32"
[[package]]
name = "cloudabi"
version = "0.0.3"
@@ -726,6 +766,12 @@ dependencies = [
"hashbrown 0.16.1",
]
[[package]]
name = "heck"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
[[package]]
name = "hermit-abi"
version = "0.5.2"
@@ -1291,6 +1337,7 @@ name = "oott"
version = "0.1.0"
dependencies = [
"chrono",
"clap",
"config",
"duration-string",
"env_logger",
@@ -2066,6 +2113,12 @@ version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6ce2be8dc25455e1f91df71bfa12ad37d7af1092ae736f3a6cd0e37bc7810596"
[[package]]
name = "strsim"
version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
[[package]]
name = "syn"
version = "2.0.114"
+1
View File
@@ -18,3 +18,4 @@ pushover = "0.4.0"
config = "0.15.19"
lazy_static = "1.5.0"
duration-string = { version="0.5.3", features = ["serde"] }
clap = {version="4.5.54", features=["derive"]}
+21 -5
View File
@@ -1,7 +1,8 @@
use clap::Parser;
use config::{Config, ConfigError, File};
use duration_string::DurationString;
use lazy_static::lazy_static;
use log::error;
use log::{error, info};
use serde::Deserialize;
// -----------------------------------------------------------
@@ -47,12 +48,27 @@ pub struct Settings {
// End configuration structure
// -----------------------------------------------------------
const CONFIG_FILE_PATH: &str = "./oott.toml";
// Command line parameters relevant to configuration
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
struct Args {
/// Config file path
#[arg(short, long)]
config: Option<String>,
}
const DEFAULT_CONFIG_FILE_PATH: &str = "./oott.toml";
impl Settings {
pub fn new() -> Result<Self, ConfigError> {
let args = Args::parse();
let config_path = args.config.unwrap_or(DEFAULT_CONFIG_FILE_PATH.to_string());
info!("Reading configuration from {}", config_path);
let local_settings = Config::builder()
.add_source(File::with_name(CONFIG_FILE_PATH))
.add_source(File::with_name(config_path.as_str()))
.build()?;
local_settings.try_deserialize()
@@ -63,8 +79,8 @@ lazy_static! {
pub static ref CONFIG: Settings = match Settings::new() {
Ok(value) => value,
Err(error) => {
error!("Error reading configuration file ({CONFIG_FILE_PATH}): {error}");
panic!("Error reading configuration file ({CONFIG_FILE_PATH}): {error}");
error!("Error reading configuration file: {error}");
panic!("Error reading configuration file: {error}");
}
};
}