Skip to main content

Getopt

Struct Getopt 

Source
pub struct Getopt {
    pub optind: usize,
    pub optopt: char,
    pub optarg: Option<String>,
    pub opterr: bool,
    /* private fields */
}
Expand description

A BSD-style command-line option parser.

Getopt iterates over command-line arguments and returns options one by one, mimicking the behavior of traditional Unix getopt.

The parser maintains internal state such as:

  • optind: index of the next argument to process
  • optarg: argument associated with the current option (if any)
  • optopt: the last option character processed
  • opterr: whether to print error messages

§Example

use bsd_getopt::Getopt;

let args = vec![
    "prog".to_string(),
    "-a".to_string(),
    "-bvalue".to_string(),
];

let mut g = Getopt::new("ab:", args);

assert_eq!(g.next(), Some('a'));
assert_eq!(g.next(), Some('b'));
assert_eq!(g.optarg, Some("value".to_string()));
assert_eq!(g.next(), None);

Fields§

§optind: usize§optopt: char§optarg: Option<String>§opterr: bool

Implementations§

Source§

impl Getopt

Source

pub fn new(optstring: &str, args: Vec<String>) -> Self

Creates a new Getopt parser.

§Parameters
  • optstring: A string describing valid options.
  • args: Command-line arguments (typically from std::env::args()).
§Behavior
  • Parsing starts from index 1 (skipping program name)
  • Internal state is initialized to default values
§Example
use bsd_getopt::Getopt;

let args = vec!["prog".to_string(), "-a".to_string()];
let parser = Getopt::new("a", args);
Source

pub fn next(&mut self) -> Option<char>

Returns the next option character, or None if parsing is complete.

This function mimics the behavior of the standard getopt:

  • Returns Some(char) for a valid option
  • Returns Some('?') for an unknown option
  • Returns Some(':') if an argument is missing and optstring starts with :
  • Returns None when no more options are available
§Side Effects
  • Updates optind, optarg, and optopt
  • May print errors to stderr if opterr is true
§Rules
  • Options can be grouped: -abc
  • Options with arguments:
    • -o value
    • -ovalue
  • -- stops option parsing
§Example
use bsd_getopt::Getopt;

let args = vec![
    "prog".to_string(),
    "-a".to_string(),
    "-b".to_string(),
    "foo".to_string(),
];

let mut g = Getopt::new("ab:", args);

assert_eq!(g.next(), Some('a'));
assert_eq!(g.next(), Some('b'));
assert_eq!(g.optarg, Some("foo".to_string()));
assert_eq!(g.next(), None);

Trait Implementations§

Source§

impl Clone for Getopt

Source§

fn clone(&self) -> Getopt

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Getopt

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.