Struct CommandLine

Source
pub struct CommandLine { /* private fields */ }
Expand description

Stores the parsed command line

Implementations§

Source§

impl CommandLine

Source

pub fn options(self) -> usize

Returns the number of options parsed

§Examples
 use std::collections::VecDeque;
 use std::env;
 use cl_parse::{CommandLine, CommandLineDef};
 // Simulate env::args()
 let env_args=vec![String::from("program"), String::from("-f"), String::from("/file/path"), String::from("arg1")];
 let cl = CommandLineDef::new()
  .add_option(vec!["-f","--filename"], Some("filepath"), None, "The file to be parsed")
  .add_flag(vec!["-b"], "A binary flag option")
  .add_argument("arg-0")
  .parse(env_args.into_iter());

  // Test Program Name
  assert_eq!(cl.options(), 5); // -f, --filename, -b, -h, --help
Source

pub fn option<T>(&self, name: &str) -> T
where T: FromStr,

Returns the option for the option key specified

§Arguments
  • name - A string slice that holds the name of the option
§Examples
 use std::collections::VecDeque;
 use std::env;
 use cl_parse::{CommandLine, CommandLineDef};
 // Simulate env::args()
 let env_args=vec![String::from("program"), String::from("-f"), String::from("/file/path")];
 let cl = CommandLineDef::new().add_option(vec!["-f","--filename"], Some("filepath"),
     None, "The file to be parsed").parse(env_args.into_iter());
 let filename:String = cl.option("-f");
  // Test Program Name
  assert_eq!(filename, "/file/path".to_string());
Source

pub fn arguments(&self) -> usize

Returns the number of arguments parsed

§Examples
 use std::collections::VecDeque;
 use std::env;
 use cl_parse::{CommandLine, CommandLineDef};
 // Simulate env::args()
 let env_args=vec![String::from("program"), String::from("-f"), String::from("/file/path")];
 let cl = CommandLineDef::new().add_option(vec!["-f","--filename"], Some("filepath"),
     None, "The file to be parsed").parse(env_args.into_iter());

  // Test Program Name
  assert_eq!(cl.arguments(), 0);
Source

pub fn argument<T>(&self, index: usize) -> T
where T: FromStr,

Returns the argument based by index

§Examples
 use std::collections::VecDeque;
 use std::env;
 use cl_parse::{CommandLine, CommandLineDef};
 // Simulate env::args()
 let env_args=vec![String::from("program"), String::from("-f"), String::from("/file/path"), String::from("arg1")];
 let cl = CommandLineDef::new()
  .add_option(vec!["-f","--filename"], Some("filepath"), None, "The file to be parsed")
  .add_argument("arg-0")
  .parse(env_args.into_iter());

 let arg0:String = cl.argument(0);
 assert_eq!(arg0, "arg1");
Source

pub fn program_name(&self) -> &str

Returns the program name specified on the command line

§Examples
 use std::collections::VecDeque;
 use std::env;
 use cl_parse::{CommandLine, CommandLineDef};
 // Simulate env::args()
 let env_args=vec![String::from("program"), String::from("-f"), String::from("/file/path")];
 let cl = CommandLineDef::new().add_option(vec!["-f","--filename"], Some("filepath"),
     None, "The file to be parsed").parse(env_args.into_iter());

  // Test Program Name
  assert_eq!(false, cl.program_name().is_empty());

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> 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.