Skip to main content

Cli

Struct Cli 

Source
pub struct Cli { /* private fields */ }
Expand description

A program CLI. Register options and arguments before parsing; registration errors panic.

Implementations§

Source§

impl Cli

Source

pub fn new() -> Cli

A CLI named after argv[0], rooted at the current directory, reading the process environment.

Source

pub fn name(&mut self, name: &str) -> &mut Self

Program name shown in usage.

Source

pub fn root(&mut self, root: &str) -> &mut Self

Base directory for default/env path values and search dirs (relative to cwd).

Source

pub fn cwd(&mut self, cwd: &str) -> &mut Self

Directory command-line paths are relative to.

Source

pub fn env<I: IntoIterator<Item = (String, String)>>( &mut self, env: I, ) -> &mut Self

Replace the environment options are read from.

Source

pub fn description(&mut self, text: &str) -> &mut Self

Set the description below the usage line and return this CLI.

Source

pub fn epilog(&mut self, text: &str) -> &mut Self

Set the final help text and return this CLI.

Source

pub fn effects(&mut self, effects: &[&str]) -> &mut Self

What running the program does: read-only, idempotent, destructive, network.

Source

pub fn stdin(&mut self, description: &str, content_type: &str) -> &mut Self

What the program reads on stdin; content_type is a MIME type or a comma-separated list.

Source

pub fn stdout(&mut self, description: &str, content_type: &str) -> &mut Self

What the program writes on stdout; undeclared means text.

Source

pub fn exclusive(&mut self, longs: &[&str]) -> &mut Self

At most one of these options may be given.

Source

pub fn requires(&mut self, long: &str, longs: &[&str]) -> &mut Self

When long is given, the others must be too.

Source

pub fn one_of(&mut self, longs: &[&str]) -> &mut Self

At least one of these options must be given.

Source

pub fn command(&mut self, name: &str, description: &str) -> &mut Cli

Register a command (spec section 1.7) and return it, to register its options and arguments on.

Source

pub fn command_path(&self) -> Vec<String>

The command words selected by the last parse, e.g. ["db", "migrate"].

Source

pub fn config(&mut self, option: &str, prefixes: &str) -> &mut Self

option holds the config file path; prefixes is comma-separated.

Source

pub fn require_command( &mut self, command: &str, description: &str, install_hint: &str, ) -> &mut Self

Require an executable on PATH when parsing, with a description and optional installation hint.

Fallback dirs (colon-separated, relative to root) for bare relative values of a path option.

Source

pub fn opt( &mut self, var: &str, long: &str, short: &str, default: &str, description: &str, ) -> OptRef<'_>

Register an option. default is a value, “flag”, “optional”, or “” (required).

Source

pub fn opt_array( &mut self, var: &str, long: &str, short: &str, description: &str, ) -> OptRef<'_>

Register a repeatable option whose values accumulate into a list.

Source

pub fn arg( &mut self, name: &str, description: &str, default: &str, rule: &str, ) -> ArgRef<'_>

Register a positional argument. An empty default makes it required.

Source

pub fn arg_variadic( &mut self, name: &str, description: &str, rule: &str, ) -> ArgRef<'_>

Register a final positional argument that collects all remaining tokens.

Source

pub fn parse<S: AsRef<str>>(&mut self, argv: &[S]) -> Parsed

Parse without exiting. Values are available via Cli::values when the result is Parsed::Ok.

Source

pub fn run(&mut self) -> Values

Parse std::env::args() like a CLI: handles –help, –help-json-schema and –bash-completion, prints errors and exits on failure. Returns the values.

Source

pub fn run_with<S: AsRef<str>>(&mut self, argv: &[S]) -> Values

Cli::run with explicit arguments (excluding the program name).

Source

pub fn values(&self) -> &Values

Read resolved values from the last successful parse.

Source

pub fn source(&self, long: &str) -> &'static str

Where an option’s value came from: cli, config, env, default or unset.

Source

pub fn is_set(&self, long: &str) -> bool

Whether an option came from the command line.

Source

pub fn is_explicitly_set(&self, long: &str) -> bool

Whether an option came from CLI, config, or environment rather than a default.

Source

pub fn values_json(&self) -> String

Resolved values as JSON (spec section 10).

Source

pub fn usage(&mut self) -> String

Help text (spec section 7), for the selected command.

Source

pub fn json_schema(&mut self) -> String

JSON description of the CLI (spec section 8).

Source

pub fn completion_script(&self, shell: &str) -> Option<String>

Shell script that enables completion for this program (spec section 9): eval "$(prog --completion bash)". None for an unknown shell.

Source

pub fn completion_data(&mut self) -> String

Tab-separated completion records (spec section 9).

Source

pub fn completion_data_for(&mut self, words: &[&str]) -> String

Completion records for the words typed after the program name: a program with commands follows them (spec section 9).

Trait Implementations§

Source§

impl Clone for Cli

Source§

fn clone(&self) -> Cli

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Cli

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Cli

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl Freeze for Cli

§

impl RefUnwindSafe for Cli

§

impl Send for Cli

§

impl Sync for Cli

§

impl Unpin for Cli

§

impl UnsafeUnpin for Cli

§

impl UnwindSafe for Cli

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<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.