Struct FlagSet

Source
pub struct FlagSet<'a> { /* private fields */ }
Expand description

A set of flags. Allows fine control over parse procedure.

Typically you can use the parse function.

§Example

let mut force = false;
let mut lines = 10_i32;
let args: Vec<String>;
{
    let mut flags = FlagSet::new();
    flags.add_flag("f", &mut force);
    flags.add_flag("lines", &mut lines);
    args = flags.parse(&["-f", "--lines", "20", "--", "foo"])?;
}
assert_eq!(force, true);
assert_eq!(lines, 20);
assert_eq!(args, vec![String::from("foo")]);

Implementations§

Source§

impl<'a> FlagSet<'a>

Source

pub fn new() -> Self

Creates a new set of flags.

§Example
let mut flags = FlagSet::new();
Source

pub fn add_flag(&mut self, name: &'a str, value: &'a mut dyn FlagSetter)

Add a flag to be parsed.

§Panics

Panics if the flag of the same name is already registered.

§Example
let mut force = false;
let mut flags = FlagSet::new();
flags.add_flag("f", &mut force);
Examples found in repository?
examples/someflags.rs (line 7)
3fn main() {
4    let mut force = false;
5    let mut lines = 10_i32;
6    let args: Vec<OsString> = go_flag::parse(|flags| {
7        flags.add_flag("f", &mut force);
8        flags.add_flag("lines", &mut lines);
9    });
10    println!("force = {:?}", force);
11    println!("lines = {:?}", lines);
12    for arg in &args {
13        println!("{}", arg.to_string_lossy());
14    }
15}
Source

pub fn parse<'b, T: FlagValue, S: AsRef<OsStr>>( &mut self, args: &'b [S], ) -> Result<Vec<T>, FlagError>

Parses the given arguments.

§Returns

Returns the list of positional arguments (remaining arguments).

Positional arguments can also be parsed. You’ll typically need Vec<String>, Vec<OsString> or Vec<PathBuf>.

§Errors

It returns Err if the given arguments contains invalid flags.

§Example
let mut force = false;
let mut flags = FlagSet::new();
flags.add_flag("f", &mut force);
let args: Vec<String> = flags.parse(&["-f", "foo"])?;
assert_eq!(args, vec![String::from("foo")]);
Source

pub fn parse_with_warnings<'b, T: FlagValue, S: AsRef<OsStr>>( &mut self, args: &'b [S], warnings: Option<&mut Vec<FlagWarning>>, ) -> Result<Vec<T>, FlagError>

Parses the given arguments, recording warnings issued.

§Returns

Returns the list of positional arguments (remaining arguments).

Positional arguments can also be parsed. You’ll typically need Vec<String>, Vec<OsString> or Vec<PathBuf>.

§Errors

It returns Err if the given arguments contains invalid flags.

§Example
let mut warnings = Vec::new();
let mut force = false;
let mut flags = FlagSet::new();
flags.add_flag("f", &mut force);
let args: Vec<String> = flags
    .parse_with_warnings(&["--f", "foo", "-non-flag"], Some(&mut warnings))?;
assert_eq!(args, vec![String::from("foo"), String::from("-non-flag")]);
assert_eq!(warnings[0].to_string(), "short flag with double minuses: --f");
assert_eq!(warnings[1].to_string(), "flag-like syntax appearing after argument: -non-flag");

Trait Implementations§

Source§

impl<'a> Debug for FlagSet<'a>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for FlagSet<'a>

§

impl<'a> !RefUnwindSafe for FlagSet<'a>

§

impl<'a> !Send for FlagSet<'a>

§

impl<'a> !Sync for FlagSet<'a>

§

impl<'a> Unpin for FlagSet<'a>

§

impl<'a> !UnwindSafe for FlagSet<'a>

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.