Add Argument::firstValueOr() for conveniently accessing the first value with a fallback

This commit is contained in:
Martchus 2021-01-01 18:30:17 +01:00
parent 8a9d72bee9
commit f109d36ad2
2 changed files with 13 additions and 0 deletions

View File

@ -499,6 +499,18 @@ const char *Argument::firstValue() const
} }
} }
/*!
* \brief Returns the first value like Argument::firstValue() but returns \a fallback instead of nullptr if there's no value.
*/
const char *Argument::firstValueOr(const char *fallback) const
{
if (const auto *const v = firstValue()) {
return v;
} else {
return fallback;
}
}
/*! /*!
* \brief Writes the name, the abbreviation and other information about the Argument to the give ostream. * \brief Writes the name, the abbreviation and other information about the Argument to the give ostream.
*/ */

View File

@ -335,6 +335,7 @@ public:
template <typename... TargetType> std::vector<std::tuple<TargetType...>> allValuesAs() const; template <typename... TargetType> std::vector<std::tuple<TargetType...>> allValuesAs() const;
const char *firstValue() const; const char *firstValue() const;
const char *firstValueOr(const char *fallback) const;
bool allRequiredValuesPresent(std::size_t occurrence = 0) const; bool allRequiredValuesPresent(std::size_t occurrence = 0) const;
bool isPresent() const; bool isPresent() const;
std::size_t occurrences() const; std::size_t occurrences() const;