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§

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.

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§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more
Returns the corresponding clap::App.
Creates the struct from clap::ArgMatches. It cannot fail with a parameter generated by clap by construction. Read more
Gets the struct from the command line arguments. Print the error message and quit the program in case of failure. Read more
Gets the struct from any iterator such as a Vec of your making. Print the error message and quit the program in case of failure. Read more
Gets the struct from any iterator such as a Vec of your making. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.