use clap::{Parser, Subcommand};
use crate::store::KvStore;
const STORE_PATH: &str = "store.json";
#[derive(Parser)]
#[command(name = "minkv")]
#[command(about = "一个轻量级持久化键值存储", long_about = None)]
pub struct Cli {
#[command(subcommand)]
pub command: Commands,
}
#[derive(Subcommand)]
pub enum Commands {
Get {
key: String,
},
Set {
key: String,
value: String,
},
Remove {
key: String,
},
}
pub fn run() {
let cli = Cli::parse();
let mut store = KvStore::open(STORE_PATH);
match cli.command {
Commands::Get { key } => {
match store.get(&key) {
Some(value) => println!("{}", value),
None => {
eprintln!("键不存在: {}", key);
std::process::exit(1);
}
}
}
Commands::Set { key, value } => {
store.set(key, value);
println!("OK");
}
Commands::Remove { key } => {
if store.remove(&key).is_none() {
eprintln!("键不存在: {}", key);
std::process::exit(1);
}
println!("OK");
}
}
}