pub struct Cli<C> { /* private fields */ }Expand description
Command-line configuration with YAML + argv precedence.
§Type parameter
C is any config struct that implements Default. Defaults are the
base layer; a YAML file loaded via -c/--config overrides them; explicit
argv flags override those.
§YAML support
YAML loading is the caller’s responsibility. Pass a loader closure to
Cli::parse_args_with_yaml to apply YAML fields before argv parsing when
a -c/--config path is present.
Implementations§
Source§impl<C: Default + 'static> Cli<C>
impl<C: Default + 'static> Cli<C>
Sourcepub fn new(name: &str, description: &str) -> Self
pub fn new(name: &str, description: &str) -> Self
Create a new CLI with the given program name and description.
Sourcepub fn add_option<T, F>(&mut self, names: &str, field: F, description: &str)
pub fn add_option<T, F>(&mut self, names: &str, field: F, description: &str)
Register a named option on the root command.
Sourcepub fn add_flag<F>(&mut self, names: &str, field: F, description: &str)
pub fn add_flag<F>(&mut self, names: &str, field: F, description: &str)
Register a bool flag on the root command.
Sourcepub fn add_multi<F>(&mut self, names: &str, field: F, description: &str)
pub fn add_multi<F>(&mut self, names: &str, field: F, description: &str)
Register a multi-option on the root command.
Sourcepub fn add_positional<T, F>(
&mut self,
name: &str,
field: F,
description: &str,
required: bool,
)
pub fn add_positional<T, F>( &mut self, name: &str, field: F, description: &str, required: bool, )
Register a positional argument on the root command.
Sourcepub fn add_subcommand(&mut self, name: &str, description: &str) -> &mut Cmd<C>
pub fn add_subcommand(&mut self, name: &str, description: &str) -> &mut Cmd<C>
Add a named subcommand and return a mutable reference to register options on it.
Sourcepub fn require_subcommand(&mut self, min: i32, max: i32)
pub fn require_subcommand(&mut self, min: i32, max: i32)
Require between min and max subcommands (0 = unlimited).
Sourcepub fn set_epilog(&mut self, text: &str)
pub fn set_epilog(&mut self, text: &str)
Set the epilog shown after options in --help.
Sourcepub fn set_fallthrough(&mut self, enable: bool)
pub fn set_fallthrough(&mut self, enable: bool)
Control whether unknown flags fall through to the root command when a
subcommand is active. Default: true.
Sourcepub fn config_mut(&mut self) -> &mut C
pub fn config_mut(&mut self) -> &mut C
Mutably borrow the current config value (for programmatic override).
Sourcepub fn into_config(self) -> C
pub fn into_config(self) -> C
Consume the CLI and return the parsed config.
Sourcepub fn config_path(&self) -> &str
pub fn config_path(&self) -> &str
Path provided via -c/--config, empty if not supplied.
Sourcepub fn help_requested(&self) -> bool
pub fn help_requested(&self) -> bool
True when --help or -h was the only meaningful argument.
Sourcepub fn got_subcommand(&self, name: &str) -> bool
pub fn got_subcommand(&self, name: &str) -> bool
True when the named subcommand was parsed.
Sourcepub fn active_subcommand(&self) -> &str
pub fn active_subcommand(&self) -> &str
Name of the active subcommand, or empty string.
Sourcepub fn parse_args(&mut self, args: &[String]) -> bool
pub fn parse_args(&mut self, args: &[String]) -> bool
Parse argv (without the program name).
Returns false if --help was requested or an error occurred.
The caller should exit cleanly on false when help_requested() is
true, and print an error / exit with failure otherwise.
Precedence: struct defaults < load_yaml callback < explicit flags.
Sourcepub fn parse_args_with_yaml<F>(
&mut self,
args: &[String],
yaml_loader: Option<F>,
) -> bool
pub fn parse_args_with_yaml<F>( &mut self, args: &[String], yaml_loader: Option<F>, ) -> bool
Parse with an optional YAML loader hook.
yaml_loader is called with the raw file content (if -c/--config is
present) before argv flags are applied. The caller owns the YAML
parsing dependency.