#argument #cli #arg #parser #parse

arg

Very simple code generator for command line argument parsing

5 unstable releases

0.3.1 Aug 25, 2021
0.3.0 May 6, 2020
0.2.0 Oct 24, 2019
0.1.1 Oct 17, 2019
0.1.0 Oct 16, 2019

#253 in Command-line interface

Download history 29/week @ 2021-10-07 20/week @ 2021-10-14 26/week @ 2021-10-21 9/week @ 2021-10-28 13/week @ 2021-11-04 4/week @ 2021-11-11 23/week @ 2021-11-18 28/week @ 2021-11-25 29/week @ 2021-12-02 11/week @ 2021-12-09 20/week @ 2021-12-16 10/week @ 2021-12-23 13/week @ 2021-12-30 7/week @ 2022-01-06 19/week @ 2022-01-13 25/week @ 2022-01-20

65 downloads per month

Apache-2.0

13KB
193 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.

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

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.

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.

Usage

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 felocity. 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
    remain_paths: Vec<String>,
}

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

Dependencies

~330–760KB
~18K SLoC