argparse_rs/
lib.rs

1//! argparser-rs
2//! =========
3//! 
4//! A simple argument parser, meant to parse command line input. It is inspired by the Python ArgumentParser
5//! 
6//! 
7//! Simple class to parse arguments. The Highlights are:
8//!  * A configurable `add_opt` method, to tell it what to look for.
9//!  * A generic `get` method, which will return the argument you want with any type that implements `FromStr`.
10//!  * The arguments are stored in maps, and so are accessed by the name you give them in the `add_opt` method.
11//!  * The `parse` method can be called on an `Iterator` that produces `&String`s.
12//!  * There are no static or global variables so you can have as many parsers as you want.
13//!  * The parser can take any values that implement `FromStr` or for which you can provide a closure to parse them from String
14//!  * You can specify if any argument is required or not, as well as default values for all
15//!  * It also prints a default help message, similar to the one Python's argparser prints
16//! 
17//! Example use:
18//! 
19//! ```rust
20//! extern crate argparse;
21//! 
22//! use std::collections::HashMap;
23//! 
24//! use argparse::{ArgParser, ArgType, hashmap_parser, vec_parser};
25//! 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.""#;
26//! 
27//! fn main() {
28//!     let mut parser = ArgParser::new("argparse".into());
29//!     
30//!     parser.add_opt("length", None, 'l', true,
31//!         LONG_STR, ArgType::Option);
32//!     parser.add_opt("height", None, 'h', true,
33//!         "Height of user in centimeters", ArgType::Option);
34//!     parser.add_opt("name", None, 'n', true,
35//!         "Name of user", ArgType::Option);
36//!     parser.add_opt("frequencies", None, 'f', false,
37//!         "User's favorite frequencies", ArgType::List);
38//!     parser.add_opt("mao", Some("false"), 'm', false,
39//!         "Is the User Chairman Mao?", ArgType::Flag);
40//!     parser.add_opt("socks", None, 's', false,
41//!         "If you wear socks that day", ArgType::Dict);
42//!     
43//!     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()
44//!         .map(|s| s.into())
45//!         .collect::<Vec<String>>();
46//! 
47//!     let p_res = parser.parse(test_1.iter()).unwrap();
48//! 
49//!     let str_to_veci32 = |s: &str| {
50//!         Some(s.split_whitespace().map(|s| s.parse().unwrap())
51//!             .collect::<Vec<i32>>())
52//!     };
53//!     
54//!     assert!(p_res.get("length") == Some(-60));
55//!     assert_eq!(p_res.get("height"), Some(-6001.45e-2));
56//!     assert_eq!(p_res.get::<String>("name"), Some("Johnny".into()));
57//!     assert_eq!(p_res.get_with("frequencies", str_to_veci32), 
58//!         Some(vec![1,2,3,4,5]));
59//!     assert_eq!(p_res.get_with("frequencies", vec_parser), 
60//!         Some(vec![1,2,3,4,5]));
61//!     assert_eq!(p_res.get("mao"), Some(true));
62//!     
63//!     let h = [("Monday", true), ("Friday", false)]
64//!         .iter()
65//!         .map(|&(k, v)| (k.into(), v))
66//!         .collect();
67//!         
68//!     assert_eq!(p_res.get_with::<HashMap<String, bool>, _>("socks", hashmap_parser),
69//!         Some(h));
70//! 
71//!     parser.help();
72//! }
73//! ```
74#![warn(missing_docs)]
75
76pub mod argparser;
77pub mod slide;
78
79pub use argparser::{ArgParser, ArgParseResults, ParseResult,
80    ArgType, ArgGetter, hashmap_parser, vec_parser};