更新项目结构,添加新依赖,优化交互模式和命令行参数处理
All checks were successful
Rust Cross-Compile and Release / build_and_release (push) Successful in 8m40s

This commit is contained in:
Yakumo Hokori
2026-01-15 16:10:18 +08:00
parent ae95cfda79
commit 34f8384768
4 changed files with 534 additions and 111 deletions

186
Cargo.lock generated
View File

@@ -11,6 +11,62 @@ dependencies = [
"memchr",
]
[[package]]
name = "anstream"
version = "0.6.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "43d5b281e737544384e969a5ccad3f1cdd24b48086a0fc1b2a5262a26b8f4f4a"
dependencies = [
"anstyle",
"anstyle-parse",
"anstyle-query",
"anstyle-wincon",
"colorchoice",
"is_terminal_polyfill",
"utf8parse",
]
[[package]]
name = "anstyle"
version = "1.0.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5192cca8006f1fd4f7237516f40fa183bb07f8fbdfedaa0036de5ea9b0b45e78"
[[package]]
name = "anstyle-parse"
version = "0.2.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4e7644824f0aa2c7b9384579234ef10eb7efb6a0deb83f9630a49594dd9c15c2"
dependencies = [
"utf8parse",
]
[[package]]
name = "anstyle-query"
version = "1.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc"
dependencies = [
"windows-sys 0.61.2",
]
[[package]]
name = "anstyle-wincon"
version = "3.0.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d"
dependencies = [
"anstyle",
"once_cell_polyfill",
"windows-sys 0.61.2",
]
[[package]]
name = "anyhow"
version = "1.0.100"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a23eb6b1614318a8071c9b2521f36b424b2c83db5eb3a0fead4a6c0809af6e61"
[[package]]
name = "autocfg"
version = "1.4.0"
@@ -39,6 +95,8 @@ checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b"
name = "ccmk"
version = "1.0.1"
dependencies = [
"anyhow",
"clap",
"inquire",
"regex",
]
@@ -49,6 +107,52 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "clap"
version = "4.5.54"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c6e6ff9dcd79cff5cd969a17a545d79e84ab086e444102a591e288a8aa3ce394"
dependencies = [
"clap_builder",
"clap_derive",
]
[[package]]
name = "clap_builder"
version = "4.5.54"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fa42cf4d2b7a41bc8f663a7cab4031ebafa1bf3875705bfaf8466dc60ab52c00"
dependencies = [
"anstream",
"anstyle",
"clap_lex",
"strsim",
]
[[package]]
name = "clap_derive"
version = "4.5.49"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2a0b5487afeab2deb2ff4e03a807ad1a03ac532ff5a2cee5d86884440c7f7671"
dependencies = [
"heck",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "clap_lex"
version = "0.7.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c3e64b0cc0439b12df2fa678eae89a1c56a529fd067a9115f7827f1fffd22b32"
[[package]]
name = "colorchoice"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b05b61dc5112cbb17e4b6cd61790d9845d13888356391624cbe7e41efeac1e75"
[[package]]
name = "crossterm"
version = "0.25.0"
@@ -98,6 +202,12 @@ dependencies = [
"byteorder",
]
[[package]]
name = "heck"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
[[package]]
name = "inquire"
version = "0.7.5"
@@ -115,6 +225,12 @@ dependencies = [
"unicode-width",
]
[[package]]
name = "is_terminal_polyfill"
version = "1.70.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695"
[[package]]
name = "libc"
version = "0.2.172"
@@ -152,7 +268,7 @@ dependencies = [
"libc",
"log",
"wasi",
"windows-sys",
"windows-sys 0.48.0",
]
[[package]]
@@ -170,6 +286,12 @@ version = "1.21.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d"
[[package]]
name = "once_cell_polyfill"
version = "1.70.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe"
[[package]]
name = "parking_lot"
version = "0.12.3"
@@ -193,6 +315,24 @@ dependencies = [
"windows-targets 0.52.6",
]
[[package]]
name = "proc-macro2"
version = "1.0.105"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "535d180e0ecab6268a3e718bb9fd44db66bbbc256257165fc699dadf70d16fe7"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.43"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc74d9a594b72ae6656596548f56f667211f8a97b3d4c3d467150794690dc40a"
dependencies = [
"proc-macro2",
]
[[package]]
name = "redox_syscall"
version = "0.5.12"
@@ -273,6 +413,23 @@ version = "1.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8917285742e9f3e1683f0a9c4e6b57960b7314d0b08d30d1ecd426713ee2eee9"
[[package]]
name = "strsim"
version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
[[package]]
name = "syn"
version = "2.0.114"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d4d107df263a3013ef9b1879b0df87d706ff80f65a86ea879bd9c31f9b307c2a"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "thread_local"
version = "1.1.8"
@@ -283,6 +440,12 @@ dependencies = [
"once_cell",
]
[[package]]
name = "unicode-ident"
version = "1.0.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9312f7c4f6ff9069b165498234ce8be658059c6728633667c526e27dc2cf1df5"
[[package]]
name = "unicode-segmentation"
version = "1.12.0"
@@ -295,6 +458,12 @@ version = "0.1.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7dd6e30e90baa6f72411720665d41d89b9a3d039dc45b8faea1ddd07f617f6af"
[[package]]
name = "utf8parse"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
[[package]]
name = "wasi"
version = "0.11.0+wasi-snapshot-preview1"
@@ -323,6 +492,12 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]]
name = "windows-link"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5"
[[package]]
name = "windows-sys"
version = "0.48.0"
@@ -332,6 +507,15 @@ dependencies = [
"windows-targets 0.48.5",
]
[[package]]
name = "windows-sys"
version = "0.61.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc"
dependencies = [
"windows-link",
]
[[package]]
name = "windows-targets"
version = "0.48.5"

View File

@@ -2,7 +2,11 @@
name = "ccmk"
version = "1.0.1"
edition = "2024"
description = "A CLI tool to quickly generate CMake project structures"
authors = ["hokori"]
[dependencies]
anyhow = "1.0"
clap = { version = "4.4", features = ["derive"] }
inquire = "0.7"
regex = "1.10.2"
regex = "1.10"

View File

@@ -1,71 +1,116 @@
# CMake项目生成工具 (ccmk)
# ccmk - CMake项目生成工具
一个简单的命令行工具用于快速生成CMake项目结构。
一个简单的命令行工具,用于快速生成 CMake 项目结构。
## 功能
- 交互式创建CMake项目
- 支持C和C++项目
- 支持可执行文件和静态库项目
- **交互式模式** - 无参数运行时,按提示选择配置
- **命令行模式** - 直接通过参数指定配置
- 支持 C 和 C++ 项目
- 支持可执行文件、静态库、共享库
- 自动创建项目目录结构
- 自动生成CMakeLists.txt和示例源代码
- 自动生成 CMakeLists.txt 和示例源代码
- 自动处理项目名中的特殊字符
## 安装
1. 确保已安装Rust工具链
2. 克隆本项目
3. 运行以下命令安装:
```bash
cargo install --path .
```
## 使用方法
1. 运行命令:
### 交互式模式
直接运行,按提示操作:
```bash
ccmk
```
2. 按照提示输入:
- 项目名称(会自动处理特殊字符)
- 编程语言C或C++
- 项目类型(可执行文件或静态库)
- C++标准版本如果选择C++
### 命令行模式
3. 工具会自动创建项目目录和文件
```bash
ccmk -n 项目名 -l cpp -p exe -c 17 -o ./output
```
### 参数说明
| 参数 | 简写 | 说明 | 默认值 |
|------|------|------|--------|
| `--name` | `-n` | 项目名称 | 交互式必填 |
| `--lang` | `-l` | 语言: `c``cpp` | `cpp` |
| `--project-type` | `-p` | 类型: `exe`, `static-lib`, `shared-lib` | `exe` |
| `--cxx-standard` | `-c` | C++ 标准: `11`, `14`, `17`, `20`, `23` | `17` |
| `--output` | `-o` | 输出目录 | 当前目录 |
| `--force` | - | 强制覆盖已存在的目录 | - |
### 使用示例
```bash
# 创建 C++ 可执行项目
ccmk -n myapp -l cpp -p exe
# 创建 C 静态库
ccmk -n mylib -l c -p static-lib
# 创建 C++ 共享库,使用 C++20 标准
ccmk -n mylib -l cpp -p shared-lib -c 20
# 指定输出目录
ccmk -n myproject -o /path/to/output
# 覆盖已存在的目录
ccmk -n myproject --force
```
## 生成的项目结构
### 可执行文件
```
项目名/
├── CMakeLists.txt
├── include/
└── src/
└── main.cmain.cpp
└── main.cmain.cpp
```
### 库类型 (static-lib / shared-lib)
```
项目名/
├── CMakeLists.txt
├── include/
├── src/
│ └── main.c 或 main.cpp
└── README.md
```
## 示例
```bash
### 交互式模式
```
$ ccmk
请输入项目名称My Project
请选择编程语言C++
请选择项目类型Static Library
请选择项目类型Shared Library
请选择C++标准版本17
✅ 项目已成功创建!
项目已成功创建!
📁 项目结构:
- MyProject/
├── CMakeLists.txt
├── include/
── src/
└── main.cpp
── src/
└── main.cpp
└── README.md
```
## 注意事项
- 项目名中的特殊字符(如空格、标点符号)会被自动移除
- 如果清理后的项目名为空,会使用默认名称"cmake_project"
- 生成的CMakeLists.txt使用CMake 3.14作为最低版本要求
- 项目名中的特殊字符会被自动移除(仅保留字母、数字、下划线、连字符)
- 如果清理后的项目名为空,会使用默认名称 `cmake_project`
- 库类型项目会自动生成 `GNUInstallDirs` 安装配置
- CMakeLists.txt 使用 CMake 3.20 作为最低版本要求

View File

@@ -1,70 +1,172 @@
use inquire::{Select, Text};
use anyhow::{anyhow, Context, Result};
use clap::{Parser, ValueEnum};
use inquire::{Confirm, Select, Text};
use regex::Regex;
use std::fs;
use std::path::Path;
use regex::Regex;
#[derive(Debug, Clone, ValueEnum)]
enum Lang {
C,
Cpp,
}
#[derive(Debug, Clone, ValueEnum)]
enum ProjectType {
Exe,
StaticLib,
SharedLib,
}
#[derive(Debug, Clone, ValueEnum)]
enum CxxStandard {
#[value(name = "11")]
Cxx11,
#[value(name = "14")]
Cxx14,
#[value(name = "17")]
Cxx17,
#[value(name = "20")]
Cxx20,
#[value(name = "23")]
Cxx23,
}
#[derive(Parser, Debug)]
#[command(name = "ccmk")]
#[command(author, version, about, long_about = None)]
struct Args {
/// Project name
#[arg(short, long)]
name: Option<String>,
/// Language: C or C++
#[arg(short, long, value_enum)]
lang: Option<Lang>,
/// Project type: exe, static-lib, shared-lib
#[arg(short, long, value_enum)]
project_type: Option<ProjectType>,
/// C++ standard (for C++ projects)
#[arg(short, long, value_enum)]
cxx_standard: Option<CxxStandard>,
/// Output directory
#[arg(short, long)]
output: Option<String>,
/// Force overwrite existing directory
#[arg(long)]
force: bool,
}
fn get_lang_ext(lang: &Lang) -> &'static str {
match lang {
Lang::C => "c",
Lang::Cpp => "cpp",
}
}
fn get_lang_cmake(lang: &Lang) -> &'static str {
match lang {
Lang::C => "C",
Lang::Cpp => "CXX",
}
}
fn generate_cmakelists(
project_name: &str,
lang: &str,
project_type: &str,
cxx_standard: &str,
lang: &Lang,
project_type: &ProjectType,
cxx_standard: &Option<CxxStandard>,
) -> String {
let mut lines = vec![
"cmake_minimum_required(VERSION 3.20)".to_string(),
format!("project({} LANGUAGES {})", project_name, lang.to_uppercase()),
format!("project({} LANGUAGES {})", project_name, get_lang_cmake(lang)),
"".to_string(),
];
// 添加C++标准设置
if lang == "cxx" {
lines.push(format!("set(CMAKE_CXX_STANDARD {})", cxx_standard));
if let Lang::Cpp = lang {
let standard = match cxx_standard {
Some(CxxStandard::Cxx11) => "11",
Some(CxxStandard::Cxx14) => "14",
Some(CxxStandard::Cxx17) => "17",
Some(CxxStandard::Cxx20) => "20",
Some(CxxStandard::Cxx23) => "23",
None => "17",
};
lines.push(format!("set(CMAKE_CXX_STANDARD {})", standard));
lines.push("set(CMAKE_CXX_STANDARD_REQUIRED ON)".to_string());
lines.push("set(CMAKE_CXX_EXTENSIONS OFF)".to_string());
lines.push("".to_string());
}
// 处理不同项目类型
let ext = get_lang_ext(lang);
match project_type {
"exe" => {
ProjectType::Exe => {
lines.push(format!(
"add_executable({} src/main.{})",
project_name,
if lang == "c" { "c" } else { "cpp" }, // 使用.cpp扩展名但CMake语言为CXX
project_name, ext,
));
}
"static_lib" => {
ProjectType::StaticLib => {
lines.push(format!(
"add_library({} STATIC src/main.{})",
project_name,
if lang == "c" { "c" } else { "cpp" }, // 使用.cpp扩展名但CMake语言为CXX
project_name, ext,
));
lines.push(format!("target_include_directories({} PRIVATE include)", project_name));
lines.push(format!("target_include_directories({} PUBLIC include)", project_name));
}
ProjectType::SharedLib => {
lines.push(format!(
"add_library({} SHARED src/main.{})",
project_name, ext,
));
lines.push(format!("target_include_directories({} PUBLIC include)", project_name));
lines.push(format!(
"target_compile_definitions({} PUBLIC {}_EXPORTS)",
project_name,
project_name.to_uppercase()
project_name.to_uppercase().replace('-', "_")
));
}
_ => panic!("Unsupported project type"),
}
// 添加导出设置(仅库类型)
if matches!(project_type, ProjectType::StaticLib | ProjectType::SharedLib) {
lines.push("".to_string());
lines.push("# Export targets".to_string());
lines.push("include(GNUInstallDirs)".to_string());
lines.push(format!(
"install(TARGETS {} DESTINATION lib)",
project_name
));
lines.push(
"install(DIRECTORY include/ DESTINATION include)".to_string(),
);
}
lines.join("\n")
}
fn generate_source_file(lang: &str) -> String {
if lang == "c" {
"#include <stdio.h>\n\nint main() {\n printf(\"Hello, World!\\n\");\n return 0;\n}".to_string()
} else {
"#include <iostream>\n\nint main() {\n std::cout << \"Hello, World!\" << std::endl;\n return 0;\n}".to_string()
fn generate_source_file(lang: &Lang) -> String {
match lang {
Lang::C => {
"#include <stdio.h>\n\nint main() {\n printf(\"Hello, World!\\n\");\n return 0;\n}"
.to_string()
}
Lang::Cpp => {
"#include <iostream>\n\nint main() {\n std::cout << \"Hello, World!\" << std::endl;\n return 0;\n}"
.to_string()
}
}
}
// 清理项目名,移除不适合作为文件夹名的字符
fn sanitize_folder_name(name: &str) -> String {
// 使用正则表达式替换所有非字母数字、下划线和连字符的字符
let re = Regex::new(r"[^a-zA-Z0-9_-]").unwrap();
let sanitized = re.replace_all(name, "").to_string();
// 确保不为空,如果为空则使用默认名称
if sanitized.is_empty() {
return "cmake_project".to_string();
}
@@ -72,64 +174,152 @@ fn sanitize_folder_name(name: &str) -> String {
sanitized
}
fn main() -> Result<(), inquire::InquireError> {
// 1. 输入项目名称
let project_name = Text::new("请输入项目名称:").prompt()?;
// 2. 选择语言类型
let lang_choices = vec!["C", "C++"];
let lang = Select::new("请选择编程语言:", lang_choices).prompt()?;
// 3. 选择项目类型
let type_choices = vec!["Executable", "Static Library"];
let project_type = Select::new("请选择项目类型:", type_choices).prompt()?;
// 4. 选择C++标准版本仅当选择C++时)
let cxx_standard = if lang == "C++" {
let standard_choices = vec!["11", "14", "17", "20"];
Select::new("请选择C++标准版本:", standard_choices).prompt()?
} else {
&"00".to_string()
};
// 清理项目名,创建有效的文件夹名
let folder_name = sanitize_folder_name(&project_name);
// 创建项目文件夹和目录结构
let output_path = Path::new(".").join(&folder_name);
fs::create_dir_all(&output_path)?;
fs::create_dir_all(output_path.join("src"))?;
fs::create_dir_all(output_path.join("include"))?;
// 写入CMakeLists.txt
fs::write(
output_path.join("CMakeLists.txt"),
generate_cmakelists(
&project_name,
if lang == "C" { "c" } else { "cxx" },
match project_type {
"Executable" => "exe",
"Static Library" => "static_lib",
_ => "exe",
},
&cxx_standard,
),
)?;
// 写入示例源文件
let source_ext = if lang == "C" { "c" } else { "cpp" }; // 源文件扩展名保持.cppCMake语言使用CXX
fs::write(
output_path.join("src").join(format!("main.{}", source_ext)),
generate_source_file(if lang == "C" { "c" } else { "cpp" }),
)?;
println!("✅ 项目已成功创建!");
fn print_project_structure(folder_name: &str, ext: &str, project_type: &ProjectType) {
println!("\n项目已成功创建!");
println!("📁 项目结构:");
println!(" - {}/", folder_name);
println!(" ├── CMakeLists.txt");
println!(" ├── include/");
println!(" └── src/");
println!(" └── main.{}", source_ext);
match project_type {
ProjectType::Exe => {
println!(" └── src/");
println!(" └── main.{}", ext);
}
ProjectType::StaticLib | ProjectType::SharedLib => {
println!(" ├── src/");
println!(" │ └── main.{}", ext);
println!(" └── README.md");
}
}
}
fn interactive_mode() -> Result<(String, Lang, ProjectType, Option<CxxStandard>)> {
let project_name = Text::new("请输入项目名称:").prompt()?;
let lang_choices = vec!["C", "C++"];
let lang_raw = Select::new("请选择编程语言:", lang_choices).prompt()?;
let lang = if lang_raw == "C" { Lang::C } else { Lang::Cpp };
let type_choices = vec!["Executable", "Static Library", "Shared Library"];
let type_raw = Select::new("请选择项目类型:", type_choices).prompt()?;
let project_type = match type_raw {
"Executable" => ProjectType::Exe,
"Static Library" => ProjectType::StaticLib,
"Shared Library" => ProjectType::SharedLib,
_ => ProjectType::Exe,
};
let cxx_standard = if matches!(lang, Lang::Cpp) {
let standard_choices = vec!["11", "14", "17", "20", "23"];
let standard_raw = Select::new("请选择C++标准版本:", standard_choices).prompt()?;
Some(match standard_raw {
"11" => CxxStandard::Cxx11,
"14" => CxxStandard::Cxx14,
"17" => CxxStandard::Cxx17,
"20" => CxxStandard::Cxx20,
_ => CxxStandard::Cxx23,
})
} else {
None
};
Ok((project_name, lang, project_type, cxx_standard))
}
fn create_project(
project_name: &str,
lang: &Lang,
project_type: &ProjectType,
cxx_standard: &Option<CxxStandard>,
output_dir: &Path,
force: bool,
) -> Result<()> {
// 检查目录是否存在
if output_dir.exists() {
if force {
println!("⚠️ 目录已存在,将被覆盖");
} else {
let overwrite = Confirm::new(&format!(
"目录 '{}' 已存在,是否覆盖?",
output_dir.display()
))
.prompt()?;
if !overwrite {
return Err(anyhow!("已取消"));
}
}
}
// 创建目录结构
fs::create_dir_all(output_dir.join("src"))?;
if matches!(project_type, ProjectType::StaticLib | ProjectType::SharedLib) {
fs::create_dir_all(output_dir.join("include"))?;
}
// 写入 CMakeLists.txt
fs::write(
output_dir.join("CMakeLists.txt"),
generate_cmakelists(project_name, lang, project_type, cxx_standard),
)
.with_context(|| "写入 CMakeLists.txt 失败")?;
// 写入源文件
let ext = get_lang_ext(lang);
fs::write(
output_dir.join("src").join(format!("main.{}", ext)),
generate_source_file(lang),
)
.with_context(|| "写入源文件失败")?;
// 为库类型创建 README.md
if matches!(project_type, ProjectType::StaticLib | ProjectType::SharedLib) {
let readme_content = format!(
"# {}\n\nA CMake library project.\n\n## Build\n\n```bash\nmkdir build && cd build\ncmake ..\nmake\n```\n\n## Install\n\n```bash\ncmake --install .\n```\n",
project_name
);
fs::write(output_dir.join("README.md"), readme_content)?;
}
print_project_structure(
&output_dir.file_name().unwrap().to_string_lossy(),
ext,
project_type,
);
Ok(())
}
fn main() -> Result<()> {
let args = Args::parse();
// 如果没有提供任何参数,使用交互模式
if args.name.is_none()
&& args.lang.is_none()
&& args.project_type.is_none()
&& args.cxx_standard.is_none()
{
let (name, lang, project_type, cxx_standard) = interactive_mode()?;
let folder_name = sanitize_folder_name(&name);
let output_path = Path::new(".").join(&folder_name);
create_project(&name, &lang, &project_type, &cxx_standard, &output_path, args.force)?;
return Ok(());
}
// 命令行模式
let name = args.name.ok_or_else(|| anyhow!("项目名称为必填项"))?;
let lang = args.lang.unwrap_or(Lang::Cpp);
let project_type = args.project_type.unwrap_or(ProjectType::Exe);
let cxx_standard = args.cxx_standard.or(Some(CxxStandard::Cxx17));
let folder_name = sanitize_folder_name(&name);
let output_path = if let Some(output) = args.output {
Path::new(&output).to_path_buf()
} else {
Path::new(".").join(&folder_name)
};
create_project(&name, &lang, &project_type, &cxx_standard, &output_path, args.force)?;
Ok(())
}