Skip to main content

get_argument

Function get_argument 

Source
pub fn get_argument(
    matches: &ArgMatches,
    name: &str,
) -> Result<String, ProcessError>
Expand description

Retrieves the value of a specified command-line argument.

§Arguments

  • matches - Clap argument matches object containing parsed arguments.
  • name - The name of the argument to retrieve.

§Returns

  • Result<String, ProcessError> - Returns the argument value on success or an error if the argument is missing.

§Errors

  • Returns ProcessError::MissingArgument if the specified argument is not provided.

§Example

let matches = Command::new("test")
    .arg(clap::arg!(--"config" <CONFIG> "Specifies the configuration file"))
    .get_matches_from(vec!["test", "--config", "path/to/config.toml"]);
let config_path = get_argument(&matches, "config").expect("Argument not found");
println!("Config path: {}", config_path);