3 个版本 (破坏性更新)

0.3.0 2022年2月13日
0.2.0 2022年1月16日
0.1.0 2020年8月30日

#106#arg

Download history 77/week @ 2024-03-11 45/week @ 2024-03-18 69/week @ 2024-03-25 84/week @ 2024-04-01 41/week @ 2024-04-08 61/week @ 2024-04-15 47/week @ 2024-04-22 56/week @ 2024-04-29 46/week @ 2024-05-06 50/week @ 2024-05-13 44/week @ 2024-05-20 33/week @ 2024-05-27 50/week @ 2024-06-03 41/week @ 2024-06-10 35/week @ 2024-06-17 52/week @ 2024-06-24

每月 181 次下载
7 个crate中使用(通过 argopt

MIT 许可证

16KB
377

Crates.io Workflow Status

本crate提供用于命令行参数解析的属性宏。

用法

只需将属性 #[cmd] 添加到函数中,该函数就转换为命令行程序。

#[argopt::cmd]
fn main(host: String, port: u16) {
    // ...
}
$ cargo run
error: The following required arguments were not provided:
    <HOST>
    <PORT>

USAGE:
    argopt-test <HOST> <PORT>

For more information try --help
$ cargo run -- --help
argopt-test 

USAGE:
    argopt-test <HOST> <PORT>

ARGS:
    <HOST>    
    <PORT>    

OPTIONS:
    -h, --help    Print help information

您可以通过使用 #[opt(...)] 属性来自定义参数的行为。

#[argopt::cmd]
fn main(
    #[opt(short = 'h', long = "host")]
    host: String,
    #[opt(short, long, default_value_t = 80)]
    port: u16,
) {
    // ...
}

您还可以通过添加文档注释来添加帮助信息。

/// Sample program
#[argopt::cmd]
fn main(
    /// Host name
    #[opt(short = 'h', long = "host")]
    host: String,
    /// Port number
    #[opt(short, long, default_value_t = 80)]
    port: u16,
) {
    // ...
}

您还可以使用 #[opt(...)] 属性来自定义应用程序的行为。

/// Sample program
#[argopt::cmd]
#[opt(author, version, about, long_about = None)]
fn main(
    /// Host name
    #[opt(short = 'h', long = "host")]
    host: String,
    /// Port number
    #[opt(short, long, default_value_t = 80)]
    port: u16,
) {
    // ...
}
$ cargo run -- --help
argopt-test 0.1.0
Sample program

USAGE:
    argopt-test [OPTIONS] --host <HOST>

OPTIONS:
    -h, --host <HOST>    Host name
        --help           Print help information
    -p, --port <PORT>    Port number [default: 80]
    -V, --version        Print version information

可用选项与 clap::Parser 中的选项相同。

子命令

您可以通过将属性 #[subcmd] 添加到函数中来创建子命令。

use argopt::{subcmd, cmd_group};
use std::path::PathBuf;

#[subcmd]
fn add(
    #[opt(short)]
    interactive: bool,
    #[opt(short)]
    patch: bool,
    files: Vec<PathBuf>,
) {
    // ...
}

#[subcmd]
fn commit(
    #[opt(short)]
    message: Option<String>,
    #[opt(short)]
    all: bool,
) {
    // ...
}

#[cmd_group(commands = [add, commit])]
#[opt(author, version, about, long_about = None)]
fn main() {}

轻松处理详细级别

有一个功能允许您与 log crate 交互并自动处理详细级别。

use argopt::cmd;
use log::*;

#[cmd(verbose)]
fn main() {
    error!("This is error");
    warn!("This is warn");
    info!("This is info");
    debug!("This is debug");
    trace!("This is trace");
}
$ cargo run
This is error

$ cargo run -- -v
This is error
This is warn

$ cargo run -- -vv
This is error
This is warn
This is info

$ cargo run -- -vvv
This is error
This is warn
This is info
This is debug

$ cargo run -- -vvvv
This is error
This is warn
This is info
This is debug
This is trace

您还可以使用 verbose 选项来处理子命令应用程序。

...

#[cmd_group(commands = [add, commit], verbose)]
fn main() {}

依赖关系

~2MB
~43K SLoC