#argument #cli #arg #parser #parse

macro argopt-impl

Parse command line argument by defining a function

2 unstable releases

Uses new Rust 2021

0.2.0 Jan 16, 2022
0.1.0 Aug 30, 2020

#12 in #arg

Download history 154/week @ 2021-10-04 187/week @ 2021-10-11 94/week @ 2021-10-18 79/week @ 2021-10-25 181/week @ 2021-11-01 5/week @ 2021-11-08 120/week @ 2021-11-15 164/week @ 2021-11-22 191/week @ 2021-11-29 46/week @ 2021-12-06 42/week @ 2021-12-13 3/week @ 2021-12-20 5/week @ 2021-12-27 10/week @ 2022-01-03 26/week @ 2022-01-10 27/week @ 2022-01-17

69 downloads per month
Used in 2 crates (via argopt)

MIT license

15KB
374 lines

Crates.io Workflow Status

argopt

This crate provides attribute macros for command-line argument parsing.

Usage

Just by adding an attribute #[cmd] to a function, the function is converted to a command line program.

use argopt::cmd;

#[cmd]
fn main(host: String, port: u16) {
    // ...
}

The output is:

$ cargo run
error: The following required arguments were not provided:
    <host>
    <port>

USAGE:
    argopt-test <host> <port>

You can customize the behavior of arguments by annotating them with attributes.

use argopt::cmd;

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

And you can add help messages by adding doccomments.

use argopt::cmd;

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

The output is:

argopt-test 0.1.0
Sample program

USAGE:
    simple [OPTIONS] --host <host>

FLAGS:
        --help       Prints help information
    -V, --version    Prints version information

OPTIONS:
    -h, --host <host>    Host name
    -p, --port <port>    Port number [default: 80]

You can use the same options as structopt.

Subcommands

You can create sub commands by adding the attribute #[subcmd] to functions.

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])]
fn main() {}

Easy Verbosity Level Handling

There is a feature that allows you to interact with the log crate and handle the verbosity level automatically.

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

The output is:

$ 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

License: MIT

Dependencies

~0.6–1MB
~24K SLoC