#parser #arguments-parser #command-line-arguments #cli-parser

no-std arg

Very simple code generator for command line argument parsing

7 unstable releases

0.4.1 Jan 4, 2023
0.4.0 Dec 31, 2022
0.3.1 Aug 25, 2021
0.3.0 May 6, 2020
0.1.1 Oct 17, 2019

#647 in Command-line interface

Download history 4/week @ 2024-06-17 38/week @ 2024-06-24 22/week @ 2024-07-01 12/week @ 2024-07-08 28/week @ 2024-07-15 42/week @ 2024-07-22 55/week @ 2024-07-29 30/week @ 2024-08-05 38/week @ 2024-08-12 2/week @ 2024-08-19 8/week @ 2024-08-26 42/week @ 2024-09-02 21/week @ 2024-09-09 51/week @ 2024-09-16 49/week @ 2024-09-23 40/week @ 2024-09-30

162 downloads per month
Used in 2 crates

Apache-2.0

18KB
258 lines

arg

Actions Status Crates.io Documentation

Very simple code generator for command line argument parsing.

No dependencies.

Features

  • std - Enables utilities that require std library.

Debug

Set env variable ARG_RS_PRINT_PARSER to any value except 0 or false to print parser result


lib.rs:

Arg is simple command line argument parser, without any dependencies

Features

  • std - Enables utilities that require std library.

Syntax

Fields

Arg

  • short - Specifies that it is flag with short switch. Optionally can be supplied with flag.
  • long - Specifies that it is flag with long switch. Optionally can be supplied with flag.
  • default_value - Specifies default value to use. Can be supplied with initialization expression as string. Otherwise uses Default trait.
  • required - Specifies whether argument is required. By default all arguments are optional. But booleans cannot be marked as required
  • sub - Specifies field to be sub-command. There can be only one sub-command and it is mutually exclusive with Vec<_> argument to collect rest of arguments. All other options are not applied to sub type of field.

Types

  • Flag - is bool switch, automatically selected when bool is type of argument. Each time flag is supplied it results in !previous_state
  • Option - switch that accepts value. Used for any non-Vec type. Automatically overrides.
  • Multi Option - switch with Vec<T> type, which allows to accumulate multiple values of switch.
  • Argument - Plain argument that takes value.
  • Multi argument - Collection of arguments that accumulates into Vec<T>, there can be only one.
  • Sub-command - Propagates rest of arguments to another parser, there ca be only one.

Conversion

By default all types, aside from bool flags use FromStr::from_str to parse value from string.

Optional

If type is Option<T> then argument is assumed to be optional, in which case it cannot be marked with required or default_value

As result, not providing argument shall not fail parser.

Sub-command

It relies on enum to represent sub-commands.

Note that when sub-command is used, it is no longer possible to collect multiple arguments into array, resulting in compilation error.

Sub-command consumes all remaining arguments, so top command flags/options must be passed prior sub-command invocation.

use arg::Args;

#[derive(Args, Debug)]
///First
struct First {
    #[arg(short, long)]
    ///About this flag
    flag: bool,

    #[arg(short = "v", long = "velocity", default_value = "42")]
    ///This is felocity. Default value is 42.
    speed: u32,
}

#[derive(Args, Debug)]
///Second
struct Second {
    #[arg(short = "v", long = "velocity", default_value = "42")]
    ///This is velocity. Default value is 42.
    speed: u32,
    ///To store rest of paths
    paths: Vec<String>,
}

#[derive(Args, Debug)]
///My subcommand with implicit command 'help` to list commands
enum MySubCommand {
    ///my first command
    First(First),
    ///my second command
    Second(Second),
}

#[derive(Args, Debug)]
struct MyArgs {
    #[arg(short, long)]
    ///About this flag
    verbose: bool,
    #[arg(sub)]
    ///My sub command. Use `help` to show list of commands.
    cmd: MySubCommand
}

Usage

Here is comprehensive example to illustrate all ways to handle flags and options

use arg::Args;

#[derive(Args, Debug)]
///my_exe 0.1.0
///About my program
///
///About my program
struct MyArgs {
    #[arg(short, long)]
    ///About this flag
    flag: bool,

    #[arg(long = "verbose")]
    ///Verbose mode
    verbose: Option<bool>,

    #[arg(short = "v", long = "velocity", default_value = "42")]
    ///This is velocity. Default value is 42.
    speed: u32,

    #[arg(short = "g", long = "gps")]
    ///GPS coordinates.
    gps: Vec<u32>,

    ///To store path
    path: String,

    ///To store path 2
    path2: String,

    ///To store rest of paths as multi argument collector
    remain_paths: Vec<String>,
}

fn main() {
    match MyArgs::from_text("-v path1 path2") {
        Ok(args) => println!("args={:?}", args),
        Err(err) => println!("err={:?}", err),
    }
}

Dependencies

~1.3–2MB
~36K SLoC