Struct spirit_cfg_helpers::CfgDump
source · pub struct CfgDump { /* private fields */ }
Expand description
A command line fragment add --dump-config
to allow showing loaded configuration.
When this is added into the command line options structure, the --dump-config
and
--dump-config-as
options are added.
These dump the current configuration and exit.
In case the configuration is collected over multiple configuration files, directories and possibly environment variables and command line overrides, it is not always clear what exact configuration is actually used. This allows the user to query the actual configuration the application would use.
The fragment can be either used manually with the dump
method or
automatically by registering its helper
.
Requirements
For this to work, the configuration structure must implement Serialize
. This is not
mandated by Spirit
itself. However, all the fragments provided by spirit
crates implement it. For custom structures it is often sufficient to stick
#[derive(Serialize)]
onto them.
Examples
use serde_derive::{Deserialize, Serialize};
use spirit::Spirit;
use spirit_cfg_helpers::CfgDump;
use structopt::StructOpt;
#[derive(Default, Deserialize, Serialize)]
struct Cfg {
option: Option<String>,
}
#[derive(Debug, StructOpt)]
struct Opts {
#[structopt(flatten)]
dump: CfgDump,
}
impl Opts {
fn dump(&self) -> &CfgDump {
&self.dump
}
}
fn main() {
Spirit::<Opts, Cfg>::new()
.with(CfgDump::helper(Opts::dump))
.run(|_| Ok(()));
}
Implementations§
source§impl CfgDump
impl CfgDump
sourcepub fn dump<C: Serialize>(&self, cfg: &C)
pub fn dump<C: Serialize>(&self, cfg: &C)
Dump configuration if it is asked for in the options.
If the parsed options specify to dump the configuration, this does so and exits. If the options don’t specify that, it does nothing.
This can be used manually. However, the common way is to register the
helper
within a Builder
and let it do everything automatically.
sourcepub fn helper<O, C, F>(extract: F) -> impl Helper<O, C>where
F: FnOnce(&O) -> &Self + Send + 'static,
O: Debug + StructOpt + Send + Sync + 'static,
C: DeserializeOwned + Serialize + Send + Sync + 'static,
pub fn helper<O, C, F>(extract: F) -> impl Helper<O, C>where
F: FnOnce(&O) -> &Self + Send + 'static,
O: Debug + StructOpt + Send + Sync + 'static,
C: DeserializeOwned + Serialize + Send + Sync + 'static,
A helper that can be registered with Builder::with
.
The parameter is an extractor, a function that takes the whole command line options
structure and returns a reference to just the CfgDump
instance in there.
Note that for configuration to be dumped, it needs to be parsed first.
Also, as this exits if the dumping is requested, it makes some sense to register it sooner
than later. It registers itself as a config_validator
and it
is not needed to validate parts of the configuration only to throw it out on the exit.
Trait Implementations§
source§impl StructOpt for CfgDump
impl StructOpt for CfgDump
source§fn from_clap(matches: &ArgMatches<'_>) -> Self
fn from_clap(matches: &ArgMatches<'_>) -> Self
clap::ArgMatches
. It cannot fail
with a parameter generated by clap
by construction. Read moresource§fn from_args() -> Selfwhere
Self: Sized,
fn from_args() -> Selfwhere
Self: Sized,
source§fn from_iter<I>(iter: I) -> Selfwhere
Self: Sized,
I: IntoIterator,
<I as IntoIterator>::Item: Into<OsString> + Clone,
fn from_iter<I>(iter: I) -> Selfwhere
Self: Sized,
I: IntoIterator,
<I as IntoIterator>::Item: Into<OsString> + Clone,
Vec
of your making.
Print the error message and quit the program in case of failure. Read moresource§fn from_iter_safe<I>(iter: I) -> Result<Self, Error>where
Self: Sized,
I: IntoIterator,
<I as IntoIterator>::Item: Into<OsString> + Clone,
fn from_iter_safe<I>(iter: I) -> Result<Self, Error>where
Self: Sized,
I: IntoIterator,
<I as IntoIterator>::Item: Into<OsString> + Clone,
Vec
of your making. Read more