[][src]Struct go_flag::FlagSet

pub struct FlagSet<'a> { /* fields omitted */ }

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")]);

Methods

impl<'a> FlagSet<'a>[src]

pub fn new() -> Self[src]

Creates a new set of flags.

Example

let mut flags = FlagSet::new();

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

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);

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

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")]);

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>
[src]

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

impl<'a> Debug for FlagSet<'a>[src]

Auto Trait Implementations

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

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

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

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

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

Blanket Implementations

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> From<T> for T[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]