diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 5b5447a..d6f7eb8 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,3 +1,7 @@ -add_executable(cxxopts main.cpp cxxopts.cpp) +add_executable(example main.cpp) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall") + +add_library(cxxopts SHARED cxxopts.cpp) + +target_link_libraries(example cxxopts) diff --git a/src/cxxopts.cpp b/src/cxxopts.cpp index c458121..9f99eb2 100644 --- a/src/cxxopts.cpp +++ b/src/cxxopts.cpp @@ -1,13 +1,41 @@ +/* + +Copyright (c) 2014 Jarryd Beck + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. + +*/ + #include "cxxopts.hpp" namespace cxxopts { - std::basic_regex option_matcher - ("--([[:alpha:]][-_[:alpha:]]+)(=(.*))?|-([a-zA-Z]+)"); + namespace + { - std::basic_regex option_specifier - ("(([a-zA-Z]),)?([a-zA-Z][-_a-zA-Z]+)"); + std::basic_regex option_matcher + ("--([[:alpha:]][-_[:alpha:]]+)(=(.*))?|-([a-zA-Z]+)"); + + std::basic_regex option_specifier + ("(([a-zA-Z]),)?([a-zA-Z][-_a-zA-Z]+)"); + } OptionAdder Options::add_options() diff --git a/src/cxxopts.hpp b/src/cxxopts.hpp index e25345d..75e8b1a 100644 --- a/src/cxxopts.hpp +++ b/src/cxxopts.hpp @@ -1,3 +1,27 @@ +/* + +Copyright (c) 2014 Jarryd Beck + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. + +*/ + #include #include #include @@ -31,18 +55,16 @@ namespace cxxopts } template - bool - value_has_arg(bool*) + struct value_has_arg { - return false; - } + static constexpr bool value = true; + }; - template - bool - value_has_arg(T*) + template <> + struct value_has_arg { - return true; - } + static constexpr bool value = false; + }; template class default_value : public Value @@ -68,7 +90,7 @@ namespace cxxopts bool has_arg() const { - return value_has_arg(m_store); + return value_has_arg::value; } const T& @@ -98,13 +120,6 @@ namespace cxxopts return std::make_shared>(); } - extern std::basic_regex option_matcher; - - extern std::basic_regex option_specifier; - - class MessageException - ; - class OptionException : public std::exception { public: diff --git a/src/main.cpp b/src/main.cpp index 99d3b8a..a6d9d3e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,7 +1,28 @@ -#include +/* + +Copyright (c) 2014 Jarryd Beck + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. + +*/ + #include -#include -#include #include "cxxopts.hpp" @@ -10,51 +31,40 @@ int main(int argc, char* argv[]) try { - std::match_results result; + cxxopts::Options options; - for (int i = 1; i < argc; ++i) - { - std::cout << "Argument " << i << std::endl; - std::regex_match(argv[i], result, cxxopts::option_matcher); - std::cout << "empty = " << result.empty() << std::endl; - std::cout << "size = " << result.size() << std::endl; + options.add_options() + ("a,apple", "an apple") + ("b,bob", "Bob") + ("f,file", "File", cxxopts::value()) + ; - std::cout << "matches:" << std::endl; - for (int j = 0; j != result.size(); ++j) + options.parse(argc, argv); + + if (options.count("a")) { - std::cout << "arg " << j << ": " << result[j] << std::endl; + std::cout << "Saw option ‘a’" << std::endl; } - } - cxxopts::Options options; + if (options.count("b")) + { + std::cout << "Saw option ‘b’" << std::endl; + } - options.add_options() - ("a,apple", "an apple") - ("b,bob", "Bob") - ("f,file", "File", cxxopts::value()) - ; + if (options.count("f")) + { + std::cout << "File = " << options["f"].as() + << std::endl; + } - options.parse(argc, argv); + if (options.count("help")) + { + //std::cout << options.print_help(); + } - if (options.count("a")) + } catch (const cxxopts::OptionException& e) { - std::cout << "Saw option ‘a’" << std::endl; - } - - if (options.count("b")) - { - std::cout << "Saw option ‘b’" << std::endl; - } - - if (options.count("f")) - { - std::cout << "File = " << options["f"].as() - << std::endl; - } - - } catch (const std::regex_error& e) - { - std::cout << "regex_error: " << e.what() << std::endl; + std::cout << "error parsing options: " << e.what() << std::endl; exit(1); }