[][src]Module crosvm::argument

Handles argument parsing.

Example

let arguments = &[
    Argument::positional("FILES", "files to operate on"),
    Argument::short_value('p', "program", "PROGRAM", "Program to apply to each file"),
    Argument::short_value('c', "cpus", "N", "Number of CPUs to use. (default: 1)"),
    Argument::flag("unmount", "Unmount the root"),
    Argument::short_flag('h', "help", "Print help message."),
];

let match_res = set_arguments(args, arguments, |name, value| {
    match name {
        "" => println!("positional arg! {}", value.unwrap()),
        "program" => println!("gonna use program {}", value.unwrap()),
        "cpus" => {
            let v: u32 = value.unwrap().parse().map_err(|_| {
                Error::InvalidValue {
                    value: value.unwrap().to_owned(),
                    expected: "this value for `cpus` needs to be integer",
                }
            })?;
        }
        "unmount" => println!("gonna unmount"),
        "help" => return Err(Error::PrintHelp),
        _ => unreachable!(),
    }
    unreachable!();
});

match match_res {
    Ok(_) => println!("running with settings"),
    Err(Error::PrintHelp) => print_help("best_program", "FILES", arguments),
    Err(e) => println!("{}", e),
}

Structs

Argument

Information about an argument expected from the command line.

Enums

ArgumentValueMode
Error

An error with argument parsing.

Functions

parse_arguments
print_help

Prints command line usage information to stdout.

set_arguments

Parses the given args against the list of know arguments arg_list and calls f with each present argument and value if required.

Type Definitions

Result

Result of a argument parsing.