forked from AbleOS/ableos
73 lines
1.9 KiB
Rust
73 lines
1.9 KiB
Rust
/*
|
|
clparse
|
|
* A simple command line parser for ableOS
|
|
*/
|
|
|
|
use alloc::collections::HashMap;
|
|
use std::collections::HashMap;
|
|
|
|
const CURRENT_PATH: &str = "file://test/";
|
|
|
|
#[derive(Debug)]
|
|
struct Command {
|
|
root_command: String,
|
|
arguments: HashMap<String, String>,
|
|
}
|
|
|
|
impl Command {
|
|
pub fn parse(command: String) -> Command {
|
|
let split_command = command.split("?");
|
|
let mut root = "".to_string();
|
|
let mut rootCount = 0;
|
|
let mut args = HashMap::<String, String>::new();
|
|
for subcommand in split_command {
|
|
match rootCount {
|
|
0 => root = subcommand.to_string(),
|
|
1 => {
|
|
for subarg in subcommand.split("&") {
|
|
let mut arg1 = "";
|
|
let mut arg2 = "";
|
|
let mut arg_count = 0;
|
|
for arg in subarg.split("=") {
|
|
if arg_count == 0 {
|
|
arg1 = arg;
|
|
} else {
|
|
arg2 = arg;
|
|
}
|
|
arg_count += 1;
|
|
}
|
|
args.insert(arg1.to_string(), arg2.to_string());
|
|
}
|
|
}
|
|
_ => {}
|
|
}
|
|
rootCount += 1;
|
|
}
|
|
|
|
Command {
|
|
root_command: root,
|
|
arguments: args,
|
|
}
|
|
}
|
|
}
|
|
fn main() {
|
|
let x = Command::parse("ls?path=".to_string());
|
|
|
|
let y = &x.arguments["path"];
|
|
parsePath(y.to_string());
|
|
}
|
|
|
|
fn parsePath(path: String) {
|
|
if path.len() == 0 {
|
|
println!("EMPTY PATH");
|
|
} else {
|
|
match &path[..1] {
|
|
"@" => println!("PARTIAL PATH: {}{}", CURRENT_PATH, path),
|
|
"/" => println!("FULL PATH: {}", path),
|
|
_ => {
|
|
println!("MALFORMED PATH: {}", path)
|
|
}
|
|
}
|
|
}
|
|
}
|