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>
impl<'a, 'b> Flags<'a, 'b>
pub fn new(inner: &'a InnerFlags<'b>) -> Self
sourcepub fn long(&self, flag: impl AsRef<str>) -> bool
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");sourcepub fn is_empty(&self) -> bool
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§
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> 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
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit)source§impl<T> CloneToUninit for Twhere
T: Copy,
impl<T> CloneToUninit for Twhere
T: Copy,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit)