Get clap working for command parsing
This commit is contained in:
parent
ee9e33b381
commit
a7c5a0a9ea
7
Cargo.lock
generated
7
Cargo.lock
generated
@ -264,6 +264,7 @@ version = "0.1.0"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"clap",
|
"clap",
|
||||||
"dir_rpc",
|
"dir_rpc",
|
||||||
|
"shlex",
|
||||||
"vfs_rpc",
|
"vfs_rpc",
|
||||||
]
|
]
|
||||||
|
|
||||||
@ -326,6 +327,12 @@ dependencies = [
|
|||||||
"syn",
|
"syn",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "shlex"
|
||||||
|
version = "1.3.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "smallvec"
|
name = "smallvec"
|
||||||
version = "1.13.2"
|
version = "1.13.2"
|
||||||
|
@ -6,4 +6,5 @@ edition = "2021"
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
clap = { version = "4.5.18", features = ["derive"] }
|
clap = { version = "4.5.18", features = ["derive"] }
|
||||||
dir_rpc = { version = "0.1.0", path = "../dir_rpc" }
|
dir_rpc = { version = "0.1.0", path = "../dir_rpc" }
|
||||||
|
shlex = "1.3.0"
|
||||||
vfs_rpc = { version = "0.1.0", path = "../vfs/vfs_rpc" }
|
vfs_rpc = { version = "0.1.0", path = "../vfs/vfs_rpc" }
|
||||||
|
87
src/main.rs
87
src/main.rs
@ -1,30 +1,19 @@
|
|||||||
use std::{
|
use std::{
|
||||||
io::{self, Write},
|
io::{self, Write},
|
||||||
os::mikros::syscalls,
|
os::mikros::syscalls,
|
||||||
path::Path,
|
path::{Path, PathBuf},
|
||||||
};
|
};
|
||||||
|
|
||||||
use clap::{Parser, Subcommand};
|
use clap::Parser;
|
||||||
|
|
||||||
#[derive(Parser, Debug)]
|
#[derive(Parser, Debug)]
|
||||||
struct Cli {
|
struct Ls {
|
||||||
#[command(subcommand)]
|
path: Option<PathBuf>,
|
||||||
command: Commands
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Subcommand, Debug)]
|
#[derive(Parser, Debug)]
|
||||||
enum Commands {
|
struct Cat {
|
||||||
Pwd,
|
path: PathBuf,
|
||||||
Ls {
|
|
||||||
path: Option<String>
|
|
||||||
},
|
|
||||||
Cat {
|
|
||||||
path: String,
|
|
||||||
},
|
|
||||||
Exit {
|
|
||||||
path: String,
|
|
||||||
},
|
|
||||||
Help,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
@ -56,23 +45,30 @@ fn main() {
|
|||||||
if line.is_empty() {
|
if line.is_empty() {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
let cmd = line.split(' ').collect::<Vec<_>>();
|
let Some(cmd) = shlex::split(line) else {
|
||||||
dbg!(Cli::try_parse_from(&cmd));
|
println!("Syntax error");
|
||||||
match cmd[0] {
|
continue;
|
||||||
|
};
|
||||||
|
match cmd[0].as_str() {
|
||||||
"pwd" => println!("{}", cwd.to_string_lossy()),
|
"pwd" => println!("{}", cwd.to_string_lossy()),
|
||||||
"ls" => {
|
"ls" => {
|
||||||
let mut path = if cmd.len() > 1 {
|
let cmd = match Ls::try_parse_from(&cmd) {
|
||||||
Path::new(cmd[1]).to_owned()
|
Ok(cmd) => cmd,
|
||||||
} else {
|
Err(err) => {
|
||||||
cwd.clone()
|
err.print().unwrap();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
if path.is_relative() {
|
let cmd_path = cmd.path.clone().unwrap_or(PathBuf::new());
|
||||||
|
let abs_path = if cmd_path.is_relative() {
|
||||||
let mut abs_path = cwd.clone();
|
let mut abs_path = cwd.clone();
|
||||||
abs_path.push(path);
|
abs_path.push(cmd_path.clone());
|
||||||
path = abs_path;
|
abs_path
|
||||||
}
|
} else {
|
||||||
let Ok((fs_pid, dir_fd)) = vfs_client.open_dir(&path) else {
|
cmd_path.clone()
|
||||||
println!("psh: {}: failed to open", path.to_string_lossy());
|
};
|
||||||
|
let Ok((fs_pid, dir_fd)) = vfs_client.open_dir(&abs_path) else {
|
||||||
|
println!("ls: {}: failed to open", cmd_path.display());
|
||||||
continue;
|
continue;
|
||||||
};
|
};
|
||||||
let fs_client = dir_rpc::Client::new(fs_pid);
|
let fs_client = dir_rpc::Client::new(fs_pid);
|
||||||
@ -81,18 +77,22 @@ fn main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
"cat" => {
|
"cat" => {
|
||||||
if cmd.len() < 2 {
|
let cmd = match Cat::try_parse_from(&cmd) {
|
||||||
println!("Usage: cat <file>");
|
Ok(cmd) => cmd,
|
||||||
continue;
|
Err(err) => {
|
||||||
}
|
err.print().unwrap();
|
||||||
let mut path = Path::new(cmd[1]).to_owned();
|
continue;
|
||||||
if path.is_relative() {
|
}
|
||||||
|
};
|
||||||
|
let abs_path = if cmd.path.is_relative() {
|
||||||
let mut abs_path = cwd.clone();
|
let mut abs_path = cwd.clone();
|
||||||
abs_path.push(path);
|
abs_path.push(cmd.path.clone());
|
||||||
path = abs_path;
|
abs_path
|
||||||
}
|
} else {
|
||||||
let Ok(contents) = std::fs::read(&path) else {
|
cmd.path.clone()
|
||||||
println!("psh: {}: failed to read", path.to_string_lossy());
|
};
|
||||||
|
let Ok(contents) = std::fs::read(&abs_path) else {
|
||||||
|
println!("cat: {}: failed to read", cmd.path.display());
|
||||||
continue;
|
continue;
|
||||||
};
|
};
|
||||||
io::stdout().lock().write_all(&contents).unwrap();
|
io::stdout().lock().write_all(&contents).unwrap();
|
||||||
@ -109,12 +109,11 @@ fn main() {
|
|||||||
println!("Commands:");
|
println!("Commands:");
|
||||||
println!("pwd");
|
println!("pwd");
|
||||||
println!("ls");
|
println!("ls");
|
||||||
|
|
||||||
println!("cat");
|
println!("cat");
|
||||||
println!("exit");
|
println!("exit");
|
||||||
println!("help");
|
println!("help");
|
||||||
}
|
}
|
||||||
_ => println!("psh: command not found: {}", cmd[0]),
|
_ => println!("psh: {}: command not found", cmd[0]),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user