From 4a0af0e95038450baa160ed7ba863072c39f457c Mon Sep 17 00:00:00 2001 From: Jarryd Beck Date: Fri, 23 Aug 2019 08:26:16 +1000 Subject: [PATCH] Fix parsing char type Fixes #201. Parse char type correctly and check for length. --- CHANGELOG.md | 1 + README.md | 2 +- include/cxxopts.hpp | 11 +++++++++++ src/example.cpp | 6 ++++++ 4 files changed, 19 insertions(+), 1 deletion(-) 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 diff --git a/README.md b/README.md index 2e18a5e..6ec80ac 100644 --- a/README.md +++ b/README.md @@ -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(text); + } + + c = text[0]; + } + template 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(apple)) ("b,bob", "Bob") + ("char", "A character", cxxopts::value()) ("t,true", "True", cxxopts::value()->default_value("true")) ("f, file", "File", cxxopts::value>(), "FILE") ("i,input", "Input", cxxopts::value()) @@ -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() << "’" << std::endl; + } + if (result.count("f")) { auto& ff = result["f"].as>();