diff options
author | Carl Dong <contact@carldong.me> | 2021-10-24 02:26:19 -0400 |
---|---|---|
committer | Carl Dong <contact@carldong.me> | 2022-06-22 18:18:52 -0400 |
commit | fc02f77ca604f0221171bfde3059b34f5d0fb1cd (patch) | |
tree | e1afcf97d71a2e0508803874f3490cab7e13de14 /src/util/system.cpp | |
parent | 57a491bee17af88f75c2cea8c109d93b1cdbc9a8 (diff) |
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.
Diffstat (limited to 'src/util/system.cpp')
-rw-r--r-- | src/util/system.cpp | 52 |
1 files changed, 46 insertions, 6 deletions
diff --git a/src/util/system.cpp b/src/util/system.cpp index 6e2638a5d6..e83ad11e80 100644 --- a/src/util/system.cpp +++ b/src/util/system.cpp @@ -613,35 +613,75 @@ bool ArgsManager::IsArgNegated(const std::string& strArg) const std::string ArgsManager::GetArg(const std::string& strArg, const std::string& strDefault) const { + return GetArg(strArg).value_or(strDefault); +} + +std::optional<std::string> ArgsManager::GetArg(const std::string& strArg) const +{ const util::SettingsValue value = GetSetting(strArg); - return SettingToString(value, strDefault); + return SettingToString(value); +} + +std::optional<std::string> SettingToString(const util::SettingsValue& value) +{ + if (value.isNull()) return std::nullopt; + if (value.isFalse()) return "0"; + if (value.isTrue()) return "1"; + if (value.isNum()) return value.getValStr(); + return value.get_str(); } std::string SettingToString(const util::SettingsValue& value, const std::string& strDefault) { - return value.isNull() ? strDefault : value.isFalse() ? "0" : value.isTrue() ? "1" : value.isNum() ? value.getValStr() : value.get_str(); + return SettingToString(value).value_or(strDefault); } int64_t ArgsManager::GetIntArg(const std::string& strArg, int64_t nDefault) const { + return GetIntArg(strArg).value_or(nDefault); +} + +std::optional<int64_t> ArgsManager::GetIntArg(const std::string& strArg) const +{ const util::SettingsValue value = GetSetting(strArg); - return SettingToInt(value, nDefault); + return SettingToInt(value); +} + +std::optional<int64_t> SettingToInt(const util::SettingsValue& value) +{ + if (value.isNull()) return std::nullopt; + if (value.isFalse()) return 0; + if (value.isTrue()) return 1; + if (value.isNum()) return value.getInt<int64_t>(); + return LocaleIndependentAtoi<int64_t>(value.get_str()); } int64_t SettingToInt(const util::SettingsValue& value, int64_t nDefault) { - return value.isNull() ? nDefault : value.isFalse() ? 0 : value.isTrue() ? 1 : value.isNum() ? value.getInt<int64_t>() : LocaleIndependentAtoi<int64_t>(value.get_str()); + return SettingToInt(value).value_or(nDefault); } bool ArgsManager::GetBoolArg(const std::string& strArg, bool fDefault) const { + return GetBoolArg(strArg).value_or(fDefault); +} + +std::optional<bool> ArgsManager::GetBoolArg(const std::string& strArg) const +{ const util::SettingsValue value = GetSetting(strArg); - return SettingToBool(value, fDefault); + return SettingToBool(value); +} + +std::optional<bool> SettingToBool(const util::SettingsValue& value) +{ + if (value.isNull()) return std::nullopt; + if (value.isBool()) return value.get_bool(); + return InterpretBool(value.get_str()); } bool SettingToBool(const util::SettingsValue& value, bool fDefault) { - return value.isNull() ? fDefault : value.isBool() ? value.get_bool() : InterpretBool(value.get_str()); + return SettingToBool(value).value_or(fDefault); } bool ArgsManager::SoftSetArg(const std::string& strArg, const std::string& strValue) |