Expand description
The facade for building compiled Rust CLIs with usage.
Depend on usage-rs under the short crate name usage. That is the one package an
application needs: derive macros, the argv runtime, help, and clap-shaped errors ship in the
defaults. Completions stay behind a feature; low-level adopters that want only the binding
runtime keep depending on usage-argv directly.
[dependencies]
usage = { package = "usage-rs", version = "6" }What happens after a parse can come from the same declaration: a command implements Run,
the subcommand enum says #[usage(run)], and the match that routes argv to the code
carrying it out is generated rather than written. RunWith under #[usage(run_with)] hands
each command shared state, and RunAsync / RunAsyncWith under #[usage(run_async)] /
#[usage(run_async_with)] are the async pair. Nothing about any of it reaches the spec.
Enable portable expression validation only when a CLI declares validate rules:
usage = { package = "usage-rs", version = "6", features = ["validation"] }use usage_rs as usage;
use usage::Cli;
#[derive(Cli)]
#[usage(bin = "ex")]
struct Ex {
#[usage(long, value_hint = usage::ValueHint::FilePath)]
file: Option<std::path::PathBuf>,
}
let argv = [std::ffi::OsStr::new("--file"), std::ffi::OsStr::new("input.txt")];
let ex = Ex::parse_from(&argv).expect("valid command line");
assert_eq!(ex.file.as_deref(), Some(std::path::Path::new("input.txt")));Re-exports§
pub use usage_argv as argv;
Modules§
- diagnostic
- What a user reads when a command line does not parse.
- embedded
- Process control for a CLI embedded in another runtime.
- help
- The one-line summary of how a command is invoked.
- run
- Dispatch: handing a parsed command to the code that carries it out.
- spec
- Cold-path metadata, and writing it out as a spec.
- warn
- What a parse has to say about declarations that still work but should not be used.
Macros§
Structs§
- Arg
- A positional argument.
- Binding
Type - Resolved identity of a derive-generated binding type.
- Clause
- A separator-delimited positional group.
- Command
- A command: its flags, its positional arguments, and its subcommands.
- Flag
- A flag, addressed by any of its long or short forms.
- Invalid
Value - Why a value would not convert into the type its field holds.
- Parser
- A single-pass parse over
argv. - Validation
Error - A command-wide validation or finalization failure.
Enums§
- ArgAction
- What supplying a declared flag does.
- Double
Dash - How an argument relates to the
--separator. - Error
- A binding failure.
- Event
- Something the parser bound.
- Unknown
Flags - What to do with a flag-like token that names no flag in scope.
- Value
Hint - A value’s shell-native completion class for
#[usage(value_hint = ...)].
Constants§
- HELP_
LONG_ KEY - The key
--helpanswers to, and the one-hdoes. - HELP_
SHORT_ KEY - See
HELP_LONG_KEY. - MAX_
DEPTH - How deep a command tree this parser will descend.
- SPEC_
REQUEST - The word a tool sends to ask a binary for its own spec.
- VERSION_
LONG_ KEY - See
HELP_LONG_KEY. - VERSION_
SHORT_ KEY - See
HELP_LONG_KEY.
Statics§
- HELP_
LONG --help, which every command answers to.- HELP_
SHORT -h, which prints the shorter form.- VERSION_
LONG --version, where the CLI declared one.- VERSION_
SHORT -V, which clap also supplies.
Traits§
- Run
- A command that can be carried out with nothing but what it parsed.
- RunAsync
- An async command:
Run, awaited. - RunAsync
With - An async command that is handed something shared when it runs:
RunWith, awaited. - RunWith
- A command that is handed something shared when it runs.
Functions§
- as_str
- Interpret a value as UTF-8.
- assert_
unique_ subcommand_ names - Refuse two subcommands that answer to the same name, aliases included.
- concat_
args - Join groups of argument tables into one, at compile time.
- concat_
flags - Join groups of flag tables into one, at compile time.
- find_
subcommand - Resolve a subcommand by name or alias, at compile time.
- invalid_
choice_ value - One
Error::InvalidValuefor a word that is not one of a value enum’s choices. - invalid_
os_ value - One
Error::InvalidValuefor bytes the platform cannot hold in a path. - invalid_
parsed_ value - One
Error::InvalidValuefor a value whose type would not build from it. - invalid_
utf8_ value - One
Error::InvalidValuefor a word that was not UTF-8. - is_
help_ flag - Whether a flag is one of the two the parser supplies rather than the CLI declaring it.
- is_
spec_ request - Whether this argv asks for the spec rather than for the CLI to run.
- is_
version_ arg - Whether one exact root argument selects a declared or synthesized version action.
- is_
version_ flag - Whether a flag is one of the two the parser supplies for
--version. - key_
base - The high half of every key one declaration’s items get.
- multicall_
applet - The applet name to parse as the first word, when argv[0] is not the dispatcher.
- multicall_
basename - Basename of argv[0] for a multicall CLI: last path component, with a trailing
.exestripped so Windows and Unix agree. - os_
string_ from_ bytes - Rebuild an
OsStringfrom bytes the parser handed back. - os_
values - Convert every repeated value of one path-like field, reporting
namefor the first the platform cannot hold. - parsed_
values - Convert every repeated value of one field through
FromStr, reportingnamefor the first that fails. - render_
failure - What a caller should print for a parse failure, and what to exit with.
- render_
failure_ plain - A parse failure, never coloured.
- render_
failure_ view - Render a failure through a spec-declared executable view.
- render_
warnings - What a caller should print for the deprecations a command line used.
- table_
len - How many entries a group of tables holds in total.
- utf8_
values - Convert every repeated value of one text field, reporting
namefor the first that is not UTF-8.
Derive Macros§
- ArgGroup
- Compile an enum into a set of related flags.
- Args
- Compile a struct into one subcommand’s flags and arguments.
- Cli
- Compile a struct into a parser and a spec. See the crate docs.
- Subcommands
- Compile an enum into a set of subcommands.
- Value
Enum - Compile an enum into the words one value may be.