[src]

Crate getopts

Simple getopt alternative.

Construct a vector of options, either by using reqopt, optopt, and optflag or by building them from components yourself, and pass them to getopts, along with a vector of actual arguments (not including argv[0]). You'll either get a failure code back, or a match. You'll have to verify whether the amount of 'free' arguments in the match is what you expect. Use opt_* accessors to get argument values out of the matches object.

Single-character options are expected to appear on the command line with a single preceding dash; multiple-character options are expected to be proceeded by two dashes. Options that expect an argument accept their argument following either a space or an equals sign. Single-character options don't require the space.

Example

The following example shows simple command line parsing for an application that requires an input file to be specified, accepts an optional output file name following -o, and accepts both -h and --help as optional flags.

extern crate getopts;
use getopts::{optopt,optflag,getopts,OptGroup};
use std::os;

fn do_work(inp: &str, out: Option<~str>) {
    println!("{}", inp);
    match out {
        Some(x) => println!("{}", x),
        None => println!("No Output"),
    }
}

fn print_usage(program: &str, _opts: &[OptGroup]) {
    println!("Usage: {} [options]", program);
    println!("-o\t\tOutput");
    println!("-h --help\tUsage");
}

fn main() {
    let args = os::args();

    let program = args[0].clone();

    let opts = ~[
        optopt("o", "", "set output file name", "NAME"),
        optflag("h", "help", "print this help menu")
    ];
    let matches = match getopts(args.tail(), opts) {
        Ok(m) => { m }
        Err(f) => { fail!(f.to_err_msg()) }
    };
    if matches.opt_present("h") {
        print_usage(program, opts);
        return;
    }
    let output = matches.opt_str("o");
    let input: &str = if !matches.free.is_empty() {
        (*matches.free.get(0)).clone()
    } else {
        print_usage(program, opts);
        return;
    };
    do_work(input, output);
}
Matches

The result of checking command line arguments. Contains a vector of matches and a vector of free strings.

Opt

A description of a possible option.

OptGroup

One group of options, e.g., both -h and --help, along with their shared description and properties.

FailType

The type of failure that occurred.

Fail_

The type returned when the command line does not conform to the expected format. Call the to_err_msg method to retrieve the error as a string.

HasArg

Describes whether an option has an argument.

Name

Name of an option. Either a string or a single char.

Occur

Describes how often an option may occur.

getopts

Parse command line arguments according to the provided options.

opt

Create a generic option group, stating all parameters explicitly

optflag

Create a long option that is optional and does not take an argument.

optflagmulti

Create a long option that can occur more than once and does not take an argument.

optflagopt

Create a long option that is optional and takes an optional argument.

optmulti

Create a long option that is optional, takes an argument, and may occur multiple times.

optopt

Create a long option that is optional and takes an argument.

reqopt

Create a long option that is required and takes an argument.

short_usage

Derive a short one-line usage summary from a set of long options.

usage

Derive a usage message from a set of long options.

Result

The result of parsing a command line with a set of options.