[][src]Struct spirit_cfg_helpers::CfgHelp

pub struct CfgHelp { /* fields omitted */ }

A command line options fragment to add the --help-config option.

For the user to be able to configure an application, the user needs to know what options can be configured. Usually, this is explained using an example configuration file or through a manually written documentation. However, maintaining either is a lot of work, not mentioning that various spirit crates provide configuration fragments composed from several type parameters so hunting down all the available options might be hard.

This helper uses the StructDoc trait to extract the structure and documentation of the configuration automatically. Usually, its derive will extract description from fields' doc comments. See the structdoc crate's documentation to know how to let the documentation be created semi-automatically. All the configuration fragments provided by the spirit crates implement StructDoc, unless their [cfg-help] feature is disabled.

When the --help-config is specified, this auto-generated documentation is printed and the application exits.

The fragment can be used either manually with the help method or by registering the extension within an Extensible.

Examples

use serde_derive::Deserialize;
use spirit::Spirit;
use spirit::prelude::*;
use spirit_cfg_helpers::CfgHelp;
use structdoc::StructDoc;
use structopt::StructOpt;

#[derive(Default, Deserialize, StructDoc)]
struct Cfg {
    /// A very much useless but properly documented option.
    option: Option<String>,
}

#[derive(Debug, StructOpt)]
struct Opts {
    #[structopt(flatten)]
    help: CfgHelp,
}

impl Opts {
    fn help(&self) -> &CfgHelp {
        &self.help
    }
}

fn main() {
    Spirit::<Opts, Cfg>::new()
        .with(CfgHelp::extension(Opts::help))
        .run(|_| Ok(()));
}

Implementations

impl CfgHelp[src]

pub fn help<C: StructDoc>(&self)[src]

Show the help and exit if it was specified as an option.

This can be called manually to check for the command line option at the right time. If it was specified, the help for the C type is printed and the application exits. If the command line was not specified, this does nothing.

Note that the C type is passed as type parameter, therefore this needs to be invoked with the turbofish syntax.

The preferred way is usually by registering the extension.

Examples

use serde_derive::Deserialize;
use spirit_cfg_helpers::CfgHelp;
use structdoc::StructDoc;
use structopt::StructOpt;

#[derive(Deserialize, StructDoc)]
struct Cfg {
    /// A very much useless but properly documented option
    option: Option<String>,
}

#[derive(StructOpt)]
struct Opts {
    #[structopt(flatten)]
    help: CfgHelp,
}

let opts = Opts::from_args();
opts.help.help::<Cfg>();

pub fn extension<O, C, F>(extract: F) -> impl Extension<Builder<O, C>> where
    F: FnOnce(&O) -> &Self + Send + 'static,
    O: Debug + StructOpt + Send + Sync + 'static,
    C: DeserializeOwned + StructDoc + Send + Sync + 'static, 
[src]

A helper to be registered within an Extensible.

The extractor should take the whole command line options structure and provide reference to just the CfgHelp instance.

Trait Implementations

impl Clone for CfgHelp[src]

impl Debug for CfgHelp[src]

impl Default for CfgHelp[src]

impl StructOpt for CfgHelp[src]

impl StructOptInternal for CfgHelp[src]

Auto Trait Implementations

impl RefUnwindSafe for CfgHelp

impl Send for CfgHelp

impl Sync for CfgHelp

impl Unpin for CfgHelp

impl UnwindSafe for CfgHelp

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> IntoResult<T> for T[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.