pub struct CommandLine { /* private fields */ }Expand description
Stores the parsed command line
Implementations§
Source§impl CommandLine
impl CommandLine
Sourcepub fn options(self) -> usize
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, --helpSourcepub fn option<T>(&self, name: &str) -> Twhere
T: FromStr,
pub fn option<T>(&self, name: &str) -> Twhere
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());Sourcepub fn arguments(&self) -> usize
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);Sourcepub fn argument<T>(&self, index: usize) -> Twhere
T: FromStr,
pub fn argument<T>(&self, index: usize) -> Twhere
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");
Sourcepub fn program_name(&self) -> &str
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§
impl Freeze for CommandLine
impl RefUnwindSafe for CommandLine
impl Send for CommandLine
impl Sync for CommandLine
impl Unpin for CommandLine
impl UnwindSafe for CommandLine
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more