Rust 版本的 commander.js,了解一下? - V2EX
2019-05-02 21:51:29 Author: www.v2ex.com(查看原文) 阅读量:234 收藏

不得不说,Rust 的宏给了我耳目一新的感觉。这么强大的宏,鄙人还是第一次见。 受到 commander.js 和 rocket.rs 的启发,我想能不能把 rust 的宏用于开发 CLI 程序,在经过一段时间的思考和行动之后,终于还是成功了。 这对于我而言是个挑战,毕竟 Rust 这门语言我才接触不久。

github 地址: commander-rust 主页:commadner-rust 主页 (主页由于没时间了,没做移动端适配。。。)

大概的构思是这样的,我想开发 cli 可以这样开发:

#![feature(proc_macro_hygiene)]

use commander_rust::{option, command, entry, Cli, run};

#[option(-c, --cn, "Chinese")]
#[option(-e, --en, "English")]
#[option(-j, --jp, "Japanese")]
#[command(hello, "Say hello")]
fn hello(cli: Cli) {
    if cli.has("cn") {
        println!("你好,世界");
    } else if cli.has("en") {
        println!("hello, world!");
    } else if cli.has("jp") {
        println!("こんにちは、世界");
    }
}

#[entry]
fn main() { run!(); }

这样的话,我个人觉得逻辑和布局上都比较清楚。 大家有什么好的意见吗?不要脸地求 star。。。


文章来源: https://www.v2ex.com/t/560576#reply6
如有侵权请联系:admin#unsafe.sh