boilerplate for rust CLI apps
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
cli-app-base/examples/minimal.rs

48 lines
1.1 KiB

//! This is a minimal example with a config file and logging
#[macro_use]
extern crate serde_derive;
#[macro_use]
extern crate log;
use clappconfig::AppConfig; // Must be in scope so the trait methods may be used
#[derive(Serialize, Deserialize, Debug, Default)]
#[serde(default)]
struct Config {
foo: u32,
bar: u32,
}
impl AppConfig for Config {
// We want init() to just return the config struct
type Init = Config;
/// Logging - we do not have a config option for this, so just return a default value
fn logging(&self) -> &str {
"info"
}
/// Finalize config (this is called inside `init()`)
fn configure<'a>(self, _clap: &clap::ArgMatches<'a>) -> anyhow::Result<Self::Init> {
Ok(self)
}
}
fn main() -> anyhow::Result<()> {
let cfg = Config::init(
"Foo Example",
"examples/foo.json",
env!("CARGO_PKG_VERSION"),
)?;
trace!("Trace message");
debug!("Debug message");
info!("Info message");
warn!("Warn message");
error!("Error message");
println!("Welcome to foo, config is:\n{:#?}", cfg);
Ok(())
}