Struct commandlines::Command
source · pub struct Command {
pub argv: Vec<String>,
pub argc: usize,
pub executable: String,
pub options: Vec<String>,
pub definitions: HashMap<String, String>,
}Expand description
A command line argument object
The Command struct defines fields that hold parsed command line argument data and provides methods that can be used to define the logic of a command line interface application.
Examples
Instantiation
Instantiate a Command struct with a Vec<String> that is defined with std::env::args().collect():
extern crate commandlines;
use commandlines::Command;
let c = Command::new(std::env::args().collect());Fields§
§argv: Vec<String>Vector of command line strings defined on instantiation
argc: usizenumber of strings in Command.argv
executable: Stringexecutable at index position 0 of Command.argv
options: Vec<String>Vector of command line options in Command.argv
definitions: HashMap<String, String>HashMap of command line option definitions mapped as key=option:value=definition
Implementations§
source§impl Command
impl Command
sourcepub fn new(arguments: Vec<String>) -> Self
pub fn new(arguments: Vec<String>) -> Self
Instantiates and returns a new Command object
Arguments
arguments- aVec<String>of command line arguments
Remarks
The command line arguments passed to the executable should be defined with std::env::args().collect().
Examples
extern crate commandlines;
let c = commandlines::Command::new(std::env::args().collect());sourcepub fn has_args(&self) -> bool
pub fn has_args(&self) -> bool
Returns a boolean for the question “Does the command include any arguments?”
Remarks
An argument is defined as a command line string after the executable. The executable at index position 0 in the Vec<String> returned by std::env::args().collect() is not part of this definition.
Examples
let c = commandlines::Command::new(std::env::args().collect());
if !c.has_args() {
eprintln!("{}", "Missing arguments");
}sourcepub fn has_definitions(&self) -> bool
pub fn has_definitions(&self) -> bool
Returns a boolean for the question “Does the command include any definition options?”
Remarks
A definition option is defined as a command line string that takes a short or long option format with an equal character that is used to indicate that a definition of the option follows. They may take either of the following formats:
-o=def--option=def
Examples
let c = commandlines::Command::new(std::env::args().collect());
if c.has_definitions() {
// definitions were parsed in the command
}sourcepub fn has_options(&self) -> bool
pub fn has_options(&self) -> bool
Returns a boolean for the question “Does the command include any options?”
Remarks
An option is defined as a command line string that starts with one or two hyphen characters. This definition includes standard long (e.g., --longoption) and short (e.g., -s) command line options.
Examples
let c = commandlines::Command::new(std::env::args().collect());
if c.has_options() {
// start application-specific option parsing logic
}sourcepub fn contains_arg(&self, needle: &str) -> bool
pub fn contains_arg(&self, needle: &str) -> bool
Returns a boolean for the question “Does the command include the argument string needle?” at any index
Examples
let c = commandlines::Command::new(std::env::args().collect());
if c.contains_arg("spam") {
// a `spam` argument was in the command
}sourcepub fn contains_definition(&self, needle: &str) -> bool
pub fn contains_definition(&self, needle: &str) -> bool
Returns a boolean for the question “Does the command include the definition option needle?”
Examples
let c = commandlines::Command::new(std::env::args().collect());
if c.contains_definition("--spam") {
// command included a `--spam=[definition]` option
}sourcepub fn contains_option(&self, needle: &str) -> bool
pub fn contains_option(&self, needle: &str) -> bool
Returns a boolean for the question “Does the command include the option string needle?” at any index
Examples
let c = commandlines::Command::new(std::env::args().collect());
if c.contains_option("--help") {
// you have a standard request for help documentation
}sourcepub fn get_definition_for(&self, needle: &str) -> Option<&String>
pub fn get_definition_for(&self, needle: &str) -> Option<&String>
Returns Option<&String> definition for a key defined by needle
Returns None if the option was not used in the command
Examples
The following example demonstrates how to get the definition string for a command line option with the format --name=[definition]:
let c = commandlines::Command::new(std::env::args().collect());
match c.get_definition_for("--name") {
Some(x) => println!("{}", *x),
None => eprintln!("{}", "Missing")
};sourcepub fn get_argument_after(&self, needle: &str) -> Option<&String>
pub fn get_argument_after(&self, needle: &str) -> Option<&String>
Returns Option<&String> for argument at index position i+1 for needle at index position i
Returns None if needle is the last positional argument in the command
Examples
let c = commandlines::Command::new(std::env::args().collect());
match c.get_argument_after("-o") {
Some(x) => println!("The argument after -o is {}", *x),
None => eprintln!("-o is the last positional argument in the command")
}