Crates.io | rclip-cmd |
lib.rs | rclip-cmd |
version | 0.0.4 |
source | src |
created_at | 2023-09-27 09:58:35.310566 |
updated_at | 2023-09-27 11:07:48.193884 |
description | A command line interface parser for rust |
homepage | |
repository | https://github.com/drew-chase/rclip |
max_upload_size | |
id | 984467 |
size | 58,789 |
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
To get started with RCLIP-CMD, follow these steps:
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.
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.
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.
Here's a detailed example of how to use RCLIP-CMD to define, parse, and work with command-line options in Rust:
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 new Option
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.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 new OptionsManager
instance. It takes two arguments: the context (a string representing the application name) and a vector of Option
objects representing the available command-line options.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 a Vec<String>
, which is then passed to the parse_options
function.
options_manager.parse_options()
: This function parses the provided command-line arguments. It returns a Result
where Ok
contains a vector of Option
objects representing the parsed options, or Err
contains an error message as a string if parsing fails.
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 returns true
if the option is found, otherwise false
.
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.
Lastly, we handle any errors that might occur during argument parsing:
else {
println!("Error: {}", result.err().unwrap());
}
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.
This code is licensed under the GNU General Public License, version 3.0 (GPL-3.0).
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.