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 |
#1026 in Command-line interface
58 downloads per month
Used in 2 crates
18KB
258 lines
arg
Very simple code generator for command line argument parsing.
No dependencies.
Features
std
- Enables utilities that requirestd
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 requirestd
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 asrequired
sub
- Specifies field to be sub-command. There can be only one sub-command and it is mutually exclusive withVec<_>
argument to collect rest of arguments. All other options are not applied tosub
type of field.
Types
- Flag - is
bool
switch, automatically selected whenbool
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.4–2MB
~36K SLoC