Throw in ParseResult::as when option isn't present

Fixes #124.
This commit is contained in:
Jarryd Beck 2018-11-19 17:45:51 +11:00
parent c713b44d92
commit 84feb4bd87
3 changed files with 8 additions and 0 deletions

View File

@ -20,6 +20,7 @@ options. The project adheres to semantic versioning.
* Fix version numbering in CMakeLists.txt
* Remove unused declaration of the undefined `ParseResult::get_option`.
* Throw on invalid option syntax when beginning with a `-`.
* Throw in `as` when option wasn't present.
## 2.1.1

View File

@ -1048,6 +1048,10 @@ namespace cxxopts
const T&
as() const
{
if (m_value == nullptr) {
throw std::domain_error("No value");
}
#ifdef CXXOPTS_NO_RTTI
return static_cast<const values::standard_value<T>&>(*m_value).get();
#else

View File

@ -53,6 +53,7 @@ TEST_CASE("Basic options", "[options]")
("a,av", "a short option with a value", cxxopts::value<std::string>())
("6,six", "a short number option")
("p, space", "an option with space between short and long")
("nothing", "won't exist", cxxopts::value<std::string>())
;
Argv argv({
@ -92,6 +93,8 @@ TEST_CASE("Basic options", "[options]")
CHECK(arguments[1].key() == "short");
CHECK(arguments[2].key() == "value");
CHECK(arguments[3].key() == "av");
CHECK_THROWS_AS(result["nothing"].as<std::string>(), std::domain_error);
}
TEST_CASE("Short options", "[options]")