all tests pass

This commit is contained in:
Jarryd Beck 2017-10-27 08:38:29 +11:00
parent 3fed557cf2
commit acbce8e363
2 changed files with 38 additions and 5 deletions

View File

@ -719,11 +719,11 @@ namespace cxxopts
if (m_result) if (m_result)
{ {
copy = std::make_shared<Self>(m_store); copy = std::make_shared<Self>();
} }
else else
{ {
copy = std::make_shared<Self>(); copy = std::make_shared<Self>(m_store);
} }
copy->m_default = m_default; copy->m_default = m_default;
@ -969,6 +969,17 @@ namespace cxxopts
return m_count; return m_count;
} }
template <typename T>
const T&
as() const
{
#ifdef CXXOPTS_NO_RTTI
return static_cast<const values::standard_value<T>&>(*m_value).get();
#else
return dynamic_cast<const values::standard_value<T>&>(*m_value).get();
#endif
}
private: private:
void void
ensure_value(std::shared_ptr<const OptionDetails> details) ensure_value(std::shared_ptr<const OptionDetails> details)
@ -1006,7 +1017,7 @@ namespace cxxopts
return riter->second.count(); return riter->second.count();
} }
const OptionDetails& const OptionValue&
operator[](const std::string& option) const operator[](const std::string& option) const
{ {
auto iter = m_options.find(option); auto iter = m_options.find(option);
@ -1016,7 +1027,9 @@ namespace cxxopts
throw option_not_present_exception(option); throw option_not_present_exception(option);
} }
return *iter->second; auto riter = m_results.find(iter->second);
return riter->second;
} }
private: private:
@ -1448,9 +1461,10 @@ ParseResult::consume_positional(std::string a)
auto iter = m_options.find(*m_next_positional); auto iter = m_options.find(*m_next_positional);
if (iter != m_options.end()) if (iter != m_options.end())
{ {
auto& result = m_results[iter->second];
if (!iter->second->value().is_container()) if (!iter->second->value().is_container())
{ {
if (iter->second->count() == 0) if (result.count() == 0)
{ {
add_to_option(*m_next_positional, a); add_to_option(*m_next_positional, a);
++m_next_positional; ++m_next_positional;

View File

@ -219,6 +219,24 @@ TEST_CASE("Empty with implicit value", "[implicit]")
REQUIRE(result["implicit"].as<std::string>() == ""); REQUIRE(result["implicit"].as<std::string>() == "");
} }
TEST_CASE("Parse into a reference", "[reference]")
{
int value = 0;
cxxopts::Options options("into_reference", "parses into a reference");
options.add_options()
("ref", "A reference", cxxopts::value(value));
Argv av({"into_reference", "--ref", "42"});
auto argv = av.argv();
auto argc = av.argc();
auto result = options.parse(argc, argv);
CHECK(result.count("ref") == 1);
CHECK(value == 42);
}
TEST_CASE("Integers", "[options]") TEST_CASE("Integers", "[options]")
{ {
cxxopts::Options options("parses_integers", "parses integers correctly"); cxxopts::Options options("parses_integers", "parses integers correctly");
@ -236,6 +254,7 @@ TEST_CASE("Integers", "[options]")
REQUIRE(result.count("positional") == 6); REQUIRE(result.count("positional") == 6);
auto& positional = result["positional"].as<std::vector<int>>(); auto& positional = result["positional"].as<std::vector<int>>();
REQUIRE(positional.size() == 6);
CHECK(positional[0] == 5); CHECK(positional[0] == 5);
CHECK(positional[1] == 6); CHECK(positional[1] == 6);
CHECK(positional[2] == -6); CHECK(positional[2] == -6);