PackBinary/cmd_parse.cpp

133 lines
4.3 KiB
C++
Raw Normal View History

2024-10-27 15:51:46 +08:00
#include "cmd_parse.h"
#include "install.h"
#include "pack.h"
2024-10-29 17:00:05 +08:00
#include <boost/filesystem.hpp>
2024-10-29 12:42:11 +08:00
#include <iostream>
2024-10-27 15:51:46 +08:00
2024-10-29 17:00:05 +08:00
namespace fs = boost::filesystem;
2024-10-29 12:42:11 +08:00
CCmdParse::CCmdParse()
2024-10-27 15:51:46 +08:00
{
}
2024-10-29 12:42:11 +08:00
bool CCmdParse::cmdParse(int argc, char* argv[])
2024-10-27 15:51:46 +08:00
{
2024-10-29 12:42:11 +08:00
cmd::options_description desc("options");
desc.add_options()("help,h", "produce help message")(
2024-10-29 17:00:05 +08:00
"dirs,d", cmd::value<std::vector<std::string>>(),
2024-10-29 12:42:11 +08:00
"set search dirs")("mode,m", cmd::value<int>()->default_value(-1),
2024-10-29 17:00:05 +08:00
"设置执行模式,0-打包,1-安装")(
"purpose,p", cmd::value<std::string>(),
"安装目标目录")("file,f", cmd::value<std::string>(), "二进制文件")(
"ico,i", cmd::value<std::string>(), "图标文件")(
"category,c", cmd::value<std::string>()->default_value("Utility"),
"分类名称,例如Utility。")("only,o",
cmd::value<bool>()->default_value(false),
"仅生成shell启动脚本(mode=1时)");
2024-10-29 12:42:11 +08:00
cmd::variables_map vm;
cmd::store(cmd::parse_command_line(argc, argv, desc), vm);
cmd::notify(vm);
if (vm.count("help")) {
std::cout << desc;
return false;
2024-10-27 15:51:46 +08:00
}
2024-10-29 12:42:11 +08:00
if (vm.count("mode")) {
result_.mode = vm["mode"].as<int>();
2024-10-27 15:51:46 +08:00
}
if (vm.count("only")) {
result_.only_shell = vm["only"].as<bool>();
}
2024-10-29 17:00:05 +08:00
if (vm.count("dirs")) {
result_.lib_dirs = vm["dirs"].as<std::vector<std::string>>();
}
if (vm.count("purpose")) {
result_.purpose_dir = vm["purpose"].as<std::string>();
}
if (vm.count("file")) {
result_.binary = vm["file"].as<std::string>();
}
if (vm.count("ico")) {
result_.ico = vm["ico"].as<std::string>();
}
if (vm.count("category")) {
result_.category = vm["category"].as<std::string>();
2024-10-27 15:51:46 +08:00
}
2024-10-29 12:42:11 +08:00
return true;
2024-10-27 15:51:46 +08:00
}
bool CCmdParse::Run()
2024-10-27 15:51:46 +08:00
{
2024-10-29 17:00:05 +08:00
std::cout << "信息 ==========================================>\n";
std::cout << "binary:" << result_.binary << "\n";
std::cout << "category:" << result_.category << "\n";
std::cout << "ico:" << result_.ico << "\n";
std::cout << "mode:" << result_.mode << "\n";
std::cout << "purpose_dir:" << result_.purpose_dir << "\n";
std::cout << "dirs:" << result_.lib_dirs.size() << "\n";
for (const auto& item : result_.lib_dirs) {
std::cout << ">>" << item << "\n";
}
auto check_file_dir = [&](const std::string& path, bool is_file) -> bool {
std::string type = is_file ? "文件:" : "文件夹:";
fs::path tp(path);
if (is_file) {
if (!fs::exists(tp) || !fs::is_regular_file(tp)) {
std::cout << type << path << ",不存在\n";
return false;
} else {
std::cout << type << path << ",检查通过!\n";
return true;
}
2024-10-29 17:00:05 +08:00
} else {
if (!fs::exists(tp)) {
try {
fs::create_directories(tp);
std::cout << type << path << ",不存在但自动创建成功。\n";
return true;
} catch (const std::exception& e) {
std::cerr << e.what() << '\n';
return false;
}
std::cout << type << path << ",不存在\n";
return false;
} else {
std::cout << type << path << ",检查通过!\n";
return true;
}
2024-10-29 17:00:05 +08:00
}
};
std::cout << "检查 ==========================================>\n";
if (!check_file_dir(result_.binary, true)) {
return false;
}
switch (result_.mode) {
case 0: {
if (!check_file_dir(result_.purpose_dir, false)) {
return false;
}
std::cout << "结果 ==========================================>\n";
CPackBinary pack;
return pack.startPack(result_);
}
case 1: {
if (!result_.only_shell && !check_file_dir(result_.ico, true)) {
return false;
}
std::cout << "结果 ==========================================>\n";
CInstallBinary install;
return install.startInstall(result_);
}
default:
std::cout << "不正确的mode模式。" << std::endl;
return false;
}
2024-10-27 15:51:46 +08:00
}