PackBinary/cmd_parse.cpp
2024-10-29 17:00:05 +08:00

86 lines
2.7 KiB
C++

#include "cmd_parse.h"
#include <boost/filesystem.hpp>
#include <iostream>
namespace fs = boost::filesystem;
CCmdParse::CCmdParse()
{
}
bool CCmdParse::cmdParse(int argc, char* argv[])
{
cmd::options_description desc("options");
desc.add_options()("help,h", "produce help message")(
"dirs,d", cmd::value<std::vector<std::string>>(),
"set search dirs")("mode,m", cmd::value<int>()->default_value(-1),
"设置执行模式,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。");
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;
}
if (vm.count("mode")) {
result_.mode = vm["mode"].as<int>();
}
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>();
}
return true;
}
bool CCmdParse::checkArgs()
{
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 (!fs::exists(tp) || !fs::is_regular_file(tp)) {
std::cout << type << path << ",不存在\n";
return false;
} else {
std::cout << type << path << ",检查通过!\n";
return true;
}
};
std::cout << "检查 ==========================================>\n";
check_file_dir(result_.binary, true);
check_file_dir(result_.ico, true);
return false;
}