#arguments-parser #argument #parser #terminal #command-line #argparser

argparse-rs

一个简单的参数解析器,用于解析命令行输入。它受到了 Python 的 ArgumentParser 的启发

1 个不稳定版本

使用旧的 Rust 2015

0.1.0 2016年1月15日

#75#argument

Download history 26/week @ 2024-03-11 31/week @ 2024-03-18 36/week @ 2024-03-25 285/week @ 2024-04-01 25/week @ 2024-04-08 25/week @ 2024-04-15 60/week @ 2024-04-22 28/week @ 2024-04-29 27/week @ 2024-05-06 23/week @ 2024-05-13 24/week @ 2024-05-20 23/week @ 2024-05-27 33/week @ 2024-06-03 31/week @ 2024-06-10 33/week @ 2024-06-17 50/week @ 2024-06-24

151 每月下载次数
gnuplot 使用

MIT 许可证

35KB
499

argparser-rs

一个简单的参数解析器,用于解析命令行输入。它受到了 Python 的 ArgumentParser 的启发

简单的类,用于解析参数。其亮点包括

  • 可配置的 add_opt 方法,用于指定要查找的内容。
  • 通用的 get 方法,它可以返回任何实现了 FromStr 的类型的参数。
  • 参数存储在映射中,因此可以通过在 add_opt 方法中提供的名称来访问。
  • parse 方法可以用于产生 &StringIterator
  • 没有静态或全局变量,因此你可以创建任意数量的解析器。
  • 解析器可以接受任何实现了 FromStr 的值,或者为你提供从字符串解析它们的闭包。
  • 你可以指定任何参数是否必需,以及所有参数的默认值。
  • 它还会打印一个默认的帮助信息,类似于 Python 的 argparser 打印的信息。

示例用法

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();
}

终端输出

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?

无运行时依赖