argparse-rs

A simple argument parser, meant to parse command line input. It is inspired by the Python ArgumentParser

1 unstable release

Uses old Rust 2015

0.1.0 Jan 15, 2016
Download history 28/week @ 2024-07-22 28/week @ 2024-07-29 25/week @ 2024-08-05 30/week @ 2024-08-12 17/week @ 2024-08-19 13/week @ 2024-08-26 23/week @ 2024-09-02 12/week @ 2024-09-09 29/week @ 2024-09-16 30/week @ 2024-09-23 24/week @ 2024-09-30 14/week @ 2024-10-07 28/week @ 2024-10-14 36/week @ 2024-10-21 28/week @ 2024-10-28 41/week @ 2024-11-04

134 downloads per month
Used in gnuplot

MIT license

35KB
499 lines

argparser-rs

A simple argument parser, meant to parse command line input. It is inspired by the Python ArgumentParser

Simple class to parse arguments. The Highlights are:

  • A configurable add_opt method, to tell it what to look for.
  • A generic get method, which will return the argument you want with any type that implements FromStr.
  • The arguments are stored in maps, and so are accessed by the name you give them in the add_opt method.
  • The parse method can be called on an Iterator that produces &Strings.
  • There are no static or global variables so you can have as many parsers as you want.
  • The parser can take any values that implement FromStr or for which you can provide a closure to parse them from String
  • You can specify if any argument is required or not, as well as default values for all
  • It also prints a default help message, similar to the one Python's argparser prints

Example use:

extern crate argparse;

use std::collections::HashMap;

use argparse::{ArgParser, ArgType, hashmap_parser, vec_parser};
const LONG_STR: &'static str = r#"Check your proxy settings or contact your network administrator to make sure the proxy server is working. If you don't believe you should be using a proxy server: Go to the Chromium menu > Settings > Show advanced settings... > Change proxy settings... and make sure your configuration is set to "no proxy" or "direct.""#;

fn main() {
    let mut parser = ArgParser::new("argparse".into());
    
    parser.add_opt("length", None, 'l', true,
        LONG_STR, ArgType::Option);
    parser.add_opt("height", None, 'h', true,
        "Height of user in centimeters", ArgType::Option);
    parser.add_opt("name", None, 'n', true,
        "Name of user", ArgType::Option);
    parser.add_opt("frequencies", None, 'f', false,
        "User's favorite frequencies", ArgType::List);
    parser.add_opt("mao", Some("false"), 'm', false,
        "Is the User Chairman Mao?", ArgType::Flag);
    parser.add_opt("socks", None, 's', false,
        "If you wear socks that day", ArgType::Dict);
    
    let test_1 = "./go -l -60 -h -6001.45e-2 -n Johnny -m -f 1 2 3 4 5 -s Monday:true Friday:false".split_whitespace()
        .map(|s| s.into())
        .collect::<Vec<String>>();

    let p_res = parser.parse(test_1.iter()).unwrap();

    let str_to_veci32 = |s: &str| {
        Some(s.split_whitespace().map(|s| s.parse().unwrap())
            .collect::<Vec<i32>>())
    };
    
    assert!(p_res.get("length") == Some(-60));
    assert_eq!(p_res.get("height"), Some(-6001.45e-2));
    assert_eq!(p_res.get::<String>("name"), Some("Johnny".into()));
    assert_eq!(p_res.get_with("frequencies", str_to_veci32), 
        Some(vec![1,2,3,4,5]));
    assert_eq!(p_res.get_with("frequencies", vec_parser), 
        Some(vec![1,2,3,4,5]));
    assert_eq!(p_res.get("mao"), Some(true));
    
    let h = [("Monday", true), ("Friday", false)]
        .iter()
        .map(|&(k, v)| (k.into(), v))
        .collect();
        
    assert_eq!(p_res.get_with::<HashMap<String, bool>, _>("socks", hashmap_parser),
        Some(h));

    parser.help();
}

Terminal Output:

socks:Some("Monday:true Friday:false ")
length:Some("-60")
frequencies:Some("1 2 3 4 5 ")
height:Some("-6001.45e-2")
help:Some("true")
name:Some("Johnny")
mao:Some("true")
Usage:	./argparse [--socks k:v k2:v2...] [--length LENGTH] [--frequencies FREQUENCIES...] [--height HEIGHT] [--help ] [--name NAME] [--mao ] 
Options:

--socks (-s)	Required: false	Type: Dict
	If you wear socks that day

--length (-l)	Required: true	Type: Option
	Check your proxy settings or contact your network administrator 
		to make sure the proxy server is working. If you don't believe 
		you should be using a proxy server: Go to the Chromium menu > 
		Settings > Show advanced settings... > Change proxy settings... 
		and make sure your configuration is set to "no proxy" or "direct."

--frequencies (-f)	Required: false	Type: List
	User's favorite frequencies

--height (-h)	Required: true	Type: Option
	Height of user in centimeters

--help (-h)	Required: false	Type: Flag
	Show this help message

--name (-n)	Required: true	Type: Option
	Name of user

--mao (-m)	Required: false	Type: Flag
	Is the User Chairman Mao?

No runtime deps