Skip to main content

Module defaults

Module defaults 

Source
Expand description

Flag defaults from the tool’s config file.

Every sandogasa tool lets users pin flag defaults in a [defaults] table of its config — /etc/<tool>/config.toml overridden per key by ~/.config/<tool>/config.toml (see the root DEVELOPMENT.md for the pattern):

[defaults]          # tool-wide
explain = true

[defaults.update]   # for one subcommand only
quiet = true

Keys are the flag’s long name (as typed on the command line, dashes included). A top-level key covers global and top-level flags, and also applies to any invoked subcommand that has a flag of that name (a subcommand without it just ignores the default) — so one explain = true line covers every dbranch subcommand with --explain. Values: true turns a boolean flag on (false is a no-op — flags can’t be un-set, so it just means “no default”); strings and numbers become --key value; arrays repeat the flag per element.

Precedence and safety rules, in order:

  • anything given on the command line (or via a flag’s env var) wins — a config default never overrides it;
  • a default is silently skipped when it conflicts (per clap’s conflicts_with) with an explicitly-given flag, so e.g. --quiet on the command line suppresses a configured explain = true rather than erroring;
  • --no-defaults (added to every tool by this module) skips the whole table for one run;
  • unknown keys are hard errors — a typo’d flag name must not be silently ignored.

Functions§

parse_with_defaults
Parse the command line like T::parse(), applying [defaults] from the tool’s config layers (/etc/<tool>/config.toml, then ~/.config/<tool>/config.toml overriding it per key) for flags not given on the command line. Call with the tool’s crate name: parse_with_defaults::<Cli>(env!("CARGO_PKG_NAME")).