[−][src]Struct spirit_cfg_helpers::CfgDump
A command line fragment to 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 extension
.
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::prelude::*; 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::extension(Opts::dump)) .run(|_| Ok(())); }
Implementations
impl CfgDump
[src]
pub fn dump<C: Serialize>(&self, cfg: &C)
[src]
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
extension
within an Extensible
(either spirit::Spirit
or
spirit::Builder
) and let it do everything automatically.
pub fn extension<E, F>(extract: F) -> impl Extension<E> where
E: Extensible<Ok = E>,
F: FnOnce(&E::Opts) -> &Self + Send + 'static,
E::Config: Serialize,
[src]
E: Extensible<Ok = E>,
F: FnOnce(&E::Opts) -> &Self + Send + 'static,
E::Config: Serialize,
An extension that can be registered with Extensible::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. Therefore it'll fail to dump it if the configuration is invalid.
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
impl Clone for CfgDump
[src]
impl Debug for CfgDump
[src]
impl Default for CfgDump
[src]
impl StructOpt for CfgDump
[src]
fn clap<'a, 'b>() -> App<'a, 'b>
[src]
fn from_clap(matches: &ArgMatches<'_>) -> Self
[src]
fn from_args() -> Self
[src]
fn from_args_safe() -> Result<Self, Error>
[src]
fn from_iter<I>(iter: I) -> Self where
I: IntoIterator,
<I as IntoIterator>::Item: Into<OsString>,
<I as IntoIterator>::Item: Clone,
[src]
I: IntoIterator,
<I as IntoIterator>::Item: Into<OsString>,
<I as IntoIterator>::Item: Clone,
fn from_iter_safe<I>(iter: I) -> Result<Self, Error> where
I: IntoIterator,
<I as IntoIterator>::Item: Into<OsString>,
<I as IntoIterator>::Item: Clone,
[src]
I: IntoIterator,
<I as IntoIterator>::Item: Into<OsString>,
<I as IntoIterator>::Item: Clone,
impl StructOptInternal for CfgDump
[src]
fn augment_clap<'a, 'b>(app: App<'a, 'b>) -> App<'a, 'b>
[src]
fn is_subcommand() -> bool
[src]
fn from_subcommand(_sub: (&'b str, Option<&'b ArgMatches<'a>>)) -> Option<Self>
[src]
Auto Trait Implementations
impl RefUnwindSafe for CfgDump
impl Send for CfgDump
impl Sync for CfgDump
impl Unpin for CfgDump
impl UnwindSafe for CfgDump
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> IntoResult<T> for T
[src]
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T
[src]
fn clone_into(&self, target: &mut T)
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,