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: usize

number of strings in Command.argv

§executable: String

executable 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§

Instantiates and returns a new Command object

Arguments
  • arguments - a Vec<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());

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");
}

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
}

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
}

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
}

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
}

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
}

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")
};

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")
}

Trait Implementations§

Formats the value using the given formatter. Read more
Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Converts the given value to a String. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.