From 0663f42aecd518954cfce8bcd2cda1cb0d2ccf62 Mon Sep 17 00:00:00 2001 From: Armin Friedl Date: Sat, 1 May 2021 09:27:40 +0200 Subject: [PATCH] Separate options definition and parsing --- .gitignore | 1 + src/Opt.hpp | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.cpp | 56 ++++++------------------------------------------- 3 files changed, 66 insertions(+), 50 deletions(-) create mode 100644 src/Opt.hpp diff --git a/.gitignore b/.gitignore index 0e23fc9..8eb7c90 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .clangd/ +.cache/ build/ target/ compile_commands.json diff --git a/src/Opt.hpp b/src/Opt.hpp new file mode 100644 index 0000000..2087c42 --- /dev/null +++ b/src/Opt.hpp @@ -0,0 +1,59 @@ +#include +#include +#include +#include +#include +#include + +#include + +template <> +struct TCLAP::ArgTraits { + typedef ValueLike ValueCategory; +}; + +namespace xwim { + +namespace fs = std::filesystem; + +class Opt { + private: + // clang-format off + TCLAP::CmdLine cmd_line + {"xwim - Do What I Mean Extractor", ' ', "0.5.0"}; + + TCLAP::SwitchArg arg_compress + {"c", "compress", "Compress ", cmd_line, false}; + + TCLAP::SwitchArg arg_extract + {"x", "extract", "Extract ", cmd_line, false}; + + TCLAP::ValueArg arg_out + {"o", "out", "Out ", false, fs::path{}, "A path on the filesystem", cmd_line}; + + TCLAP::UnlabeledMultiArg arg_paths + {"Paths", "Filesystem paths to extract or compress", true, "A path on the filesystem", cmd_line}; + // clang-format on + + public: + void parse(int argc, char** argv) { cmd_line.parse(argc, argv); } + + void validate() { + if (arg_extract.isSet() && arg_compress.isSet()) { + // This is a bit ugly but `none-or-xor` only available in + // tclap-1.4 which is not well supported in current + // distributions + auto out = TCLAP::StdOutput{}; + TCLAP::ArgException e{ + "Cannot compress `-c` and extract `-x` simultaneously"}; + + try { + out.failure(cmd_line, e); + } catch (TCLAP::ExitException& e) { + exit(e.getExitStatus()); + } + } + } +}; + +} // namespace xwim diff --git a/src/main.cpp b/src/main.cpp index 1a6ca8a..e3e28da 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,73 +1,29 @@ #include #include #include -#include -#include -#include -#include -#include -#include #include #include +#include #include "Common.hpp" +#include "Opt.hpp" #include "Log.hpp" #include "Xwim.hpp" using namespace xwim; using namespace std; -namespace fs = std::filesystem; - -template <> -struct TCLAP::ArgTraits { - typedef ValueLike ValueCategory; -}; int main(int argc, char** argv) { log::init(); - TCLAP::CmdLine cmd{"xwim - Do What I Mean Extractor", ' ', "0.3.0"}; + Opt opt; + opt.parse(argc, argv); + opt.validate(); - TCLAP::SwitchArg arg_compress{"c", "compress", "Compress ", cmd, - false}; - TCLAP::SwitchArg arg_extract{"x", "extract", "Extract ", cmd, false}; - - TCLAP::ValueArg arg_outfile{ - "o", "out", "Out ", - false, fs::path{}, "A path on the filesystem", - cmd}; - TCLAP::UnlabeledMultiArg arg_infiles{ - "Files", "Archive to extract or files to compress", true, - "A path on the filesystem", cmd}; - - Xwim xwim; - - cmd.parse(argc, argv); - - if (arg_extract.isSet() && arg_compress.isSet()) { - // This is a bit ugly but `none-or-xor` only available in - // tclap-1.4 which is not well supported in current - // distributions - auto out = TCLAP::StdOutput{}; - TCLAP::ArgException e{ - "Cannot compress `-c` and extract `-x` simultaneously"}; - try { - out.failure(cmd, e); - } catch (TCLAP::ExitException& e) { - exit(e.getExitStatus()); - } - } - - // `none-or-xor` ensured already - if (arg_extract.isSet()) xwim.setExtract(); - if (arg_compress.isSet()) xwim.setCompress(); - - if (arg_outfile.isSet()) xwim.setOut(arg_outfile.getValue()); - if (arg_infiles.isSet()) xwim.setIns(arg_infiles.getValue()); + Xwim xwim{}; try { - xwim.try_infer(); xwim.dwim(); } catch (XwimError& e) { spdlog::error(e.what());