feat(dev): 支持 compiler --help 输出帮助信息

This commit is contained in:
Lane0218
2025-12-30 12:33:36 +08:00
parent c0c9f70f16
commit 9eefbb5ef7
5 changed files with 55 additions and 4 deletions

View File

@@ -5,13 +5,38 @@
#include "utils/CLI.h"
#include <cstring>
#include <string>
#include <stdexcept>
CLIOptions ParseCLI(int argc, char** argv) {
if (argc <= 1) {
throw std::runtime_error("用法: compiler <input.sy>");
}
CLIOptions opt;
opt.input = argv[1];
if (argc <= 1) {
throw std::runtime_error("用法: compiler [--help] <input.sy>");
}
for (int i = 1; i < argc; ++i) {
const char* arg = argv[i];
if (std::strcmp(arg, "-h") == 0 || std::strcmp(arg, "--help") == 0) {
opt.show_help = true;
return opt;
}
if (arg[0] == '-') {
throw std::runtime_error(std::string("未知参数: ") + arg +
"(使用 --help 查看用法)");
}
if (!opt.input.empty()) {
throw std::runtime_error(
"参数过多:当前只支持 1 个输入文件(使用 --help 查看用法)");
}
opt.input = arg;
}
if (opt.input.empty()) {
throw std::runtime_error("缺少输入文件:请提供 <input.sy>(使用 --help 查看用法)");
}
return opt;
}