#clap #lazy-static #argument #config #command-line-arguments #cli

argone

最直观的全局 CLI 制作工具。 *(lazy_static + config-rs + clap)

7 个版本 (4 个重大更改)

0.5.2 2022年6月18日
0.5.1 2022年6月18日
0.5.0 2021年12月2日
0.4.0 2021年12月2日
0.1.0 2021年12月1日

#953 in 命令行界面

MIT/Apache

14KB
217

argone


最直观的全局 CLI 制作工具。 *(lazy_static + config-rs + clap)

CI Crates.io Licensed Twitter

| 示例 | 文档 | 最新日志 |

[dependencies]
argone = "0.5"

阶段

  1. 解析 clap
  2. 如果 clap-argument 是 None
    并且参数有 [Config] 标记
    则从 Config(文件或环境) 提取数据。
  3. 当数据被提取且为空时,
    最终设置默认 (=) 值。

示例

* 基础
use argone::{prelude::*, *};

ARGONE! {

    version = "0.1"
    author = "just-do-halee <[email protected]>"
    about = "this is our application"

    Config {
        // file's extension could be
        // ini | json | json5 | yaml | toml | ron
        file = "examples/Config"
        // arg's env-prefix ex:) APP_...
        prefix = "APP"
    }

    Args {

        /// This is just one required argument.
        /// and three slashes will be an 'about' for clap cli.
        name: String

        /// This argument is connected to Config(file or env).
        /// Template is
        /// [Config] $name: Option<$type> = Some($default_value) or
        /// [Config] $name: Vec<$type> = vec![$default_values]
        /// and this works in non-required argument.
        [Config] age: Option<u8> = Some(12),
        [Config] job: Vec<String>

        // And all arugments are working on clap_derive format.

        /// Exactly same with clap_derive.
        (short, long)
        parents: Vec<String>

        /// But (default_value = "..") doesn't work to
        /// the config arguments. instead, this would work.
        (short, long, name = "WEIGHT")
        [Config] weight: Option<u8> = Some(50)

        /// In normal arguments, default_value will work.
        (short, long, default_value = "1")
        verbose: u8

    }

    commands = Sub

}

COMMANDS! {
    Sub {

        /// The subcommand
        /// but subcommands do not have config arguments.
        First {
            version = "1.0"
            author = "just-do-halee <[email protected]>"
            Args {
                /// This area is also same.
                test: String
            }
        }

        Second {
            about = "The second subcommand"
            Args {
                test: u8
            }
        }

    }
}

fn main() {
    println!("{:#?}", *ARGS);

    if ARGS.name == "test" {
        println!("name is test.");
    }

    if ARGS.age.unwrap() != 12 {
        println!("age is not matching default 12.");
    }

    for job in &ARGS.job {
        println!("job: {}", job);
    }

    for parents in &ARGS.parents {
        println!("parent: {}", parents);
    }

    println!(
        "weight is {}\nverbose is {}",
        ARGS.weight.unwrap(),
        ARGS.verbose
    );

    if let Some(sub) = &ARGS.commands {
        match sub {
            Sub::First { test } => println!("first command: {}", test),
            Sub::Second { test } => println!("second command: {}", test),
        }
    } else {
        println!("none subcommands");
    }
}

依赖

~4.5MB
~90K SLoC