Files
nudt-compiler-cpp/src/utils/CLI.cpp

43 lines
1.1 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 命令行参数解析:
// - 解析输入/输出路径
// - 解析输出类型IR/MIR/ASM与优化级别等选项
// - 将参数传递给 main.cpp 的编译流水线驱动
#include "utils/CLI.h"
#include <cstring>
#include <string>
#include <stdexcept>
CLIOptions ParseCLI(int argc, char** argv) {
CLIOptions opt;
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;
}