Skip to content

Commit

Permalink
Merge pull request #5 from pavelkryukov/develop
Browse files Browse the repository at this point in the history
Clear previous parse results before starting a new parse
  • Loading branch information
badaix committed May 13, 2018
2 parents 54a210d + a299bd8 commit 179c8dd
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions include/popl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ friend class OptionParser;

protected:
virtual void parse(const std::string& what_option, const char* value) = 0;
virtual void clear() = 0;
virtual std::string to_string() const;

std::string short_option_;
Expand Down Expand Up @@ -109,6 +110,7 @@ class ValueTemplate : public Option
protected:
virtual void update_reference();
virtual void add_value(const T& value);
virtual void clear() override;

unsigned int count_;
T* assign_to_;
Expand Down Expand Up @@ -185,6 +187,7 @@ class Switch : public ValueTemplate<bool>

protected:
void parse(const std::string& what_option, const char* value) override;
virtual void clear() override;
std::string to_string() const override;
};

Expand Down Expand Up @@ -364,6 +367,12 @@ inline void ValueTemplate<T>::set_value(const T& value)
add_value(value);
}

template<class T>
inline void ValueTemplate<T>::clear()
{
values_.clear();
count_ = 0;
}

template<class T>
inline T ValueTemplate<T>::value(size_t idx) const
Expand Down Expand Up @@ -563,16 +572,19 @@ inline std::string Implicit<T>::to_string() const
inline Switch::Switch(const std::string& short_option, const std::string& long_option, const std::string& description, bool* assign_to) :
ValueTemplate<bool>(short_option, long_option, description, assign_to)
{
if (assign_to != nullptr)
*assign_to = false;
}


inline void Switch::parse(const std::string& /*what_option*/, const char* /*value*/)
{
ValueTemplate<bool>::clear();
add_value(true);
}

inline void Switch::clear()
{
ValueTemplate<bool>::clear();
add_value(false);
}

inline Argument Switch::argument_type() const
{
Expand Down Expand Up @@ -696,6 +708,9 @@ inline void OptionParser::parse(int argc, const char * const * argv)
{
unknown_options_.clear();
non_option_args_.clear();
for (auto& opt : options_)
opt->clear();

for (int n=1; n<argc; ++n)
{
const std::string arg(argv[n]);
Expand Down

0 comments on commit 179c8dd

Please sign in to comment.