1 unstable release
0.0.4 | Sep 27, 2023 |
---|---|
0.0.3 |
|
0.0.2 |
|
0.0.1 |
|
#7 in #clip
22KB
134 lines
RCLIP-CMD (Rust Command Line Interface Parser)
RCLIP-CMD (Rust Command Line Interface Parser) is a Rust library designed to simplify the process of parsing command-line arguments and options for your Rust applications. With RCLIP-CMD, you can easily define and manage command-line options, parse arguments, and access the values of specified options.
Table of Contents
1. Features
- Define and manage command-line options.
- Parse command-line arguments.
- Access the values of specified options.
- Print a help screen.
2. Getting Started
To get started with RCLIP-CMD, follow these steps:
1. Add RCLIP-CMD to Your Dependencies
To use RCLIP-CMD in your Rust project, add it to your Cargo.toml
file as a dependency:
[dependencies]
rclip-cmd = "1.0.0"
This step ensures that your project can access the RCLIP-CMD library.
2. Import Necessary Modules
In your Rust code, import the modules and types provided by RCLIP-CMD to use its functionality:
use std::env;
use rclip_cmd::{options_manager::OptionsManager, option::Option};
Here, we import the necessary modules from RCLIP-CMD, including OptionsManager
and Option
, which are essential for defining and managing command-line options.
3. Define Your Command-Line Options
Before you can parse command-line arguments, you need to define the options for your application using the Option
struct from RCLIP-CMD. Each option is configured with a short name, a long name, and additional information about whether it is required and whether it expects an argument.
3. Example
Here's a detailed example of how to use RCLIP-CMD to define, parse, and work with command-line options in Rust:
1. Defining Command-Line Options
In this part of the code, we define the command-line options for our application using the Option
struct from RCLIP-CMD. Each option is configured with various properties:
let options = vec![
Option::new("v".to_string(), "version".to_string(), false, false, "Prints version information".to_string()),
Option::new("o".to_string(), "output".to_string(), false, false, "The output file to write to".to_string()),
Option::new("i".to_string(), "input".to_string(), true, true, "The input file".to_string()),
];
Option::new()
: This function creates a newOption
instance with the specified properties. It takes the short name, long name, whether it's required, whether it expects an argument, and a description as arguments.
2. Creating an OptionsManager
Instance
After defining the command-line options, we create an OptionsManager
instance to manage these options and handle argument parsing:
let mut options_manager = OptionsManager::new("Test Application", options);
OptionsManager::new()
: This constructor creates a newOptionsManager
instance. It takes two arguments: the context (a string representing the application name) and a vector ofOption
objects representing the available command-line options.
3. Parsing Command-Line Arguments
Next, we parse the command-line arguments using the parse_options
function:
let result = options_manager.parse_options(env::args().collect());
-
env::args().collect()
: This collects the command-line arguments into aVec<String>
, which is then passed to theparse_options
function. -
options_manager.parse_options()
: This function parses the provided command-line arguments. It returns aResult
whereOk
contains a vector ofOption
objects representing the parsed options, orErr
contains an error message as a string if parsing fails.
4. Checking if Options Are Present
We use various functions provided by the OptionsManager
instance to check if specific options are present and retrieve their argument values:
if options_manager.is_present("v") {
println!("Version 0.1.0");
}
if options_manager.is_present("i") {
let argument = options_manager.argument("i");
println!("Input: {}", argument);
}
if options_manager.is_present("o") {
let argument = options_manager.argument("o");
println!("Output: {}", argument);
}
-
options_manager.is_present(option_name)
: This function checks if the specified option (by short name) is present in the parsed options. It returnstrue
if the option is found, otherwisefalse
. -
options_manager.argument(option_name)
: This function retrieves the argument value associated with the specified option (by short name) if the option expects an argument. If the option does not have an argument or is not present, it returns an empty string.
5. Handling Errors
Lastly, we handle any errors that might occur during argument parsing:
else {
println!("Error: {}", result.err().unwrap());
}
- If the
parse_options
function returns an error (Err
), we print the error message to the console.
This code provides a complete example of how to define, manage, parse, and work with command-line options in Rust using the RCLIP-CMD library.
4. License
This code is licensed under the GNU General Public License, version 3.0 (GPL-3.0).
5. Disclaimer
This code is provided as-is, and the authors make no warranties regarding its correctness or fitness for any purpose. Please feel free to report issues or submit pull requests to improve this code.