provcfg-clap 0.1.0

Clap integration for the provcfg configuration library.
Documentation
  • Coverage
  • 100%
    1 out of 1 items documented1 out of 1 items with examples
  • Size
  • Source code size: 16.72 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 262.76 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 8s Average build duration of successful builds.
  • all releases: 8s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • secana/provcfg
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • secana

Clap integration for provcfg.

Derive [ClapArgs] alongside provcfg::Configurable to generate a <Name>Args struct (a clap::Args) plus a From<&<Name>Args> for <Name>Partial impl. A clap-parsed command line then flows into a provcfg CLI source without a hand-written per-field conversion.

use clap::Parser;
use provcfg::{Category, Config, Configurable};
use provcfg_clap::ClapArgs;

#[derive(Configurable, ClapArgs, Clone, Default, serde::Deserialize, serde::Serialize)]
#[configurable(clap_prefix = "registry")]
struct Registry {
    data_dir: String, // exposed as --registry-data-dir
    port: u16,         // exposed as --registry-port
}

#[derive(Parser)]
struct Cli {
    #[command(flatten)]
    registry: RegistryArgs,
}

let cli = Cli::parse_from(["app", "--registry-data-dir", "/var/lib/reg"]);

// The generated `From<&RegistryArgs>` builds the provcfg partial for us.
let partial: RegistryPartial = (&cli.registry).into();
let registry = Config::new().add_cli(partial).build::<RegistryProv>().unwrap();

assert_eq!(registry.data_dir.value(), "/var/lib/reg");
assert_eq!(registry.data_dir.source().category(), Category::Cli);
// `--registry-port` was not passed, so `port` keeps its compiled-in default.
assert_eq!(registry.port.source().category(), Category::Default);

The ClapArgs derive:

  • generates <Name>Args with each leaf wrapped in Option<T> and an #[arg(long = "<prefix>-<field>")] auto-derived from the field name;
  • forwards any user-written #[arg(...)] attributes; a user-supplied long = "..." overrides the auto-derived one;
  • for #[configurable(nested)] fields emits #[command(flatten)] into the nested type's own Args struct (which must also derive ClapArgs);
  • omits #[configurable(skip)] fields from both the Args struct and the From impl.