From be5b48c27f1e0c1d26166e1c9b603479f39e53e4 Mon Sep 17 00:00:00 2001 From: Luke Dashjr Date: Mon, 4 Sep 2023 22:49:11 +0000 Subject: [PATCH] util/system: Add GetFixedPointArg helper --- src/common/args.cpp | 19 +++++++++++++++++++ src/common/args.h | 11 +++++++++++ test/lint/check-doc.py | 2 +- 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/common/args.cpp b/src/common/args.cpp index a9108e5916..9c215903e4 100644 --- a/src/common/args.cpp +++ b/src/common/args.cpp @@ -502,6 +502,25 @@ int64_t SettingToInt(const common::SettingsValue& value, int64_t nDefault) return SettingToInt(value).value_or(nDefault); } +std::optional ArgsManager::GetFixedPointArg(const std::string& arg, int decimals) const +{ + const common::SettingsValue value = GetSetting(arg); + return SettingToFixedPoint(value, decimals); +} + +std::optional SettingToFixedPoint(const common::SettingsValue& value, int decimals) +{ + if (value.isNull()) return std::nullopt; + if (value.isFalse()) return 0; + if (value.isTrue()) return 1; + if (!value.isNum()) value.get_str(); // throws an exception if type is wrong + int64_t v; + if (!ParseFixedPoint(value.getValStr(), decimals, &v)) { + throw std::runtime_error(strprintf("Parse error ('%s')", value.getValStr())); + } + return v; +} + bool ArgsManager::GetBoolArg(const std::string& strArg, bool fDefault) const { return GetBoolArg(strArg).value_or(fDefault); diff --git a/src/common/args.h b/src/common/args.h index 6451b194d1..05d59263e2 100644 --- a/src/common/args.h +++ b/src/common/args.h @@ -90,6 +90,8 @@ std::optional SettingToString(const common::SettingsValue&); int64_t SettingToInt(const common::SettingsValue&, int64_t); std::optional SettingToInt(const common::SettingsValue&); +std::optional SettingToFixedPoint(const common::SettingsValue&, int decimals); + bool SettingToBool(const common::SettingsValue&, bool); std::optional SettingToBool(const common::SettingsValue&); @@ -293,6 +295,15 @@ protected: int64_t GetIntArg(const std::string& strArg, int64_t nDefault) const; std::optional GetIntArg(const std::string& strArg) const; + /** + * Return fixed-point argument + * + * @param arg Argument to get (e.g. "-foo") + * @param decimals Number of fractional decimal digits to accept + * @return Command-line argument (0 if invalid number) multiplied by 10**decimals + */ + std::optional GetFixedPointArg(const std::string& arg, int decimals) const; + /** * Return boolean argument or default value * diff --git a/test/lint/check-doc.py b/test/lint/check-doc.py index f55d0f8cb7..8696ca8e0e 100755 --- a/test/lint/check-doc.py +++ b/test/lint/check-doc.py @@ -15,7 +15,7 @@ import re FOLDER_GREP = 'src' FOLDER_TEST = 'src/test/' -REGEX_ARG = r'\b(?:GetArg|GetArgs|GetBoolArg|GetIntArg|GetPathArg|IsArgSet|get_net)\("(-[^"]+)"' +REGEX_ARG = r'\b(?:GetArg|GetArgs|GetBoolArg|GetIntArg|GetFixedPointArg|GetPathArg|IsArgSet|get_net)\("(-[^"]+)"' REGEX_DOC = r'AddArg\("(-[^"=]+?)(?:=|")' CMD_ROOT_DIR = '$(git rev-parse --show-toplevel)/{}'.format(FOLDER_GREP) CMD_GREP_ARGS = r"git grep --perl-regexp '{}' -- {} ':(exclude){}'".format(REGEX_ARG, CMD_ROOT_DIR, FOLDER_TEST)