diff options
| author | acdenisSK <[email protected]> | 2018-08-05 22:00:57 +0200 |
|---|---|---|
| committer | acdenisSK <[email protected]> | 2018-08-05 22:01:19 +0200 |
| commit | 73ab20f271c9cc6dadb7bb76938ae64d19cee71e (patch) | |
| tree | 1ff6da81ccfdd5dc895694af09c2e7bcce7be043 /src/framework | |
| parent | Fix Game From impls on no-model compilation (diff) | |
| download | serenity-73ab20f271c9cc6dadb7bb76938ae64d19cee71e.tar.xz serenity-73ab20f271c9cc6dadb7bb76938ae64d19cee71e.zip | |
Add simple no-parse getters and advancer to `Args`
Diffstat (limited to 'src/framework')
| -rw-r--r-- | src/framework/standard/args.rs | 68 |
1 files changed, 67 insertions, 1 deletions
diff --git a/src/framework/standard/args.rs b/src/framework/standard/args.rs index 8a622af..cec1f20 100644 --- a/src/framework/standard/args.rs +++ b/src/framework/standard/args.rs @@ -324,6 +324,29 @@ impl Args { } } + /// Retrieves the current argument. Does not parse. + /// + /// # Note + /// + /// This borrows `Args` for the entire lifetime of the returned argument. + /// + /// # Examples + /// + /// ```rust + /// use serenity::framework::standard::Args; + /// + /// let mut args = Args::new("42 69", &[" ".to_string()]); + /// + /// assert_eq!(args.current(), Some("42")); + /// args.next(); + /// assert_eq!(args.current(), Some("69")); + /// args.next(); + /// assert_eq!(args.current(), None); + /// ``` + pub fn current(&self) -> Option<&str> { + self.args.get(self.offset).map(|t| t.lit.as_str()) + } + /// Parses the current argument and advances. /// /// # Examples @@ -425,7 +448,6 @@ impl Args { Some(vec) } - /// Provides an iterator that will spew arguments until the end of the message. /// /// # Examples @@ -463,6 +485,30 @@ impl Args { self.iter::<T>().collect() } + /// Retrieves the current argument and also removes quotes around it if they're present. + /// Does not parse. + /// + /// # Note + /// + /// This borrows `Args` for the entire lifetime of the returned argument. + /// + /// # Examples + /// + /// ```rust + /// use serenity::framework::standard::Args; + /// + /// let mut args = Args::new("42 \"69\"", &[" ".to_string()]); + /// + /// assert_eq!(args.current_quoted(), Some("42")); + /// args.next(); + /// assert_eq!(args.current_quoted(), Some("69")); + /// args.next(); + /// assert_eq!(args.current_quoted(), None); + /// ``` + pub fn current_quoted(&self) -> Option<&str> { + self.args.get(self.offset).map(|t| quotes_extract(t)) + } + /// Like [`single`], but accounts quotes. /// /// # Examples @@ -790,6 +836,26 @@ impl Args { self.len() - self.offset } + /// Move to the next argument. + /// This increments the offset pointer. + /// + /// # Examples + /// + /// ```rust + /// use serenity::framework::standard::Args; + /// + /// let mut args = Args::new("42 69", &[" ".to_string()]); + /// + /// args.next(); + /// + /// assert_eq!(args.single::<u32>().unwrap(), 69); + /// assert!(args.is_empty()); + /// ``` + #[inline] + pub fn next(&mut self) { + self.offset += 1; + } + /// Go one step behind. /// /// # Examples |