Skip to main content

Cli

Struct Cli 

Source
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>

Source

pub fn new(name: &str, description: &str) -> Self

Create a new CLI with the given program name and description.

Source

pub fn add_option<T, F>(&mut self, names: &str, field: F, description: &str)
where T: ParseCliValue + 'static, F: Fn(&mut C) -> &mut T + Clone + 'static,

Register a named option on the root command.

Source

pub fn add_flag<F>(&mut self, names: &str, field: F, description: &str)
where F: Fn(&mut C, bool) + Clone + 'static,

Register a bool flag on the root command.

Source

pub fn add_multi<F>(&mut self, names: &str, field: F, description: &str)
where F: Fn(&mut C) -> &mut Vec<String> + Clone + 'static,

Register a multi-option on the root command.

Source

pub fn add_positional<T, F>( &mut self, name: &str, field: F, description: &str, required: bool, )
where T: ParseCliValue + 'static, F: Fn(&mut C) -> &mut T + Clone + 'static,

Register a positional argument on the root command.

Source

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.

Source

pub fn require_subcommand(&mut self, min: i32, max: i32)

Require between min and max subcommands (0 = unlimited).

Source

pub fn set_epilog(&mut self, text: &str)

Set the epilog shown after options in --help.

Source

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.

Source

pub fn config(&self) -> &C

Borrow the current config value.

Source

pub fn config_mut(&mut self) -> &mut C

Mutably borrow the current config value (for programmatic override).

Source

pub fn into_config(self) -> C

Consume the CLI and return the parsed config.

Source

pub fn config_path(&self) -> &str

Path provided via -c/--config, empty if not supplied.

Source

pub fn verbose(&self) -> i32

Verbose level provided via -v/--verbose.

Source

pub fn help_requested(&self) -> bool

True when --help or -h was the only meaningful argument.

Source

pub fn got_subcommand(&self, name: &str) -> bool

True when the named subcommand was parsed.

Source

pub fn active_subcommand(&self) -> &str

Name of the active subcommand, or empty string.

Source

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.

Source

pub fn parse_args_with_yaml<F>( &mut self, args: &[String], yaml_loader: Option<F>, ) -> bool
where F: FnOnce(&str, &mut C),

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.

Source

pub fn parse(&mut self) -> bool

Convenience: parse std::env::args() (skips argv[0]).

Auto Trait Implementations§

§

impl<C> !RefUnwindSafe for Cli<C>

§

impl<C> !Send for Cli<C>

§

impl<C> !Sync for Cli<C>

§

impl<C> !UnwindSafe for Cli<C>

§

impl<C> Forget for Cli<C>

§

impl<C> Freeze for Cli<C>
where Cmd<C>: Freeze, Vec<Cmd<C>>: Freeze, C: Freeze,

§

impl<C> Move for Cli<C>

§

impl<C> Unpin for Cli<C>
where C: Unpin, Cmd<C>: Unpin, Vec<Cmd<C>>: Unpin,

§

impl<C> UnsafeUnpin for Cli<C>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<S, T> Duplex<S> for T
where T: FromSample<S> + ToSample<S>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<S> FromSample<S> for S

Source§

fn from_sample_(s: S) -> S

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<F, T> IntoSample<T> for F
where T: FromSample<F>,

Source§

fn into_sample(self) -> T

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> ToSample<U> for T
where U: FromSample<T>,

Source§

fn to_sample_(self) -> U

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.