Crates.io | rust-arguments |
lib.rs | rust-arguments |
version | |
source | src |
created_at | 2025-03-16 01:36:12.924117+00 |
updated_at | 2025-03-16 01:36:12.924117+00 |
description | A Flexible Command-line argument parser |
homepage | https://github.com/linuxfanboy4/rust-arguments |
repository | https://github.com/linuxfanboy4/rust-arguments.git |
max_upload_size | |
id | 1594044 |
Cargo.toml error: | TOML parse error at line 18, column 1 | 18 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
The Rust Arguments Parser is a robust and powerful library designed to facilitate the parsing of command-line arguments in Rust applications. It provides a highly configurable and extensible framework for defining, validating, and processing command-line inputs, making it an indispensable tool for developers who require advanced argument handling in their projects.
-
), long (--
), or positional formats.To integrate the Rust Arguments Parser into your project, add the following to your Cargo.toml
:
[dependencies]
rust-arguments = { git = "https://github.com/linuxfanboy4/rust-arguments.git" }
Below is a simple example demonstrating how to define and parse command-line arguments using the Rust Arguments Parser:
use rust_arguments::{ArgParser, ArgMatches};
fn main() {
let parser = ArgParser::new()
.arg("input")
.short("input", 'i')
.long("input", "input-file")
.takes_value("input")
.required("input")
.default("input", "default.txt")
.validator("input", |val| val.ends_with(".txt"));
let args: Vec<String> = std::env::args().collect();
let matches = parser.parse(&args);
println!("Input file: {}", matches.values.get("input").unwrap());
}
For more complex applications, subcommands can be utilized to create a hierarchical command structure:
use rust_arguments::{ArgParser, ArgMatches};
fn main() {
let sub_parser = ArgParser::new()
.arg("output")
.short("output", 'o')
.long("output", "output-file")
.takes_value("output")
.required("output");
let parser = ArgParser::new()
.arg("input")
.short("input", 'i')
.long("input", "input-file")
.takes_value("input")
.required("input")
.subcommand("process", sub_parser);
let args: Vec<String> = std::env::args().collect();
let matches = parser.parse(&args);
if let Some(sub_matches) = matches.subcommand {
println!("Processing with output file: {}", sub_matches.values.get("output").unwrap());
} else {
println!("Input file: {}", matches.values.get("input").unwrap());
}
}
ArgParser
The ArgParser
struct is the core component of the library, responsible for defining and parsing command-line arguments.
new()
: Initializes a new ArgParser
instance.arg(name: &str)
: Adds a new argument with the specified name.short(name: &str, short: char)
: Assigns a short flag to the specified argument.long(name: &str, long: &str)
: Assigns a long flag to the specified argument.takes_value(name: &str)
: Specifies that the argument requires a value.required(name: &str)
: Marks the argument as required.default(name: &str, default: &str)
: Sets a default value for the argument.validator(name: &str, validator: F)
: Attaches a custom validation function to the argument.subcommand(name: &str, parser: ArgParser)
: Adds a subcommand to the parser.parse(args: &[String])
: Parses the provided arguments and returns an ArgMatches
instance.ArgMatches
The ArgMatches
struct encapsulates the results of the argument parsing process.
values: HashMap<String, String>
: Contains the values of arguments that require them.flags: HashMap<String, bool>
: Indicates the presence of flag arguments.positionals: Vec<String>
: Holds positional arguments.Contributions to the Rust Arguments Parser are welcome. Please ensure that your contributions adhere to the following guidelines:
To contribute, fork the repository, create a branch for your feature, and submit a pull request.
This project is licensed under the MIT License. See the LICENSE file for details.