add parameter parser: not done or working yet, but it's definitely there
This commit is contained in:
parent
816697b6fa
commit
d8b8c7a48f
1 changed files with 79 additions and 2 deletions
81
src/lib.rs
81
src/lib.rs
|
@ -1,21 +1,98 @@
|
|||
use std::str::Chars;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::*;
|
||||
|
||||
#[test]
|
||||
fn tests() {
|
||||
dbg!(parse_args("Hello world".to_string(), 0x00));
|
||||
dbg!(parse_args("Hello world", 0x00));
|
||||
dbg!(parse_args("\"Hello world\'\"", 0x07));
|
||||
}
|
||||
}
|
||||
|
||||
/// Parses the command line arguments into a list of parameters.
|
||||
pub fn parse_args(args: String, mode: u8) -> Vec<String> {
|
||||
pub fn parse_args(input: &str, mode: u8) -> Vec<&str> {
|
||||
let args = &mut input.clone();
|
||||
match mode {
|
||||
/// Returns exact input string.
|
||||
0x00 => {vec![args]},
|
||||
/// Seperates all strings
|
||||
0x07 => {
|
||||
let mut arguments: Vec<String> = vec![];
|
||||
let mut singlequotes: Vec<usize>;
|
||||
let mut doublequotes: Vec<usize>;
|
||||
let mut variables: Vec<usize>;
|
||||
let mut escaped_spaces: Vec<usize>;
|
||||
let mut extraneous_backslashes: Vec<usize>;
|
||||
let mut marked_for_removal: Vec<usize>;
|
||||
|
||||
|
||||
// Begin by marking characters that have special behavior.
|
||||
for (i, c) in args.chars().enumerate() {
|
||||
match c {
|
||||
'\'' => {
|
||||
if args[i - 1] == '\\' && args[i - 2] != '\\' {
|
||||
singlequotes.push(i);
|
||||
}
|
||||
},
|
||||
'\"' => {
|
||||
if args[i - 1] == '\\' && args[i - 2] != '\\' {
|
||||
doublequotes.push(i);
|
||||
}
|
||||
}
|
||||
'$' => {
|
||||
if args[i - 1] == '\\' && args[i - 2] != '\\' {
|
||||
variables.push(i);
|
||||
}
|
||||
}
|
||||
' ' => {
|
||||
if args[i - 1] == '\\' && args[i - 2] != '\\' {
|
||||
escaped_spaces.push(i);
|
||||
}
|
||||
}
|
||||
'\\' => {
|
||||
if args[i - i] != '\\' && args[i + 1] == ' ' | '\n' {
|
||||
extraneous_backslashes.push(i);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
};
|
||||
let mut singlequoted = false;
|
||||
for i in singlequotes {
|
||||
if !singlequoted {
|
||||
singlequoted = true;
|
||||
marked_for_removal.push(i);
|
||||
} else {
|
||||
singlequoted = false;
|
||||
}
|
||||
}
|
||||
let mut doublequoted = false;
|
||||
for i in doublequotes {
|
||||
if !doublequoted {
|
||||
doublequoted = true;
|
||||
marked_for_removal.push(i);
|
||||
} else {
|
||||
doublequoted = false;
|
||||
}
|
||||
}
|
||||
for i in escaped_spaces {
|
||||
marked_for_removal.push(i - 1);
|
||||
}
|
||||
for i in extraneous_backslashes {
|
||||
marked_for_removal.push(i);
|
||||
}
|
||||
for i in marked_for_removal {
|
||||
args.insert(i, char::from(0xFF));
|
||||
}
|
||||
for (i, c) in args.chars().enumerate() {
|
||||
if c == char::from(0xFF) {
|
||||
arguments.push(args.split_at(i).0.to_string());
|
||||
arguments.push(args.split_at(i).1.to_string());
|
||||
}
|
||||
}
|
||||
return arguments;
|
||||
}
|
||||
},
|
||||
///
|
||||
0x10 => {
|
||||
|
|
Loading…
Reference in a new issue