#getopts #option

getopts

getopts-like option parsing

24 releases

0.2.18 Jul 5, 2018
0.2.17 Jan 25, 2018
0.2.15 Sep 5, 2017
0.2.14 Aug 28, 2015
0.1.0 Dec 13, 2014

#6 in Command-line interface

Download history 14225/week @ 2018-05-27 18320/week @ 2018-06-03 17354/week @ 2018-06-10 16223/week @ 2018-06-17 16579/week @ 2018-06-24 18392/week @ 2018-07-01 18634/week @ 2018-07-08 22903/week @ 2018-07-15 20012/week @ 2018-07-22 18193/week @ 2018-07-29 19123/week @ 2018-08-05 17753/week @ 2018-08-12 15853/week @ 2018-08-19

75,306 downloads per month
Used in 418 crates (208 directly)

MIT/Apache

72KB
1.5K SLoC

getopts

A Rust library for option parsing for CLI utilities.

Build Status

Documentation

Usage

Add this to your Cargo.toml:

[dependencies]
getopts = "0.2"

and this to your crate root:

extern crate getopts;

Rust Version Support

The minimum supported Rust version is 1.18.


lib.rs:

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.

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

~36KB