#arguments-parser #argument #arg #parse #parser #cli

macro argopt-impl

Parse command line argument by defining a function

3 releases (breaking)

0.3.0 Feb 13, 2022
0.2.0 Jan 16, 2022
0.1.0 Aug 30, 2020

#100 in #arg

Download history 22/week @ 2023-12-17 30/week @ 2023-12-24 76/week @ 2023-12-31 38/week @ 2024-01-07 29/week @ 2024-01-14 10/week @ 2024-01-21 9/week @ 2024-01-28 11/week @ 2024-02-04 23/week @ 2024-02-11 59/week @ 2024-02-18 83/week @ 2024-02-25 44/week @ 2024-03-03 75/week @ 2024-03-10 48/week @ 2024-03-17 65/week @ 2024-03-24 78/week @ 2024-03-31

275 downloads per month
Used in 7 crates (via argopt)

MIT license

16KB
377 lines

Crates.io Workflow Status

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.

#[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

You can customize the behavior of arguments by annotating them with #[opt(...)] attributes.

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

And you can add help messages by adding doccomments.

/// 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,
) {
    // ...
}

You can also use the #[opt(...)] attribute to customize the behavior of an application.

/// 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

The available options are the same as those of clap::Parser.

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])]
#[opt(author, version, about, long_about = None)]
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");
}
$ 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

You can also use verbose option to subcommand application.

...

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

Dependencies

~2MB
~41K SLoC