pub struct Cli { /* private fields */ }Expand description
A program CLI. Register options and arguments before parsing; registration errors panic.
Implementations§
Source§impl Cli
impl Cli
Sourcepub fn new() -> Cli
pub fn new() -> Cli
A CLI named after argv[0], rooted at the current directory, reading the process environment.
Sourcepub fn root(&mut self, root: &str) -> &mut Self
pub fn root(&mut self, root: &str) -> &mut Self
Base directory for default/env path values and search dirs (relative to cwd).
Sourcepub fn env<I: IntoIterator<Item = (String, String)>>(
&mut self,
env: I,
) -> &mut Self
pub fn env<I: IntoIterator<Item = (String, String)>>( &mut self, env: I, ) -> &mut Self
Replace the environment options are read from.
Sourcepub fn description(&mut self, text: &str) -> &mut Self
pub fn description(&mut self, text: &str) -> &mut Self
Set the description below the usage line and return this CLI.
Sourcepub fn effects(&mut self, effects: &[&str]) -> &mut Self
pub fn effects(&mut self, effects: &[&str]) -> &mut Self
What running the program does: read-only, idempotent, destructive, network.
Sourcepub fn stdin(&mut self, description: &str, content_type: &str) -> &mut Self
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.
Sourcepub fn stdout(&mut self, description: &str, content_type: &str) -> &mut Self
pub fn stdout(&mut self, description: &str, content_type: &str) -> &mut Self
What the program writes on stdout; undeclared means text.
Sourcepub fn exclusive(&mut self, longs: &[&str]) -> &mut Self
pub fn exclusive(&mut self, longs: &[&str]) -> &mut Self
At most one of these options may be given.
Sourcepub fn requires(&mut self, long: &str, longs: &[&str]) -> &mut Self
pub fn requires(&mut self, long: &str, longs: &[&str]) -> &mut Self
When long is given, the others must be too.
Sourcepub fn one_of(&mut self, longs: &[&str]) -> &mut Self
pub fn one_of(&mut self, longs: &[&str]) -> &mut Self
At least one of these options must be given.
Sourcepub fn command(&mut self, name: &str, description: &str) -> &mut Cli
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.
Sourcepub fn command_path(&self) -> Vec<String>
pub fn command_path(&self) -> Vec<String>
The command words selected by the last parse, e.g. ["db", "migrate"].
Sourcepub fn config(&mut self, option: &str, prefixes: &str) -> &mut Self
pub fn config(&mut self, option: &str, prefixes: &str) -> &mut Self
option holds the config file path; prefixes is comma-separated.
Sourcepub fn require_command(
&mut self,
command: &str,
description: &str,
install_hint: &str,
) -> &mut Self
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.
Sourcepub fn path_search(&mut self, long: &str, dirs: &str) -> &mut Self
pub fn path_search(&mut self, long: &str, dirs: &str) -> &mut Self
Fallback dirs (colon-separated, relative to root) for bare relative values of a path option.
Sourcepub fn opt(
&mut self,
var: &str,
long: &str,
short: &str,
default: &str,
description: &str,
) -> OptRef<'_>
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).
Sourcepub fn opt_array(
&mut self,
var: &str,
long: &str,
short: &str,
description: &str,
) -> OptRef<'_>
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.
Sourcepub fn arg(
&mut self,
name: &str,
description: &str,
default: &str,
rule: &str,
) -> ArgRef<'_>
pub fn arg( &mut self, name: &str, description: &str, default: &str, rule: &str, ) -> ArgRef<'_>
Register a positional argument. An empty default makes it required.
Sourcepub fn arg_variadic(
&mut self,
name: &str,
description: &str,
rule: &str,
) -> ArgRef<'_>
pub fn arg_variadic( &mut self, name: &str, description: &str, rule: &str, ) -> ArgRef<'_>
Register a final positional argument that collects all remaining tokens.
Sourcepub fn parse<S: AsRef<str>>(&mut self, argv: &[S]) -> Parsed
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.
Sourcepub fn run(&mut self) -> Values
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.
Sourcepub fn run_with<S: AsRef<str>>(&mut self, argv: &[S]) -> Values
pub fn run_with<S: AsRef<str>>(&mut self, argv: &[S]) -> Values
Cli::run with explicit arguments (excluding the program name).
Sourcepub fn source(&self, long: &str) -> &'static str
pub fn source(&self, long: &str) -> &'static str
Where an option’s value came from: cli, config, env, default or unset.
Sourcepub fn is_explicitly_set(&self, long: &str) -> bool
pub fn is_explicitly_set(&self, long: &str) -> bool
Whether an option came from CLI, config, or environment rather than a default.
Sourcepub fn values_json(&self) -> String
pub fn values_json(&self) -> String
Resolved values as JSON (spec section 10).
Sourcepub fn json_schema(&mut self) -> String
pub fn json_schema(&mut self) -> String
JSON description of the CLI (spec section 8).
Sourcepub fn completion_script(&self, shell: &str) -> Option<String>
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.
Sourcepub fn completion_data(&mut self) -> String
pub fn completion_data(&mut self) -> String
Tab-separated completion records (spec section 9).
Sourcepub fn completion_data_for(&mut self, words: &[&str]) -> String
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).