Skip to content

Commit

Permalink
fix default args for extra types
Browse files Browse the repository at this point in the history
Many types were not storing their default arguments, and reset was
simply clearing them.  This was obviously not the right behavior.
  • Loading branch information
Taylor C. Richberger committed Jan 11, 2019
1 parent a82a9d6 commit 2eb0f5f
Showing 1 changed file with 44 additions and 12 deletions.
56 changes: 44 additions & 12 deletions args.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -3414,6 +3414,7 @@ namespace args
protected:

List<T> values;
const List<T> defaultValues;
Nargs nargs;
Reader reader;

Expand All @@ -3434,7 +3435,7 @@ namespace args
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;

NargsValueFlag(Group &group_, const std::string &name_, const std::string &help_, Matcher &&matcher_, Nargs nargs_, const List<T> &defaultValues_ = {}, Options options_ = {})
: FlagBase(name_, help_, std::move(matcher_), options_), values(defaultValues_), nargs(nargs_)
: FlagBase(name_, help_, std::move(matcher_), options_), values(defaultValues_), defaultValues(defaultValues_),nargs(nargs_)
{
group_.Add(*this);
}
Expand Down Expand Up @@ -3499,6 +3500,12 @@ namespace args
{
return values.cend();
}

virtual void Reset() noexcept override
{
FlagBase::Reset();
values = defaultValues;
}
};

/** An argument-accepting flag class that pushes the found values into a list
Expand All @@ -3516,6 +3523,7 @@ namespace args
private:
using Container = List<T>;
Container values;
const Container defaultValues;
Reader reader;

public:
Expand All @@ -3534,7 +3542,7 @@ namespace args
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;

ValueFlagList(Group &group_, const std::string &name_, const std::string &help_, Matcher &&matcher_, const Container &defaultValues_ = Container(), Options options_ = {}):
ValueFlagBase(name_, help_, std::move(matcher_), options_), values(defaultValues_)
ValueFlagBase(name_, help_, std::move(matcher_), options_), values(defaultValues_), defaultValues(defaultValues_)
{
group_.Add(*this);
}
Expand Down Expand Up @@ -3572,7 +3580,7 @@ namespace args
virtual void Reset() noexcept override
{
ValueFlagBase::Reset();
values.clear();
values = defaultValues;
}

iterator begin() noexcept
Expand Down Expand Up @@ -3623,6 +3631,7 @@ namespace args
private:
const Map<K, T> map;
T value;
const T defaultValue;
Reader reader;

protected:
Expand All @@ -3633,7 +3642,7 @@ namespace args

public:

MapFlag(Group &group_, const std::string &name_, const std::string &help_, Matcher &&matcher_, const Map<K, T> &map_, const T &defaultValue_, Options options_): ValueFlagBase(name_, help_, std::move(matcher_), options_), map(map_), value(defaultValue_)
MapFlag(Group &group_, const std::string &name_, const std::string &help_, Matcher &&matcher_, const Map<K, T> &map_, const T &defaultValue_, Options options_): ValueFlagBase(name_, help_, std::move(matcher_), options_), map(map_), value(defaultValue_), defaultValue(defaultValue_)
{
group_.Add(*this);
}
Expand Down Expand Up @@ -3684,6 +3693,12 @@ namespace args
{
return value;
}

virtual void Reset() noexcept override
{
ValueFlagBase::Reset();
value = defaultValue;
}
};

/** A mapping value flag list class
Expand All @@ -3706,6 +3721,7 @@ namespace args
using Container = List<T>;
const Map<K, T> map;
Container values;
const Container defaultValues;
Reader reader;

protected:
Expand All @@ -3728,7 +3744,7 @@ namespace args
typedef std::reverse_iterator<iterator> reverse_iterator;
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;

MapFlagList(Group &group_, const std::string &name_, const std::string &help_, Matcher &&matcher_, const Map<K, T> &map_, const Container &defaultValues_ = Container()): ValueFlagBase(name_, help_, std::move(matcher_)), map(map_), values(defaultValues_)
MapFlagList(Group &group_, const std::string &name_, const std::string &help_, Matcher &&matcher_, const Map<K, T> &map_, const Container &defaultValues_ = Container()): ValueFlagBase(name_, help_, std::move(matcher_)), map(map_), values(defaultValues_), defaultValues(defaultValues_)
{
group_.Add(*this);
}
Expand Down Expand Up @@ -3780,7 +3796,7 @@ namespace args
virtual void Reset() noexcept override
{
ValueFlagBase::Reset();
values.clear();
values = defaultValues;
}

iterator begin() noexcept
Expand Down Expand Up @@ -3826,9 +3842,10 @@ namespace args
{
private:
T value;
const T defaultValue;
Reader reader;
public:
Positional(Group &group_, const std::string &name_, const std::string &help_, const T &defaultValue_ = T(), Options options_ = {}): PositionalBase(name_, help_, options_), value(defaultValue_)
Positional(Group &group_, const std::string &name_, const std::string &help_, const T &defaultValue_ = T(), Options options_ = {}): PositionalBase(name_, help_, options_), value(defaultValue_), defaultValue(defaultValue_)
{
group_.Add(*this);
}
Expand Down Expand Up @@ -3859,6 +3876,12 @@ namespace args
{
return value;
}

virtual void Reset() noexcept override
{
PositionalBase::Reset();
value = defaultValue;
}
};

/** A positional argument class that pushes the found values into a list
Expand All @@ -3876,6 +3899,7 @@ namespace args
private:
using Container = List<T>;
Container values;
const Container defaultValues;
Reader reader;

public:
Expand All @@ -3892,7 +3916,7 @@ namespace args
typedef std::reverse_iterator<iterator> reverse_iterator;
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;

PositionalList(Group &group_, const std::string &name_, const std::string &help_, const Container &defaultValues_ = Container(), Options options_ = {}): PositionalBase(name_, help_, options_), values(defaultValues_)
PositionalList(Group &group_, const std::string &name_, const std::string &help_, const Container &defaultValues_ = Container(), Options options_ = {}): PositionalBase(name_, help_, options_), values(defaultValues_), defaultValues(defaultValues_)
{
group_.Add(*this);
}
Expand Down Expand Up @@ -3933,7 +3957,7 @@ namespace args
virtual void Reset() noexcept override
{
PositionalBase::Reset();
values.clear();
values = defaultValues;
}

iterator begin() noexcept
Expand Down Expand Up @@ -3984,6 +4008,7 @@ namespace args
private:
const Map<K, T> map;
T value;
const T defaultValue;
Reader reader;

protected:
Expand All @@ -3995,7 +4020,7 @@ namespace args
public:

MapPositional(Group &group_, const std::string &name_, const std::string &help_, const Map<K, T> &map_, const T &defaultValue_ = T(), Options options_ = {}):
PositionalBase(name_, help_, options_), map(map_), value(defaultValue_)
PositionalBase(name_, help_, options_), map(map_), value(defaultValue_), defaultValue(defaultValue_)
{
group_.Add(*this);
}
Expand Down Expand Up @@ -4038,6 +4063,12 @@ namespace args
{
return value;
}

virtual void Reset() noexcept override
{
PositionalBase::Reset();
value = defaultValue;
}
};

/** A positional argument mapping list class
Expand All @@ -4061,6 +4092,7 @@ namespace args

const Map<K, T> map;
Container values;
const Container defaultValues;
Reader reader;

protected:
Expand All @@ -4084,7 +4116,7 @@ namespace args
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;

MapPositionalList(Group &group_, const std::string &name_, const std::string &help_, const Map<K, T> &map_, const Container &defaultValues_ = Container(), Options options_ = {}):
PositionalBase(name_, help_, options_), map(map_), values(defaultValues_)
PositionalBase(name_, help_, options_), map(map_), values(defaultValues_), defaultValues(defaultValues_)
{
group_.Add(*this);
}
Expand Down Expand Up @@ -4135,7 +4167,7 @@ namespace args
virtual void Reset() noexcept override
{
PositionalBase::Reset();
values.clear();
values = defaultValues;
}

iterator begin() noexcept
Expand Down

0 comments on commit 2eb0f5f

Please sign in to comment.