#include #include #include #include #include "bf.request.h" #include "bf.util.h" #include "bf.xlnt.h" #ifdef _WIN32 #include "Windows.h" #endif int main(int argc, char** argv) { #ifdef _WIN32 SetConsoleOutputCP(CP_UTF8); #endif CLI::App app{"百度翻译xlsx批量工具。"}; argv = app.ensure_utf8(argv); std::string workbook; std::string sheetName; std::string from = "auto"; std::string to = "en"; bool testRequest = false; std::vector tasks; // 存储多个字符串 app.add_option("-r,--task", tasks, "要处理的行列范围(起始行,起始列,结束行,结束列),如:1,1,10,10)")->multi_option_policy(); app.add_option("-s,--sheet", sheetName, "工作表名称"); app.add_option("-w,--workbook", workbook, "工作簿")->check(CLI::ExistingFile); app.add_option("-f,--from", from, "源语言(默认auto)"); app.add_option("-p,--to", to, "目标语言(默认en)"); app.add_flag("--test-request", testRequest, "测试请求"); // 添加其他可选参数 std::string config = "baidu_fanyi.json"; app.add_option("-c,--config", config, "配置文件(默认baidu_fanyi.json)"); bool verbose = false; app.add_flag("-v,--verbose", verbose, "详细输出"); // 解析命令行参数 CLI11_PARSE(app, argc, argv); BF_Util::initLogger(); BF_Util::setLogLevel(spdlog::level::debug); // 测试参数,这里直接返回。 // for (const auto& task : tasks) { // gLogger->info("任务:{}", task); // } // return 0; gLogger->debug("开始处理xlsx文件:{}", workbook); BF_Interface* request = new BF_Request(); std::shared_ptr del(nullptr, [request](void*) { delete request; }); std::shared_ptr bc = std::make_shared(); if (!bc->parseConfig(config)) { gLogger->error("解析配置文件失败!"); return -1; } request->setConfig(bc); if (testRequest) { gLogger->info("测试请求开始..."); gLogger->info("测试内容:[你好,世界。] from auto to en."); std::string readyData = request->doReady("你好,世界。", from, to); std::string data = fmt::format("{}{}", bc->baseUrl, readyData); std::string result = request->getResult(data); gLogger->info("测试请求结束!"); gLogger->info("结果:[{}]", result); return 0; } std::vector xlsxTasks; for (const auto& task : tasks) { auto v = BF_Util::split(task, ","); if (v.size() != 4) { gLogger->error("任务格式错误:{}", task); return -1; } XlsxTask xlsxTask; xlsxTask.startRow = std::stoi(v[0]); xlsxTask.startCol = std::stoi(v[1]); xlsxTask.endRow = std::stoi(v[2]); xlsxTask.endCol = std::stoi(v[3]); xlsxTask.from = from; xlsxTask.to = to; xlsxTasks.push_back(xlsxTask); } if (xlsxTasks.empty()) { gLogger->error("没有指定任务!"); return -1; } std::shared_ptr xlnt = std::make_shared(); xlnt->Set(request, bc); if (!xlnt->parseXlsx(workbook, xlsxTasks, sheetName)) { gLogger->error("处理xlsx文件失败!"); return -1; } gLogger->info("结束!"); return 0; }