cli: add subcommand support

This commit is contained in:
Schrottkatze 2024-02-19 17:54:40 +01:00
parent 1e9648966f
commit 56ec11e143
Signed by: schrottkatze
SSH key fingerprint: SHA256:hXb3t1vINBFCiDCmhRABHX5ocdbLiKyCdKI4HK2Rbbc
3 changed files with 42 additions and 29 deletions

View file

@ -1,18 +1,11 @@
use std::path::PathBuf;
use clap::Parser;
use self::{
cli::Args,
config_file::{find_config_file, Configs},
};
use self::config_file::{find_config_file, Configs};
pub(crate) use cli::CliConfigs;
mod cli;
mod config_file;
/// this struct may hold all configuration
pub struct Config {
pub source: PathBuf,
pub evaluator: eval::Available,
pub startup_msg: bool,
@ -20,10 +13,9 @@ pub struct Config {
impl Config {
/// Get the configs from all possible places (args, file, env...)
pub fn read() -> Self {
let args = Args::parse();
let config = if let Some(config) = args.config_path {
Ok(config)
pub fn read(args: &CliConfigs) -> Self {
let config = if let Some(config) = &args.config_path {
Ok(config.clone())
} else {
find_config_file()
};
@ -42,7 +34,6 @@ impl Config {
if let Some(file) = config {
Self {
source: args.source,
evaluator: args.evaluator.and(file.evaluator).unwrap_or_default(),
// this is negated because to an outward api, the negative is more intuitive,
// while in the source the other way around is more intuitive
@ -50,7 +41,6 @@ impl Config {
}
} else {
Self {
source: args.source,
startup_msg: !args.no_startup_message,
evaluator: args.evaluator.unwrap_or_default(),
}