diff options
author | Jarryd Beck <jarro.2783@gmail.com> | 2019-08-23 01:26:16 +0300 |
---|---|---|
committer | Jarryd Beck <jarro.2783@gmail.com> | 2019-08-23 01:26:16 +0300 |
commit | 4a0af0e95038450baa160ed7ba863072c39f457c (patch) | |
tree | f6ffd0fd9ce692137cba75e66d259c4aacdf826f | |
parent | f4f4ece8096bbe0be081250d2ae6e2a617c97d5f (diff) |
Fix parsing char type
Fixes #201. Parse char type correctly and check for length.
-rw-r--r-- | CHANGELOG.md | 1 | ||||
-rw-r--r-- | README.md | 2 | ||||
-rw-r--r-- | include/cxxopts.hpp | 11 | ||||
-rw-r--r-- | src/example.cpp | 6 |
4 files changed, 19 insertions, 1 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index bd0eed1..4bddcff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ options. The project adheres to semantic versioning. * Allow for exceptions to be disabled. * Fix duplicate default options when there is a short and long option. * Add `CXXOPTS_NO_EXCEPTIONS` to disable exceptions. +* Fix char parsing for space and check for length. ## 2.2 @@ -150,7 +150,7 @@ GCC >= 4.9 or clang >= 3.1 with libc++ are known to work. The following compilers are known not to work: -* MSVC 13 +* MSVC 2013 # TODO list diff --git a/include/cxxopts.hpp b/include/cxxopts.hpp index 9cde72d..ed3c6a2 100644 --- a/include/cxxopts.hpp +++ b/include/cxxopts.hpp @@ -763,6 +763,17 @@ namespace cxxopts } #endif + inline + void parse_value(const std::string& text, char& c) + { + if (text.length() != 1) + { + throw_or_mimic<argument_incorrect_type>(text); + } + + c = text[0]; + } + template <typename T> struct type_is_container { diff --git a/src/example.cpp b/src/example.cpp index d3bbb0a..5eb6ca2 100644 --- a/src/example.cpp +++ b/src/example.cpp @@ -43,6 +43,7 @@ parse(int argc, char* argv[]) .add_options() ("a,apple", "an apple", cxxopts::value<bool>(apple)) ("b,bob", "Bob") + ("char", "A character", cxxopts::value<char>()) ("t,true", "True", cxxopts::value<bool>()->default_value("true")) ("f, file", "File", cxxopts::value<std::vector<std::string>>(), "FILE") ("i,input", "Input", cxxopts::value<std::string>()) @@ -89,6 +90,11 @@ parse(int argc, char* argv[]) std::cout << "Saw option ‘b’" << std::endl; } + if (result.count("char")) + { + std::cout << "Saw a character ‘" << result["char"].as<char>() << "’" << std::endl; + } + if (result.count("f")) { auto& ff = result["f"].as<std::vector<std::string>>(); |