diff options
| author | Evsyukov Denis Anatolyevich <[email protected]> | 2020-01-09 15:44:35 +0300 |
|---|---|---|
| committer | Evsyukov Denis Anatolyevich <[email protected]> | 2020-01-09 16:02:10 +0300 |
| commit | 8736f2b61aee0821a6764dbc9d42e62d89d22529 (patch) | |
| tree | 1880d44cde59caa41535782227fe47865b3236ab /src | |
| parent | Added sha256 functions (diff) | |
| download | t-8736f2b61aee0821a6764dbc9d42e62d89d22529.tar.xz t-8736f2b61aee0821a6764dbc9d42e62d89d22529.zip | |
Some refactoring
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.cpp | 56 | ||||
| -rw-r--r-- | src/opts/cxxopts.hpp (renamed from src/cxxopts.hpp) | 12 | ||||
| -rw-r--r-- | src/opts/opts.cpp | 55 | ||||
| -rw-r--r-- | src/opts/opts.hpp | 10 |
4 files changed, 73 insertions, 60 deletions
diff --git a/src/main.cpp b/src/main.cpp index b5f6099..5df353f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,58 +1,6 @@ -#include <iostream> -#include <string> -#include <filesystem> - -#include "cxxopts.hpp" -#include "functions.hpp" -#include "sha256/sha256.h" - -using namespace std; +#include "opts/opts.hpp" int main(int argc, char *argv[]) { - cxxopts::Options options("MyProgram", "One line description of MyProgram"); - options - .allow_unrecognised_options() - .add_options() - ("positional", - "Positional arguments: these are the arguments that are entered " - "without an option", cxxopts::value<std::vector<std::string>>()) - ("e,edit", "edit TASK to contain TEXT", cxxopts::value<std::string>()) - ("f,finish", "mark TASK as finished", cxxopts::value<std::string>()) - ("r,remove", "Remove TASK from list", cxxopts::value<std::string>()) - ("l,list", "work on LIST", cxxopts::value<std::string>()) - ("t,taskdir", "work on the lists in DIR", cxxopts::value<std::string>()) - ("d,delete-if-empty", "delete the task file if it becomes empty") - ("g,grep", "print only tasks that contain WORD", cxxopts::value<std::string>()) - ("v,verbose", "print more detailed output (full task ids, etc)") - ("q,quiet", "print less detailed output (no task ids, etc)") - ("D,done", "list done tasks instead of unfinished ones") - ("h,help", "HELP"); - options.parse_positional({"positional"}); - auto result = options.parse(argc, argv); - const auto &arguments = result.arguments(); - if (result.count("help")) { - std::cout << options.help() << std::endl; - exit(0); - } - if (result.count("positional")) { - std::string str; - auto &v = result["positional"].as<std::vector<std::string>>(); - for (const auto &s : v) { - str += s + " "; - } - str = trim(str); - - cout << "sha256('" << str << "'): " << sha256(str) << endl; - } - std::string taskdir; - if (result.count("taskdir")) { - taskdir = result["taskdir"].as<std::string>(); - } else { - namespace fs = std::filesystem; - taskdir=fs::current_path(); - } - - std::cout << "Current path is " << taskdir << '\n'; - std::cout << "Saw " << arguments.size() << " arguments" << std::endl; + execute_commands(argc, argv); return 0; }
\ No newline at end of file diff --git a/src/cxxopts.hpp b/src/opts/cxxopts.hpp index 9cc7c78..236ecbe 100644 --- a/src/cxxopts.hpp +++ b/src/opts/cxxopts.hpp @@ -63,7 +63,7 @@ namespace cxxopts }; } -//when we ask cxxopts to use Unicode, help strings are processed using ICU, +//when we ask opts to use Unicode, help strings are processed using ICU, //which results in the correct lengths being computed for strings when they //are formatted for the help output //it is necessary to make sure that <unicode/unistr.h> can be found by the @@ -72,7 +72,7 @@ namespace cxxopts #ifdef CXXOPTS_USE_UNICODE #include <unicode/unistr.h> -namespace cxxopts +namespace opts { typedef icu::UnicodeString String; @@ -190,17 +190,17 @@ namespace cxxopts namespace std { inline - cxxopts::UnicodeStringIterator + opts::UnicodeStringIterator begin(const icu::UnicodeString& s) { - return cxxopts::UnicodeStringIterator(&s, 0); + return opts::UnicodeStringIterator(&s, 0); } inline - cxxopts::UnicodeStringIterator + opts::UnicodeStringIterator end(const icu::UnicodeString& s) { - return cxxopts::UnicodeStringIterator(&s, s.length()); + return opts::UnicodeStringIterator(&s, s.length()); } } diff --git a/src/opts/opts.cpp b/src/opts/opts.cpp new file mode 100644 index 0000000..fac0356 --- /dev/null +++ b/src/opts/opts.cpp @@ -0,0 +1,55 @@ +#include <filesystem> + +#include "cxxopts.hpp" +#include "../functions.hpp" +#include "../sha256/sha256.h" + +using namespace std; + +void execute_commands(int argc, char **argv) { + cxxopts::Options options("MyProgram", "One line description of MyProgram"); + options + .allow_unrecognised_options() + .add_options() + ("positional", + "Positional arguments: these are the arguments that are entered " + "without an option", cxxopts::value<std::vector<std::string >>()) + ("e,edit", "edit TASK to contain TEXT", cxxopts::value<std::string>()) + ("f,finish", "mark TASK as finished", cxxopts::value<std::string>()) + ("r,remove", "Remove TASK from list", cxxopts::value<std::string>()) + ("l,list", "work on LIST", cxxopts::value<std::string>()) + ("t,taskdir", "work on the lists in DIR", cxxopts::value<std::string>()) + ("d,delete-if-empty", "delete the task file if it becomes empty") + ("g,grep", "print only tasks that contain WORD", cxxopts::value<std::string>()) + ("v,verbose", "print more detailed output (full task ids, etc)") + ("q,quiet", "print less detailed output (no task ids, etc)") + ("D,done", "list done tasks instead of unfinished ones") + ("h,help", "HELP"); + options.parse_positional({"positional"}); + auto result = options.parse(argc, argv); + const auto &arguments = result.arguments(); + if (result.count("help")) { + std::cout << options.help() << std::endl; + exit(0); + } + if (result.count("positional")) { + std::string str; + auto &v = result["positional"].as<std::vector<std::string >>(); + for (const auto &s : v) { + str += s + " "; + } + str = trim(str); + + cout << "sha256('" << str << "'): " << sha256(str) << endl; + } + std::string taskdir; + if (result.count("taskdir")) { + taskdir = result["taskdir"].as<std::string>(); + } else { + namespace fs = std::filesystem; + taskdir = fs::current_path(); + } + + std::cout << "Current path is " << taskdir << '\n'; + std::cout << "Saw " << arguments.size() << " arguments" << std::endl; +}
\ No newline at end of file diff --git a/src/opts/opts.hpp b/src/opts/opts.hpp new file mode 100644 index 0000000..0f38abf --- /dev/null +++ b/src/opts/opts.hpp @@ -0,0 +1,10 @@ +// +// Created by d.evsyukov on 09.01.2020. +// + +#ifndef T_OPTS_HPP +#define T_OPTS_HPP + +void execute_commands(int argc, char **argv); + +#endif //T_OPTS_HPP |