#arg #multi #short #options-parsing #none #missing #long


getopts-like option parsing, a fork of the Rust team's getopts

3 releases

Uses old Rust 2015

0.1.2 Jun 10, 2015
0.1.1 Jun 9, 2015
0.1.0 Jun 9, 2015
Download history 12/week @ 2023-06-13 17/week @ 2023-06-20 14/week @ 2023-06-27 11/week @ 2023-07-04 10/week @ 2023-07-11 12/week @ 2023-07-18 13/week @ 2023-07-25 9/week @ 2023-08-01 10/week @ 2023-08-08 13/week @ 2023-08-15 12/week @ 2023-08-22 6/week @ 2023-08-29 15/week @ 2023-09-05 11/week @ 2023-09-12 7/week @ 2023-09-19 14/week @ 2023-09-26

51 downloads per month
Used in 2 crates


1.5K SLoC


Build Status

A Rust library for option parsing for CLI utilities, a fork of the Rust team's getopts


Add this to your Cargo.toml:

pgetopts = "0.1.0"

and this to your crate root:

extern crate pgetopts;


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 pgetopts, 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.


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

pgetopts = "0.2"

and this to your crate root:

extern crate pgetopts;


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 pgetopts;
use pgetopts::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) {
    println!("Usage: {} [options]", program);
    print!("{}", opts.options());

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);
    let output = matches.opt_str("o");
    let input = if !matches.free.is_empty() {
    } else {
        print_usage(&program, opts);
    do_work(&input, output);