Struct duat_core::commands::Flags

source ·
pub struct Flags<'a, 'b>(/* private fields */);
Expand description

A struct representing flags passed down to [Command]s when running them.

There are 2 types of flag, the short and long flags.

short flags represent singular characters passed after a single '-' character, they can show up in multiple places, and should represent an incremental addition of features to a command.

long flags are words that come after any "--" sequence, and should represent more verbose, but more readable versions of short flags.

§Examples

Both short and long flags can only be counted once, no matter how many times they show up:

let call = "my-command --foo -abcde --foo --bar -abfgh arg1";
let (flags, mut args) = split_flags_and_args(call);

assert!(flags.short("abcdefgh"));
assert!(flags.long("foo") && flags.long("bar"));
assert_eq!(args.collect::<Vec<&str>>(), vec!["arg1"]);

If you have any arguments that start with '-' or "--", but are not supposed to be flags, you can insert an empty "--" after the flags, in order to distinguish them.

let call = "command --foo --bar -abcde -- --!flag -also-not";
let (flags, mut args) = split_flags_and_args(call);

assert!(flags.short("abcde"));
assert!(flags.long("foo") && flags.long("bar"));
assert_eq!(args.collect::<String>(), "--!flag -also-not")

Implementations§

source§

impl<'a, 'b> Flags<'a, 'b>

source

pub fn new(inner: &'a InnerFlags<'b>) -> Self

source

pub fn short(&self, short: impl AsRef<str>) -> bool

Checks if all of the chars in the short passed.

§Examples
let call = "run -abcdefgh -ablk args -wz";
let (flags, mut args) = split_flags_and_args(call);

assert!(flags.short("k"));
assert!(!flags.short("w"));
assert_eq!(args.collect::<Vec<&str>>(), vec!["args", "-wz"]);
source

pub fn long(&self, flag: impl AsRef<str>) -> bool

Returns true if the long flag was passed.

§Examples
let call = "run --foo --bar args --baz";
let (flags, mut args) = split_flags_and_args(call);

assert!(flags.long("foo"));
assert!(!flags.long("baz"));
assert_eq!(&args.collect::<String>(), "args --baz");
source

pub fn is_empty(&self) -> bool

Returns true if no flags have been passed.

§Examples
let call = "run arg1 --foo --bar arg2 -baz";
let (flags, mut args) = split_flags_and_args(call);

assert!(flags.is_empty());
assert_eq!(args.collect::<Vec<&str>>(), vec![
    "arg1", "--foo", "--bar", "arg2", "-baz"
]);

Trait Implementations§

source§

impl<'a, 'b> Clone for Flags<'a, 'b>

source§

fn clone(&self) -> Flags<'a, 'b>

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
source§

impl<'a, 'b> Copy for Flags<'a, 'b>

Auto Trait Implementations§

§

impl<'a, 'b> Freeze for Flags<'a, 'b>

§

impl<'a, 'b> RefUnwindSafe for Flags<'a, 'b>

§

impl<'a, 'b> Send for Flags<'a, 'b>

§

impl<'a, 'b> Sync for Flags<'a, 'b>

§

impl<'a, 'b> Unpin for Flags<'a, 'b>

§

impl<'a, 'b> UnwindSafe for Flags<'a, 'b>

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§

default unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<T> CloneToUninit for T
where T: Copy,

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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,

§

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

§

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

§

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.