Skip to content

Commit fc02f77

Browse files
committed
ArgsMan: Add Get*Arg functions returning optional
This allows the caller to not provide a default at all and just check inside the optional to see if the arg was set or not.
1 parent 57a491b commit fc02f77

File tree

2 files changed

+54
-6
lines changed

2 files changed

+54
-6
lines changed

src/util/system.cpp

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -612,36 +612,76 @@ bool ArgsManager::IsArgNegated(const std::string& strArg) const
612612
}
613613

614614
std::string ArgsManager::GetArg(const std::string& strArg, const std::string& strDefault) const
615+
{
616+
return GetArg(strArg).value_or(strDefault);
617+
}
618+
619+
std::optional<std::string> ArgsManager::GetArg(const std::string& strArg) const
615620
{
616621
const util::SettingsValue value = GetSetting(strArg);
617-
return SettingToString(value, strDefault);
622+
return SettingToString(value);
623+
}
624+
625+
std::optional<std::string> SettingToString(const util::SettingsValue& value)
626+
{
627+
if (value.isNull()) return std::nullopt;
628+
if (value.isFalse()) return "0";
629+
if (value.isTrue()) return "1";
630+
if (value.isNum()) return value.getValStr();
631+
return value.get_str();
618632
}
619633

620634
std::string SettingToString(const util::SettingsValue& value, const std::string& strDefault)
621635
{
622-
return value.isNull() ? strDefault : value.isFalse() ? "0" : value.isTrue() ? "1" : value.isNum() ? value.getValStr() : value.get_str();
636+
return SettingToString(value).value_or(strDefault);
623637
}
624638

625639
int64_t ArgsManager::GetIntArg(const std::string& strArg, int64_t nDefault) const
640+
{
641+
return GetIntArg(strArg).value_or(nDefault);
642+
}
643+
644+
std::optional<int64_t> ArgsManager::GetIntArg(const std::string& strArg) const
626645
{
627646
const util::SettingsValue value = GetSetting(strArg);
628-
return SettingToInt(value, nDefault);
647+
return SettingToInt(value);
648+
}
649+
650+
std::optional<int64_t> SettingToInt(const util::SettingsValue& value)
651+
{
652+
if (value.isNull()) return std::nullopt;
653+
if (value.isFalse()) return 0;
654+
if (value.isTrue()) return 1;
655+
if (value.isNum()) return value.getInt<int64_t>();
656+
return LocaleIndependentAtoi<int64_t>(value.get_str());
629657
}
630658

631659
int64_t SettingToInt(const util::SettingsValue& value, int64_t nDefault)
632660
{
633-
return value.isNull() ? nDefault : value.isFalse() ? 0 : value.isTrue() ? 1 : value.isNum() ? value.getInt<int64_t>() : LocaleIndependentAtoi<int64_t>(value.get_str());
661+
return SettingToInt(value).value_or(nDefault);
634662
}
635663

636664
bool ArgsManager::GetBoolArg(const std::string& strArg, bool fDefault) const
665+
{
666+
return GetBoolArg(strArg).value_or(fDefault);
667+
}
668+
669+
std::optional<bool> ArgsManager::GetBoolArg(const std::string& strArg) const
637670
{
638671
const util::SettingsValue value = GetSetting(strArg);
639-
return SettingToBool(value, fDefault);
672+
return SettingToBool(value);
673+
}
674+
675+
std::optional<bool> SettingToBool(const util::SettingsValue& value)
676+
{
677+
if (value.isNull()) return std::nullopt;
678+
if (value.isBool()) return value.get_bool();
679+
return InterpretBool(value.get_str());
640680
}
641681

642682
bool SettingToBool(const util::SettingsValue& value, bool fDefault)
643683
{
644-
return value.isNull() ? fDefault : value.isBool() ? value.get_bool() : InterpretBool(value.get_str());
684+
return SettingToBool(value).value_or(fDefault);
645685
}
646686

647687
bool ArgsManager::SoftSetArg(const std::string& strArg, const std::string& strValue)

src/util/system.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,13 @@ struct SectionInfo
161161
};
162162

163163
std::string SettingToString(const util::SettingsValue&, const std::string&);
164+
std::optional<std::string> SettingToString(const util::SettingsValue&);
165+
164166
int64_t SettingToInt(const util::SettingsValue&, int64_t);
167+
std::optional<int64_t> SettingToInt(const util::SettingsValue&);
168+
165169
bool SettingToBool(const util::SettingsValue&, bool);
170+
std::optional<bool> SettingToBool(const util::SettingsValue&);
166171

167172
class ArgsManager
168173
{
@@ -335,6 +340,7 @@ class ArgsManager
335340
* @return command-line argument or default value
336341
*/
337342
std::string GetArg(const std::string& strArg, const std::string& strDefault) const;
343+
std::optional<std::string> GetArg(const std::string& strArg) const;
338344

339345
/**
340346
* Return path argument or default value
@@ -356,6 +362,7 @@ class ArgsManager
356362
* @return command-line argument (0 if invalid number) or default value
357363
*/
358364
int64_t GetIntArg(const std::string& strArg, int64_t nDefault) const;
365+
std::optional<int64_t> GetIntArg(const std::string& strArg) const;
359366

360367
/**
361368
* Return boolean argument or default value
@@ -365,6 +372,7 @@ class ArgsManager
365372
* @return command-line argument or default value
366373
*/
367374
bool GetBoolArg(const std::string& strArg, bool fDefault) const;
375+
std::optional<bool> GetBoolArg(const std::string& strArg) const;
368376

369377
/**
370378
* Set an argument if it doesn't already have a value

0 commit comments

Comments
 (0)