summaryrefslogtreecommitdiff
path: root/mingling/src
diff options
context:
space:
mode:
Diffstat (limited to 'mingling/src')
-rw-r--r--mingling/src/parser/args.rs69
-rw-r--r--mingling/src/parser/picker/builtin.rs42
2 files changed, 100 insertions, 11 deletions
diff --git a/mingling/src/parser/args.rs b/mingling/src/parser/args.rs
index 0210b56..084051d 100644
--- a/mingling/src/parser/args.rs
+++ b/mingling/src/parser/args.rs
@@ -1,6 +1,6 @@
use std::mem::replace;
-use mingling_core::{Flag, special_argument, special_flag};
+use mingling_core::{Flag, special_argument, special_arguments, special_flag};
/// User input arguments
#[derive(Debug, Default)]
@@ -67,32 +67,79 @@ impl std::ops::DerefMut for Argument {
}
impl Argument {
- /// Extracts argument (with value) from arguments
+ /// Picks a single argument with the given flag
pub fn pick_argument<F>(&mut self, flag: F) -> Option<String>
where
F: Into<Flag>,
{
+ if self.len() < 1 {
+ return None;
+ }
+
let flag: Flag = flag.into();
- for argument in flag.iter() {
- let value = special_argument!(self.vec, argument);
- if value.is_some() {
- return value;
+ if flag.len() > 0 {
+ // Has any flag
+ for argument in flag.iter() {
+ let value = special_argument!(self.vec, argument);
+ if value.is_some() {
+ return value;
+ }
}
+ } else {
+ // No flag
+ return Some(self.vec.remove(0));
}
None
}
- /// Extracts flags from arguments
- pub fn pick_flag<F>(&mut self, flag: F) -> bool
+ /// Picks arguments with the given flag
+ pub fn pick_arguments<F>(&mut self, flag: F) -> Vec<String>
where
F: Into<Flag>,
{
+ let mut str_result = Vec::new();
+
+ if self.len() < 1 {
+ return str_result;
+ }
+
let flag: Flag = flag.into();
for argument in flag.iter() {
- let enabled = special_flag!(self.vec, argument);
- if enabled {
- return enabled;
+ let value = special_arguments!(self.vec, argument);
+ str_result.extend(value);
+ }
+
+ str_result
+ }
+
+ /// Picks a flag with the given flag
+ pub fn pick_flag<F>(&mut self, flag: F) -> bool
+ where
+ F: Into<Flag>,
+ {
+ if self.len() < 1 {
+ return false;
+ }
+
+ let flag: Flag = flag.into();
+ if flag.len() > 0 {
+ // Has any flag
+ for argument in flag.iter() {
+ let enabled = special_flag!(self.vec, argument);
+ if enabled {
+ return enabled;
+ }
}
+ } else {
+ let first = self.vec.remove(0);
+ let first_lower = first.to_lowercase();
+ let trimmed = first_lower.trim();
+ let result = match trimmed {
+ "y" | "yes" | "true" | "1" => return true,
+ "n" | "no" | "false" | "0" => return false,
+ _ => false,
+ };
+ return result;
}
false
}
diff --git a/mingling/src/parser/picker/builtin.rs b/mingling/src/parser/picker/builtin.rs
index 9184813..8a10dfc 100644
--- a/mingling/src/parser/picker/builtin.rs
+++ b/mingling/src/parser/picker/builtin.rs
@@ -10,6 +10,14 @@ impl Pickable for String {
}
}
+impl Pickable for Vec<String> {
+ type Output = Vec<String>;
+
+ fn pick(args: &mut crate::parser::Argument, flag: mingling_core::Flag) -> Option<Self::Output> {
+ Some(args.pick_arguments(flag))
+ }
+}
+
macro_rules! impl_pickable_for_number {
($($t:ty),*) => {
$(
@@ -23,6 +31,23 @@ macro_rules! impl_pickable_for_number {
picked.parse().ok()
}
}
+
+ impl Pickable for Vec<$t> {
+ type Output = Vec<$t>;
+
+ fn pick(args: &mut crate::parser::Argument, flag: mingling_core::Flag) -> Option<Self::Output> {
+ let picked_vec = args.pick_arguments(flag);
+ let mut result = Vec::new();
+ for picked in picked_vec {
+ if let Ok(parsed) = picked.parse() {
+ result.push(parsed);
+ } else {
+ return None;
+ }
+ }
+ Some(result)
+ }
+ }
)*
};
}
@@ -52,6 +77,23 @@ impl Pickable for usize {
}
}
+impl Pickable for Vec<usize> {
+ type Output = Vec<usize>;
+
+ fn pick(args: &mut crate::parser::Argument, flag: mingling_core::Flag) -> Option<Self::Output> {
+ let picked_vec = args.pick_arguments(flag);
+ let mut result = Vec::new();
+ for picked in picked_vec {
+ let size_parse = Size::from_str(picked.as_str());
+ match size_parse {
+ Ok(size) => result.push(size.bytes() as usize),
+ Err(_) => return None,
+ }
+ }
+ Some(result)
+ }
+}
+
impl Pickable for Argument {
type Output = Argument;