Skip to content

Instantly share code, notes, and snippets.

@barafael
Created August 11, 2023 08:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save barafael/372be7b1b3f3004d9b6613f49cf55be2 to your computer and use it in GitHub Desktop.
Save barafael/372be7b1b3f3004d9b6613f49cf55be2 to your computer and use it in GitHub Desktop.
clap+humantime+serde+figment
[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"
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