Commit e31c9af607cc7b67672d7d184fb318e641cdc5b0

Authored by Jarryd Beck
1 parent 1d6d1c97

add clone to options

This is broken at the moment; it needs a complete rewrite.

The problem is that multiple options point to the same `OptionDetails`.
I really need to separate parsing results from describing options.
Showing 1 changed file with 46 additions and 2 deletions
include/cxxopts.hpp
@@ -256,6 +256,10 @@ namespace cxxopts @@ -256,6 +256,10 @@ namespace cxxopts
256 { 256 {
257 public: 257 public:
258 258
  259 + virtual
  260 + std::shared_ptr<Value>
  261 + clone() const = 0;
  262 +
259 virtual void 263 virtual void
260 parse(const std::string& text) const = 0; 264 parse(const std::string& text) const = 0;
261 265
@@ -694,6 +698,8 @@ namespace cxxopts @@ -694,6 +698,8 @@ namespace cxxopts
694 template <typename T> 698 template <typename T>
695 class standard_value : public Value 699 class standard_value : public Value
696 { 700 {
  701 + using Self = standard_value<T>;
  702 +
697 public: 703 public:
698 standard_value() 704 standard_value()
699 : m_result(std::make_shared<T>()) 705 : m_result(std::make_shared<T>())
@@ -706,6 +712,28 @@ namespace cxxopts @@ -706,6 +712,28 @@ namespace cxxopts
706 { 712 {
707 } 713 }
708 714
  715 + std::shared_ptr<Value>
  716 + clone() const
  717 + {
  718 + std::shared_ptr<Self> copy;
  719 +
  720 + if (m_result)
  721 + {
  722 + copy = std::make_shared<Self>(m_store);
  723 + }
  724 + else
  725 + {
  726 + copy = std::make_shared<Self>();
  727 + }
  728 +
  729 + copy->m_default = m_default;
  730 + copy->m_implicit = m_implicit;
  731 + copy->m_default_value = m_default_value;
  732 + copy->m_implicit_value = m_implicit_value;
  733 +
  734 + return copy;
  735 + }
  736 +
709 void 737 void
710 parse(const std::string& text) const 738 parse(const std::string& text) const
711 { 739 {
@@ -825,6 +853,15 @@ namespace cxxopts @@ -825,6 +853,15 @@ namespace cxxopts
825 { 853 {
826 } 854 }
827 855
  856 + OptionDetails(const OptionDetails& rhs)
  857 + : m_desc(rhs.m_desc)
  858 + , m_count(rhs.m_count)
  859 + {
  860 + m_value = rhs.m_value->clone();
  861 + }
  862 +
  863 + OptionDetails(OptionDetails&& rhs) = default;
  864 +
828 const String& 865 const String&
829 description() const 866 description() const
830 { 867 {
@@ -1202,10 +1239,17 @@ ParseResult::ParseResult(Iterator begin, Iterator end, @@ -1202,10 +1239,17 @@ ParseResult::ParseResult(Iterator begin, Iterator end,
1202 std::vector<std::string> positional, 1239 std::vector<std::string> positional,
1203 int& argc, char**& argv 1240 int& argc, char**& argv
1204 ) 1241 )
1205 -: m_options(begin, end)  
1206 -, m_positional(std::move(positional)) 1242 +: m_positional(std::move(positional))
1207 , m_next_positional(m_positional.begin()) 1243 , m_next_positional(m_positional.begin())
1208 { 1244 {
  1245 + for (auto current = begin; current != end; ++current)
  1246 + {
  1247 + m_options.insert({
  1248 + current->first,
  1249 + std::make_shared<OptionDetails>(*current->second)
  1250 + });
  1251 + }
  1252 +
1209 parse(argc, argv); 1253 parse(argc, argv);
1210 } 1254 }
1211 1255