Skip to main content

Args

Trait Args 

Source
pub trait Args: Sized {
    const HELP: &'static Command = _;

    // Required method
    fn parse_argv_with(
        input: &mut Argv<'_>,
        globals: &mut dyn Globals,
    ) -> Result<Self, Error>;

    // Provided methods
    fn parse_argv(input: &mut Argv<'_>) -> Result<Self, Error> { ... }
    fn completion_request(args: &[OsString]) -> Option<String> { ... }
    fn completion_script(shell: Shell) -> String { ... }
    fn parse_words(words: &[&BStr]) -> Result<Self, Error> { ... }
    fn parse_from_argv<I, S>(argv: I) -> Result<Self, Error>
       where I: IntoIterator<Item = S>,
             S: AsRef<OsStr> { ... }
    fn parse_from<I, S>(args: I) -> Result<Self, Error>
       where I: IntoIterator<Item = S>,
             S: AsRef<OsStr> { ... }
    fn parse() -> Self { ... }
    fn try_parse() -> Result<Self, Error> { ... }
    fn program() -> String { ... }
}
Expand description

A type parsed from a whole command line.

Provided Associated Constants§

Source

const HELP: &'static Command = _

This command’s help, rendered only when asked for.

Required Methods§

Source

fn parse_argv_with( input: &mut Argv<'_>, globals: &mut dyn Globals, ) -> Result<Self, Error>

Args::parse_argv as a subcommand: flags it does not declare itself are offered to globals, its ancestors’ global flags.

Provided Methods§

Source

fn parse_argv(input: &mut Argv<'_>) -> Result<Self, Error>

Parse the command line in input, consuming all of it.

This is a winnow parser: Cli::parse_argv can be passed anywhere a Parser<Argv, Cli, Error> is expected.

Source

fn completion_request(args: &[OsString]) -> Option<String>

Answer a completion script’s callback (PROG __complete_word__ …) from this command’s help data; None when args (the command line after the program’s name) is not one. See complete.

Source

fn completion_script(shell: Shell) -> String

The completion script for shell, calling back the program named in the help data (#[arg(name = "…")]).

§Panics

If the command has no name, or one that is not a plain word.

Source

fn parse_words(words: &[&BStr]) -> Result<Self, Error>

Parse words, the arguments after the program name, already borrowed as BStrs: what response::expand and words give. Nothing is copied.

An unknown flag or subcommand that is close to one of the program’s has it as Error::similar.

Source

fn parse_from_argv<I, S>(argv: I) -> Result<Self, Error>
where I: IntoIterator<Item = S>, S: AsRef<OsStr>,

Parse argv, the whole command line, program name first. The name is skipped.

Source

fn parse_from<I, S>(args: I) -> Result<Self, Error>
where I: IntoIterator<Item = S>, S: AsRef<OsStr>,

Parse args, the arguments after the program name.

use winnow_args::{Args, ErrorKind};

#[derive(Args)]
struct Cli {
    #[arg(short, long)]
    verbose: bool,
}

assert!(Cli::parse_from(["-v"]).unwrap().verbose);
assert!(Cli::parse_from_argv(["prog", "-v"]).unwrap().verbose);
let error = Cli::parse_from(["--loud"]).err().unwrap();
assert_eq!(error.kind(), ErrorKind::UnknownFlag);
Source

fn parse() -> Self

Parse the process’s arguments; on help, version or a failure, print and exit as report does. It answers no completion callback: call Args::completion_request first if the program has complete scripts.

Source

fn try_parse() -> Result<Self, Error>

Parse the process’s arguments, leaving help, version and failures to the caller: Args::parse without the printing and the exit.

use winnow_args::Args;

#[derive(Args)]
struct Cli {
    #[arg(short, long)]
    verbose: bool,
}

let cli = match Cli::try_parse() {
    Ok(cli) => cli,
    Err(error) => std::process::exit(winnow_args::report(&error, &Cli::program())),
};
Source

fn program() -> String

The program’s name, as messages give it: #[arg(name = "…")], else the file name the process was run as.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§