From 6fee83858272432f3bf59b66194e6b7773c46bbf Mon Sep 17 00:00:00 2001 From: HouXiaoxuan Date: Sat, 16 Dec 2023 22:17:28 +0800 Subject: [PATCH] cli command --- Cargo.toml | 3 ++- src/cli.rs | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 3 ++- 3 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 src/cli.rs diff --git a/Cargo.toml b/Cargo.toml index aca5246..4df52ae 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,4 +7,5 @@ edition = "2021" [dependencies] sha1 = "0.10.6" -hex = "0.4.3" \ No newline at end of file +hex = "0.4.3" +clap = { version = "4.4.11", features = ["derive"] } diff --git a/src/cli.rs b/src/cli.rs new file mode 100644 index 0000000..91f6a2f --- /dev/null +++ b/src/cli.rs @@ -0,0 +1,57 @@ +use clap::{Parser, Subcommand}; + +/// Rust实现的简易版本的Git,用于学习Rust语言 +#[derive(Parser)] +#[command(author, version, about, long_about = None)] +struct Cli { + /// The subcommand to run. + #[clap(subcommand)] + command: Command, +} + +#[derive(Subcommand)] +enum Command { + /// 初始化仓库 + Init, + /// 添加文件到暂存区 + Add { + /// 要添加的文件 + files: Vec, + }, + /// 删除文件 + Rm { + /// 要删除的文件 + files: Vec, + /// flag 删除暂存区的文件 + #[clap(long, action)] + cached: bool, + }, + /// 提交暂存区的文件 + Commit { + #[clap(short, long)] + message: String, + + #[clap(long, action)] + allow_empty: bool, + }, +} +pub fn handle_command() { + let cli = Cli::parse(); + match cli.command { + Command::Init => { + println!("init"); + } + Command::Add { files } => { + println!("add: {:?}", files); + } + Command::Rm { files, cached } => { + println!("rm: {:?}, cached= {}", files, cached); + } + Command::Commit { + message, + allow_empty, + } => { + println!("commit: {:?}, allow empty={:?}", message, allow_empty); + } + } +} diff --git a/src/main.rs b/src/main.rs index 89e2949..3a6a223 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ use std::collections::HashMap; type Index = HashMap; +mod cli; fn main() { - println!("Hello, world!"); + cli::handle_command(); }