From ae0e8784822c9a42512e7170608a9fbfabe0ed0d Mon Sep 17 00:00:00 2001 From: taynpg Date: Tue, 29 Oct 2024 12:42:11 +0800 Subject: [PATCH] =?UTF-8?q?change=EF=BC=9A=E4=B8=89=E6=96=B9=E5=BA=93?= =?UTF-8?q?=E6=94=B9=E6=88=90Boost=E5=BA=93=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/settings.json | 7 +- CMakeLists.txt | 10 ++- cmd_parse.cpp | 166 +++++++----------------------------------- cmd_parse.h | 40 ++-------- logic.cpp | 20 ----- logic.h | 15 ---- main.cpp | 10 ++- pack.cpp | 10 ++- pack.h | 1 - public.hpp | 3 +- 10 files changed, 61 insertions(+), 221 deletions(-) delete mode 100644 logic.cpp delete mode 100644 logic.h diff --git a/.vscode/settings.json b/.vscode/settings.json index 9748fd6..c438283 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -17,12 +17,11 @@ } ], "args": [ - "/f", "mfile", "/i", "mico", "/c", "mc", "/m", "mm" ] }, - // "cmake.configureSettings": { - // "CMAKE_TOOLCHAIN_FILE": "${env:VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake" - // }, + "cmake.configureSettings": { + "CMAKE_TOOLCHAIN_FILE": "${env:VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake" + }, "cmake.options.statusBarVisibility": "visible", "cmake.generator": "Ninja", "C_Cpp.default.compileCommands": "${workspaceRoot}/build/compile_commands.json", diff --git a/CMakeLists.txt b/CMakeLists.txt index b6c930a..0cf6df6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,6 +4,9 @@ project(PackBinary LANGUAGES CXX) set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(APEEND_THID_LIB_DIR "$ENV{HOME}/mlib/boost") +list(APPEND CMAKE_PREFIX_PATH ${APEEND_THID_LIB_DIR}) + if (MSVC) add_compile_options(/source-charset:utf-8) endif() @@ -16,10 +19,11 @@ set(SOURCES_FILE cmd_parse.cpp public.hpp pack.h pack.cpp install.h install.cpp - logic.h logic.cpp ) -find_package(Poco REQUIRED Foundation Util) +set(Boost_USE_STATIC_LIBS OFF) +find_package(Boost REQUIRED program_options) +include_directories(${Boost_INCLUDE_DIR}) add_executable(PackBinary ${SOURCES_FILE}) -target_link_libraries(PackBinary PRIVATE Poco::Foundation Poco::Util) \ No newline at end of file +target_link_libraries(PackBinary PRIVATE ${Boost_LIBRARIES}) \ No newline at end of file diff --git a/cmd_parse.cpp b/cmd_parse.cpp index 99df0eb..11b589a 100644 --- a/cmd_parse.cpp +++ b/cmd_parse.cpp @@ -1,150 +1,38 @@ #include "cmd_parse.h" -#include "logic.h" -#include +#include -void CPackBinaryCmd::initialize(Poco::Util::Application& self) +CCmdParse::CCmdParse() { - ServerApplication::initialize(self); } -void CPackBinaryCmd::uninitialize() +bool CCmdParse::cmdParse(int argc, char* argv[]) { - ServerApplication::uninitialize(); -} + cmd::options_description desc("options"); + desc.add_options()("help,h", "produce help message")( + "dirs,d", cmd::value>()->multitoken(), + "set search dirs")("mode,m", cmd::value()->default_value(-1), + "设置执行模式,0-打包,1-安装"); -void CPackBinaryCmd::defineOptions(Poco::Util::OptionSet& options) -{ - ServerApplication::defineOptions(options); - options.addOption(Poco::Util::Option("help", "h", "Help Message") - .required(false) - .repeatable(false) - .callback(Poco::Util::OptionCallback( - this, &CPackBinaryCmd::handleHelp))); + cmd::variables_map vm; + cmd::store(cmd::parse_command_line(argc, argv, desc), vm); + cmd::notify(vm); - options.addOption(Poco::Util::Option("ico", "i", "ico_file") - .required(false) - .repeatable(false) - .argument("ico_file's full path") - .callback(Poco::Util::OptionCallback( - this, &CPackBinaryCmd::handleInput))); - options.addOption(Poco::Util::Option("file", "f", "binary_file") - .required(false) - .repeatable(false) - // argument 就是 require 什么东西的具体介绍。 - .argument("binary_file's full path") - .binding("nobinding") - .callback(Poco::Util::OptionCallback( - this, &CPackBinaryCmd::handleInput))); - options.addOption(Poco::Util::Option("category", "c", "category name") - .required(false) - .repeatable(false) - .argument("category name") - .callback(Poco::Util::OptionCallback( - this, &CPackBinaryCmd::handleInput))); - - options.addOption(Poco::Util::Option("purpose", "p", "purpose dir") - .required(false) - .repeatable(false) - .argument("purpose dir") - .callback(Poco::Util::OptionCallback( - this, &CPackBinaryCmd::handleInput))); - options.addOption(Poco::Util::Option("mode", "m", "mode name") - .required(false) - .repeatable(false) - .argument("mode name") - .callback(Poco::Util::OptionCallback( - this, &CPackBinaryCmd::handleInput))); - options.addOption(Poco::Util::Option("ext", "e", "dirs_when_search_lib") - .required(false) - .repeatable(true) - .argument("dirs_when_search_lib") - .callback(Poco::Util::OptionCallback( - this, &CPackBinaryCmd::handleInput))); -} - -void CPackBinaryCmd::handleHelp(const std::string& name, - const std::string& value) -{ - std::string introduce = "" - "PackBinary options:\n" - " -ico 安装快捷方式时的ico文件路径。\n" - " -file 二进制文件。\n" - " -include 搜索的库路径。\n" - " -category 分类名称。\n" - " -mode 执行方式(0-打包,1-安装)。" - ""; - std::cout << introduce << std::endl; - stopOptionsProcessing(); -} - -void CPackBinaryCmd::handleInput(const std::string& name, - const std::string& value) -{ - if (name == "ico") { - result_.ico = value; - return; - } - if (name == "file") { - result_.binary = value; - return; - } - if (name == "purpose") { - result_.purpose_dir = value; - return; - } - if (name == "ext") { - result_.lib_dirs.push_back(value); - return; - } - if (name == "mode") { - result_.mode = value.empty() ? -1 : std::stoi(value); - return; - } - if (name == "category") { - result_.category = value; - return; - } -} - -int CPackBinaryCmd::main(const std::vector& args) -{ - if (!validCheck()) { - return Application::EXIT_CONFIG; - } - CMainLogic mainLogic; - if (mainLogic.run(result_)) { - return Application::EXIT_OK; - } else { - return Application::EXIT_SOFTWARE; - } -} - -bool CPackBinaryCmd::validCheck() -{ - std::cout << "ico:" << result_.ico << std::endl; - std::cout << "file:" << result_.binary << std::endl; - std::cout << "mode:" << result_.mode << std::endl; - std::cout << "category:" << result_.category << std::endl; - std::cout << "include, size = " << result_.lib_dirs.size() << std::endl; - - for (const auto& item : result_.lib_dirs) { - std::cout << ">>" << item << std::endl; - } - - switch (result_.mode) { - case 0: { - Poco::File file(result_.binary); - if (!file.exists()) { - std::cout << "binary file not exist." << std::endl; - return false; - } - return true; - } - case 1: { - return true; - } - default: - std::cout << "不合法的mode内容。" << std::endl; + if (vm.count("help")) { + std::cout << desc; return false; } + + if (vm.count("mode")) { + result_.mode = vm["mode"].as(); + } + if (vm.count("dir")) { + result_.lib_dirs = vm["dir"].as>(); + } + + return true; +} + +bool CCmdParse::checkArgs() +{ + return false; } diff --git a/cmd_parse.h b/cmd_parse.h index 7cffbf7..f7fa991 100644 --- a/cmd_parse.h +++ b/cmd_parse.h @@ -1,42 +1,18 @@ #ifndef CMD_PARSE_HEADER #define CMD_PARSE_HEADER -#include -#include -#include -#include -#include #include "public.hpp" +#include -// Poco::Util::Validator* month = new Poco::Util::RegExpValidator( -// "[0-9]{6,8}"); -// options.addOption( -// Option("month", "m", -// "date(yyyymm/yyyymmdd)").required(true).repeatable( -// false).argument("month").validator(month).binding( -// "application.month").callback( -// Poco::Util::OptionCallback < DataLoader -// > (this, &DataLoader::handleMonth))); -// ———————————————— +namespace cmd = boost::program_options; -// 版权声明:本文为博主原创文章,遵循 CC 4.0 -// BY-SA -// 版权协议,转载请附上原文出处链接和本声明。 - -// 原文链接:https://blog.csdn.net/hwjcmozw/article/details/42963633 - -class CPackBinaryCmd : public Poco::Util::ServerApplication +class CCmdParse { -protected: - void initialize(Poco::Util::Application& self) override; - void uninitialize() override; - void defineOptions(Poco::Util::OptionSet& options) override; - void handleHelp(const std::string& name, const std::string& value); - void handleInput(const std::string& name, const std::string& value); - int main(const std::vector& args) override; - -private: - bool validCheck(); +public: + CCmdParse(); +public: + bool cmdParse(int argc, char* argv[]); + bool checkArgs(); private: CmdResult result_; }; diff --git a/logic.cpp b/logic.cpp deleted file mode 100644 index 420d066..0000000 --- a/logic.cpp +++ /dev/null @@ -1,20 +0,0 @@ -#include "logic.h" -#include "install.h" -#include "pack.h" - -bool CMainLogic::run(const CmdResult& result) -{ - switch (result.mode) { - case 0: { - CPackBinary pack; - return pack.startPack(result); - } - case 1: { - CInstallBinary install; - return install.startInstall(result); - } - - default: - return false; - } -} \ No newline at end of file diff --git a/logic.h b/logic.h deleted file mode 100644 index d3e63e4..0000000 --- a/logic.h +++ /dev/null @@ -1,15 +0,0 @@ -#ifndef LOGIC_HEADER -#define LOGIC_HEADER - -#include "public.hpp" - -class CMainLogic -{ -public: - CMainLogic() = default; - ~CMainLogic() = default; -public: - bool run(const CmdResult& result); -}; - -#endif \ No newline at end of file diff --git a/main.cpp b/main.cpp index fe5bcf5..b674c58 100644 --- a/main.cpp +++ b/main.cpp @@ -4,6 +4,12 @@ int main(int argc, char** argv) { - CPackBinaryCmd app; - return app.run(argc, argv); + CCmdParse parse; + if (!parse.cmdParse(argc, argv)) { + return 0; + } + + + + return 0; } diff --git a/pack.cpp b/pack.cpp index 402afb2..0a0e4ea 100644 --- a/pack.cpp +++ b/pack.cpp @@ -1,5 +1,6 @@ #include "pack.h" -#include +#include +#include bool CPackBinary::startPack(const CmdResult& result) { @@ -29,9 +30,10 @@ CPackBinary::getDepends(const std::string& path, } fclose(pf); - Poco::StringTokenizer token(output, "\t"); - for (const auto& item : token) { - std::cout << item << std::endl; + std::vector split; + boost::split(split, output, boost::is_any_of("\t")); + for (const auto& item : split) { + std::cout << item << "\n"; } return result; } diff --git a/pack.h b/pack.h index 24b5756..79aa7d6 100644 --- a/pack.h +++ b/pack.h @@ -2,7 +2,6 @@ #define PACK_HEADER #include "public.hpp" -#include #include class CPackBinary diff --git a/public.hpp b/public.hpp index 8a0cba9..6cf25fd 100644 --- a/public.hpp +++ b/public.hpp @@ -11,7 +11,8 @@ struct CmdResult { std::string category; std::vector lib_dirs; bool valid{false}; - int mode{0}; + int mode{-1}; + bool help_{false}; }; #endif \ No newline at end of file