2 个版本

新版本 0.1.1 2024 年 8 月 27 日
0.1.0 2024 年 8 月 26 日

#790命令行界面

Download history · Rust 包仓库 50/week @ 2024-08-20 · Rust 包仓库

63 每月下载量

MIT AND BSD-2-Clause

18KB
305

声明式宏命令行解析器(受 argwerk 启发)。

argtea(发音为 arg tea)的目标是使用文档注释自动生成帮助页面。argtea 尝试比 argwerk 更灵活、更少抽象。

示例项目

use argtea::{argtea_impl, simple_format};

#[derive(Debug)]
pub struct Arguments {
    output_path: String,
    files: Vec<String>,
}

fn main() -> Result<(), &'static str> {
    let args = Arguments::parse()?;

    println!("input files: {:?}", args.files);
    println!("output file: {:?}", args.output_path);

    Ok(())
}

argtea_impl! {
    {
        /// Displays this help message.
        ("--help" | "-h") => {
            eprintln!("{}", Self::HELP);

            std::process::exit(0);
        },

        /// Sets the output file path.
        ("--output" | "-o", output_path) => {
            output_path_ = output_path;
        },

        /// Adds a file as an input.
        ///
        /// To input a file that starts with a `-`, prefix it with a `./`
        (file) => {
            files.push(file);
        },
    }

    impl Arguments {
        const HELP: &'static str = simple_format!(
            "argtea_test: a demo argtea project"
            ""
            "Usage: "
            "  `argtea_test [FLAGS] [FILES]`"
            ""
            "Options:"
            docs!()
        );

        fn parse() -> Result<Self, &'static str> {
            let mut args = std::env::args().skip(1);

            let mut files = Vec::new();
            let mut output_path_ = None;

            parse!(args);

            return Ok(Self {
                files,
                output_path: output_path_.unwrap_or_else(|| "a.out".to_owned())
            });
        }
    }
}

来自 argtea_test -h 的输出

argtea_test: a demo argtea project

Usage:
  `argtea_test [FLAGS] [FILES]`

Options:
  --help, -h
    Displays this help message.


  --output, -o <output_path>
    Sets the output file path.


  <file>
    Adds a file as an input.

    To input a file that starts with a `-`, prefix it with a `./`

无运行时依赖