Commit d74a2c65a54f14c632637bb577dce1aeadcaf9e3

Authored by Jarryd Beck
1 parent f0406578

Add ability to iterate through parsed options

Fixes #74.
include/cxxopts.hpp
... ... @@ -980,6 +980,42 @@ namespace cxxopts
980 980 size_t m_count = 0;
981 981 };
982 982  
  983 + class KeyValue
  984 + {
  985 + public:
  986 + KeyValue(std::string key, std::string value)
  987 + : m_key(std::move(key))
  988 + , m_value(std::move(value))
  989 + {
  990 + }
  991 +
  992 + const
  993 + std::string&
  994 + key() const
  995 + {
  996 + return m_key;
  997 + }
  998 +
  999 + const std::string
  1000 + value() const
  1001 + {
  1002 + return m_value;
  1003 + }
  1004 +
  1005 + template <typename T>
  1006 + T
  1007 + as() const
  1008 + {
  1009 + T result;
  1010 + values::parse_value(m_value, result);
  1011 + return result;
  1012 + }
  1013 +
  1014 + private:
  1015 + std::string m_key;
  1016 + std::string m_value;
  1017 + };
  1018 +
983 1019 class ParseResult
984 1020 {
985 1021 public:
... ... @@ -1018,6 +1054,12 @@ namespace cxxopts
1018 1054 return riter->second;
1019 1055 }
1020 1056  
  1057 + const std::vector<KeyValue>&
  1058 + arguments() const
  1059 + {
  1060 + return m_sequential;
  1061 + }
  1062 +
1021 1063 private:
1022 1064  
1023 1065 OptionValue&
... ... @@ -1059,6 +1101,8 @@ namespace cxxopts
1059 1101 std::vector<std::string>::iterator m_next_positional;
1060 1102 std::unordered_set<std::string> m_positional_set;
1061 1103 std::unordered_map<std::shared_ptr<OptionDetails>, OptionValue> m_results;
  1104 +
  1105 + std::vector<KeyValue> m_sequential;
1062 1106 };
1063 1107  
1064 1108 class Options
... ... @@ -1386,6 +1430,8 @@ ParseResult::parse_option
1386 1430 {
1387 1431 auto& result = m_results[value];
1388 1432 result.parse(value, arg);
  1433 +
  1434 + m_sequential.emplace_back(value->long_name(), arg);
1389 1435 }
1390 1436  
1391 1437 inline
... ...
test/options.cpp
... ... @@ -82,6 +82,16 @@ TEST_CASE(&quot;Basic options&quot;, &quot;[options]&quot;)
82 82 CHECK(result.count("6") == 1);
83 83 CHECK(result.count("p") == 2);
84 84 CHECK(result.count("space") == 2);
  85 +
  86 + auto& arguments = result.arguments();
  87 + REQUIRE(arguments.size() == 7);
  88 + CHECK(arguments[0].key() == "long");
  89 + CHECK(arguments[0].value() == "");
  90 + CHECK(arguments[0].as<bool>() == true);
  91 +
  92 + CHECK(arguments[1].key() == "short");
  93 + CHECK(arguments[2].key() == "value");
  94 + CHECK(arguments[3].key() == "av");
85 95 }
86 96  
87 97 TEST_CASE("Short options", "[options]")
... ...