core: command-line: clarify UserItemParse args

This commit is contained in:
Peter D. Barnes, Jr
2022-10-21 12:01:36 -07:00
committed by Gabriel Ferreira
parent dc2e494e33
commit 68557ec32a
2 changed files with 21 additions and 21 deletions

View File

@@ -858,12 +858,12 @@ CommandLineHelper::GetDefault<bool>(const bool& val)
template <>
bool
CommandLineHelper::UserItemParse<bool>(const std::string& value, bool& val)
CommandLineHelper::UserItemParse<bool>(const std::string& value, bool& dest)
{
// No new value, so just toggle it
if (value.empty())
{
val = !val;
dest = !dest;
return true;
}
@@ -873,19 +873,19 @@ CommandLineHelper::UserItemParse<bool>(const std::string& value, bool& val)
});
if (src == "true" || src == "t")
{
val = true;
dest = true;
return true;
}
else if (src == "false" || src == "f")
{
val = false;
dest = false;
return true;
}
else
{
std::istringstream iss;
iss.str(src);
iss >> val;
iss >> dest;
return !iss.bad() && !iss.fail();
}
}
@@ -901,32 +901,32 @@ CommandLineHelper::GetDefault<Time>(const Time& val)
template <>
bool
CommandLineHelper::UserItemParse<uint8_t>(const std::string& value, uint8_t& val)
CommandLineHelper::UserItemParse<uint8_t>(const std::string& value, uint8_t& dest)
{
uint8_t oldVal = val;
int newVal;
uint8_t oldDest = dest;
int newDest;
try
{
newVal = std::stoi(value);
newDest = std::stoi(value);
}
catch (std::invalid_argument& ia)
{
NS_LOG_WARN("invalid argument: " << ia.what());
val = oldVal;
dest = oldDest;
return false;
}
catch (std::out_of_range& oor)
{
NS_LOG_WARN("out of range: " << oor.what());
val = oldVal;
dest = oldDest;
return false;
}
if (newVal < 0 || newVal > 255)
if (newDest < 0 || newDest > 255)
{
return false;
}
val = newVal;
dest = newDest;
return true;
}

View File

@@ -642,31 +642,31 @@ namespace CommandLineHelper
* \brief Helpers to specialize CommandLine::UserItem::Parse()
*
* \param [in] value The argument name
* \param [out] val The argument location
* \param [out] dest The argument location
* \tparam T \deduced The type being specialized
* \return \c true if parsing was successful
*/
template <typename T>
bool UserItemParse(const std::string& value, T& val);
bool UserItemParse(const std::string& value, T& dest);
/**
* \brief Specialization of CommandLine::UserItem::Parse() to \c bool
*
* \param [in] value The argument name
* \param [out] val The boolean variable to set
* \param [out] dest The boolean variable to set
* \return \c true if parsing was successful
*/
template <>
bool UserItemParse<bool>(const std::string& value, bool& val);
bool UserItemParse<bool>(const std::string& value, bool& dest);
/**
* \brief Specialization of CommandLine::UserItem::Parse() to \c uint8_t
* to distinguish from \c char
*
* \param [in] value The argument name
* \param [out] val The \c uint8_t variable to set
* \param [out] dest The \c uint8_t variable to set
* \return \c true if parsing was successful
*/
template <>
bool UserItemParse<uint8_t>(const std::string& value, uint8_t& val);
bool UserItemParse<uint8_t>(const std::string& value, uint8_t& dest);
/**
* \ingroup commandlinehelper
@@ -760,11 +760,11 @@ CommandLine::UserItem<T>::Parse(const std::string& value) const
template <typename T>
bool
CommandLineHelper::UserItemParse(const std::string& value, T& val)
CommandLineHelper::UserItemParse(const std::string& value, T& dest)
{
std::istringstream iss;
iss.str(value);
iss >> val;
iss >> dest;
return !iss.bad() && !iss.fail();
}