Created
August 11, 2023 08:16
-
-
Save barafael/372be7b1b3f3004d9b6613f49cf55be2 to your computer and use it in GitHub Desktop.
clap+humantime+serde+figment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[package] | |
name = "clapment" | |
version = "0.1.0" | |
edition = "2021" | |
[dependencies] | |
anyhow = "1.0.72" | |
clap = { version = "4.3.21", features = ["derive"] } | |
figment = { version = "0.10.10", features = ["env", "json", "toml"] } | |
humantime = "2.1.0" | |
serde = { version = "1.0.183", features = ["derive"] } | |
serde_with = "3.2.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use clap::Parser; | |
use figment::{ | |
providers::{Env, Serialized}, | |
Figment, | |
}; | |
use serde::{Deserialize, Serialize}; | |
use serde_with::{serde_as, DisplayFromStr}; | |
use std::{path::PathBuf, time::Duration}; | |
#[serde_as] | |
#[derive(Parser, Debug, Serialize, Deserialize)] | |
struct Arguments { | |
/// Some config | |
#[clap(default_value = "config.json")] | |
config: PathBuf, | |
/// How long | |
#[clap(short, long, default_value_t = Duration::from_secs(1).into())] | |
#[serde_as(as = "DisplayFromStr")] | |
duration: humantime::Duration, | |
} | |
fn main() -> anyhow::Result<()> { | |
let config: Arguments = Figment::new() | |
.merge(Serialized::defaults(Arguments::parse())) | |
.merge(Env::prefixed("CLAPMENT_")) | |
.extract()?; | |
dbg!(config); | |
Ok(()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment