From db5477ddd8c62d57b48f2b8d19137816b2a6d745 Mon Sep 17 00:00:00 2001 From: Jarryd Beck Date: Fri, 26 Sep 2014 02:19:23 +1000 Subject: [PATCH] getting option adding working --- src/CMakeLists.txt | 2 +- src/cxxopts.hpp | 46 ++++++++++++++++++++++++++++++++++++++++++---- src/main.cpp | 11 ++++++++++- 3 files changed, 53 insertions(+), 6 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 09162e3..5b5447a 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,3 +1,3 @@ -add_executable(cxxopts main.cpp) +add_executable(cxxopts main.cpp cxxopts.cpp) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall") diff --git a/src/cxxopts.hpp b/src/cxxopts.hpp index 8adef0f..4f08cf6 100644 --- a/src/cxxopts.hpp +++ b/src/cxxopts.hpp @@ -1,10 +1,48 @@ #include +#include namespace cxxopts { - std::basic_regex option_matcher - ("--([a-zA-Z][-_a-zA-Z]+)(=(.*))?|-([a-zA-Z]+)"); + extern std::basic_regex option_matcher; - std::basic_regex option_specifier - ("(([a-zA-Z]),)?([a-zA-Z][-_a-zA-Z]+)"); + extern std::basic_regex option_specifier; + + class OptionAdder; + + class Options + { + public: + + void + parse(int& argc, char**& argv); + + OptionAdder + add_options(); + + private: + friend class OptionAdder; + + std::set m_short; + std::set m_long; + }; + + class OptionAdder + { + public: + + OptionAdder(Options& options) + : m_options(options) + { + } + + OptionAdder& + operator() + ( + const std::string& opts, + const std::string& desc + ); + + private: + Options& m_options; + }; } diff --git a/src/main.cpp b/src/main.cpp index 1faeee0..9cf4368 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,12 +1,21 @@ #include +#include #include "cxxopts.hpp" int main(int argc, char* argv[]) { + std::locale::global(std::locale("")); try { + cxxopts::option_matcher.imbue(std::locale("")); + cxxopts::option_matcher.assign + ("--([[:alpha:]][-_[:alpha:]]+)(=(.*))?|-([a-zA-Z]+)", + std::regex_constants::extended | std::regex_constants::ECMAScript | + std::regex_constants::collate); + std::cout << cxxopts::option_matcher.getloc().name() << std::endl; + std::match_results result; for (int i = 1; i < argc; ++i) @@ -19,7 +28,7 @@ int main(int argc, char* argv[]) std::cout << "matches:" << std::endl; for (int j = 0; j != result.size(); ++j) { - std::cout << result[j] << std::endl; + std::cout << "arg " << j << ": " << result[j] << std::endl; } }