Allow config to be updated with CLI args

This commit is contained in:
Josh Megnauth 2024-02-15 23:38:23 -05:00
parent 99595eeeed
commit 028fd83296
No known key found for this signature in database
GPG key ID: 70813183462EFAD3
3 changed files with 120 additions and 15 deletions

View file

@ -4,7 +4,9 @@ use cosmic::{
cosmic_config::{self, cosmic_config_derive::CosmicConfigEntry, CosmicConfigEntry},
theme,
};
use lexopt::prelude::*;
use serde::{Deserialize, Serialize};
use std::{path::PathBuf, process};
use crate::wrappers::HWDeviceType;
@ -41,3 +43,52 @@ impl Default for Config {
}
}
}
impl Config {
pub fn with_args(&mut self, args: &mut Args) {
if let Some(decoder) = args.decoder {
self.hw_decoder = decoder;
}
}
}
pub struct Args {
pub paths: Vec<PathBuf>,
pub decoder: Option<HWDeviceType>,
}
impl Args {
pub fn parse_args() -> Result<Self, lexopt::Error> {
let mut paths = Vec::new();
let mut decoder = None;
let mut parser = lexopt::Parser::from_env();
while let Some(arg) = parser.next()? {
match arg {
Long("list-hwdec") => {
println!("Supported hardware decoders:");
for hwdec in HWDeviceType::supported_devices() {
println!("\t* [{}] {hwdec}", hwdec.short_name());
}
process::exit(0);
}
Long("hwdec") => {
decoder = Some(parser.value()?.parse()?);
}
Value(path) => {
let path = path.parse()?;
paths.push(path);
}
_ => return Err(arg.unexpected()),
}
}
if paths.is_empty() {
return Err(lexopt::Error::MissingValue {
option: Some("missing video path".into()),
});
}
Ok(Self { paths, decoder })
}
}