getopt

Struct getopt 

Source
pub struct getopt {
    pub options: HashMap<char, String>,
    pub arguments: Vec<String>,
    pub option_has_arg: HashMap<char, bool>,
}
Expand description

Parsed command line options.

Created by new function. Structure contains isolated command line arguments and collected options with their optional values.

Structure can contain options that are not listed in optstring passed to new function. For strict parse mode pass this structure to validate function.

Fields§

§options: HashMap<char, String>

Map of command line options and their optional values extracted from command line arguments.

If an option does not have a value, the empty string “” is stored.

§arguments: Vec<String>

Isolated command line arguments without options.

§option_has_arg: HashMap<char, bool>

Map indicating whether an option has a required argument.

This map contains all recognized options. The presence of an option in this map does not mean that the option must always be present or supplied as a command line argument.

Implementations§

Source§

impl getopt

Source

pub fn len(&self) -> usize

Returns number of command line arguments not counting options.

It is a convenience shortcut for getopt.arguments.len().

§Example
use std::env::args;
use getopt3::hideBin;

let getopt_rc = getopt3::new(hideBin(args()), "ab:c");
if let Ok(g) = getopt_rc {
   println!("Number of command line arguments is {}", g.len());
};
Source

pub fn iter(&self) -> Iter<'_, String>

Returns Iterator over command line arguments.

It is a convenience shortcut for getopt.arguments.iter().

§Note

Options are skipped by the Iterator.

§Example
use std::env::args;
use getopt3::hideBin;

let getopt_rc = getopt3::new(hideBin(args()), "abc");
if let Ok(my_getopt) = getopt_rc {
   for arg in my_getopt.iter() {
      println!("Argument: {}", arg);
   }
}
Source

pub fn get(&self, option: char) -> Option<&String>

Return command line option value.

It is a convenience shortcut for getopt.options.get().

§Return value
  1. If option was supplied on command line returned value is Some.
  2. If option doesn’t have argument or argument is missing, a reference to an empty String is returned.
  3. If option were not supplied by user returned value is None.
§Example
use std::env::args;
use getopt3::hideBin;

let getopt_rc = getopt3::new(hideBin(args()), "ab:c");
if let Ok(my_getopt) = getopt_rc {
   if let Some(b_value) = my_getopt.get('b') {
      println!("-b argument is: {}", b_value);
   }
}
Source

pub fn has(&self, option: char) -> bool

Check if command line option was supplied on command line.

It is a convenience shortcut for getopt.options.contains_key().

§Return value
  1. If option was supplied on command line returned value is true.
  2. If option was not supplied by user returned value is false.
§Example
use std::env::args;
use getopt3::hideBin;

let getopt_rc = getopt3::new(hideBin(args()), "ab:c");
if let Ok(my_getopt) = getopt_rc {
   if my_getopt.has('b') {
      println!("-b argument is: {}", my_getopt.get('b').unwrap());
   }
}
Source

pub fn is_empty(&self) -> bool

Check if any non option arguments were supplied.

It is a convenience shortcut for getopt.arguments.is_empty().

§Return value
  1. If any arguments were supplied on the command line returned value is true.
  2. If no arguments were supplied on the command line returned value is false.
§Example
use std::env::args;
use getopt3::hideBin;

let getopt_rc = getopt3::new(hideBin(args()), "ab:c");
if let Ok(my_getopt) = getopt_rc {
   if !my_getopt.is_empty() {
      println!("Arguments were supplied on command line");
   }
}

Trait Implementations§

Source§

impl Index<usize> for getopt

Access to positional arguments by index.

Source§

type Output = String

The returned type after indexing.
Source§

fn index(&self, index: usize) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<'a> IntoIterator for &'a getopt

Iterates over arguments without consuming getopt.

Source§

type Item = &'a String

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, String>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl IntoIterator for getopt

Consumes getopt and returns Iterator over arguments.

Source§

type Item = String

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<String>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more

Auto Trait Implementations§

§

impl Freeze for getopt

§

impl RefUnwindSafe for getopt

§

impl Send for getopt

§

impl Sync for getopt

§

impl Unpin for getopt

§

impl UnwindSafe for getopt

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.