Fix parsing char type

Fixes #201. Parse char type correctly and check for length.
This commit is contained in:
Jarryd Beck 2019-08-23 08:26:16 +10:00
parent f4f4ece809
commit 4a0af0e950
4 changed files with 19 additions and 1 deletions

View File

@ -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

View File

@ -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

View File

@ -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
{

View File

@ -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>>();