parser : gestion des whitespaces
This commit is contained in:
parent
62b2dd8065
commit
04fd13b801
1 changed files with 24 additions and 1 deletions
|
@ -1,8 +1,31 @@
|
|||
use crate::command::command_builder::CommandBuilder;
|
||||
use crate::command::command_sequence::CommandSequence;
|
||||
|
||||
fn build_argv(characters: &mut Vec<char>, current_arg: &mut String) -> Vec<String> {
|
||||
if characters.is_empty() {
|
||||
vec![]
|
||||
} else {
|
||||
let current_char = characters.pop().unwrap();
|
||||
|
||||
if current_char.is_whitespace() {
|
||||
if !current_arg.is_empty() {
|
||||
let mut argv: Vec<String> = vec![current_arg.clone()];
|
||||
argv.append(&mut build_argv(characters, &mut String::from("")));
|
||||
|
||||
argv
|
||||
} else {
|
||||
build_argv(characters, current_arg)
|
||||
}
|
||||
} else {
|
||||
current_arg.push(current_char);
|
||||
build_argv(characters, current_arg)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse_command_line(line: String) -> Option<CommandSequence> {
|
||||
let argv: Vec<String> = line.split_whitespace().map(|s| s.to_string()).collect();
|
||||
let mut characters: Vec<char> = line.chars().rev().collect();
|
||||
let argv = build_argv(&mut characters, &mut String::from(""));
|
||||
|
||||
if !argv.is_empty() {
|
||||
let command = CommandBuilder::new(argv).build();
|
||||
|
|
Loading…
Reference in a new issue