30 releases

0.2.24 Aug 29, 2025
0.2.23 Jun 9, 2025
0.2.21 Aug 19, 2019
0.2.19 May 2, 2019
0.1.0 Dec 13, 2014

#84 in Command-line interface

Download history 741339/week @ 2025-12-27 1311780/week @ 2026-01-03 1619872/week @ 2026-01-10 1573000/week @ 2026-01-17 1772201/week @ 2026-01-24 1970296/week @ 2026-01-31 2568385/week @ 2026-02-07 2160010/week @ 2026-02-14 2132287/week @ 2026-02-21 2582360/week @ 2026-02-28 2917480/week @ 2026-03-07 2791641/week @ 2026-03-14 2486839/week @ 2026-03-21 2604067/week @ 2026-03-28 2713330/week @ 2026-04-04 3151134/week @ 2026-04-11

11,322,789 downloads per month
Used in 2,678 crates (528 directly)

MIT/Apache

82KB
2K SLoC

getopts

A Rust library for option parsing for CLI utilities.

Documentation

Usage

Add this to your Cargo.toml:

[dependencies]
getopts = "0.2"

Contributing

The getopts library is used by rustc, so we have to be careful about not changing its behavior.


lib.rs:

Simple getopt alternative.

Construct instance of Options and configure it by using reqopt(), optopt() and other methods that add option configuration. Then call parse() method and pass into it 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. Everything after double-dash "--" argument is considered to be a 'free' argument, even if it starts with dash.

Usage

This crate is on crates.io and can be used by adding getopts to the dependencies in your project's Cargo.toml.

[dependencies]
getopts = "0.2"

and this to your crate root:

extern crate getopts;

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::Options;
use std::env;

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

fn print_usage(program: &str, opts: Options) {
    let brief = format!("Usage: {} FILE [options]", program);
    print!("{}", opts.usage(&brief));
}

fn main() {
    let args: Vec<String> = env::args().collect();
    let program = args[0].clone();

    let mut opts = Options::new();
    opts.optopt("o", "", "set output file name", "NAME");
    opts.optflag("h", "help", "print this help menu");
    let matches = match opts.parse(&args[1..]) {
        Ok(m) => { m }
        Err(f) => { panic!("{}", f.to_string()) }
    };
    if matches.opt_present("h") {
        print_usage(&program, opts);
        return;
    }
    let output = matches.opt_str("o");
    let input = if !matches.free.is_empty() {
        matches.free[0].clone()
    } else {
        print_usage(&program, opts);
        return;
    };
    do_work(&input, output);
}

Dependencies

~0–385KB