Skip to content

Commit d4a4819

Browse files
committed
util/system: Add GetFixedPointArg helper
1 parent a612919 commit d4a4819

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

src/common/args.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,25 @@ int64_t SettingToInt(const common::SettingsValue& value, int64_t nDefault)
503503
return SettingToInt(value).value_or(nDefault);
504504
}
505505

506+
std::optional<int64_t> ArgsManager::GetFixedPointArg(const std::string& arg, int decimals) const
507+
{
508+
const common::SettingsValue value = GetSetting(arg);
509+
return SettingToFixedPoint(value, decimals);
510+
}
511+
512+
std::optional<int64_t> SettingToFixedPoint(const common::SettingsValue& value, int decimals)
513+
{
514+
if (value.isNull()) return std::nullopt;
515+
if (value.isFalse()) return 0;
516+
if (value.isTrue()) return 1;
517+
if (!value.isNum()) value.get_str(); // throws an exception if type is wrong
518+
int64_t v;
519+
if (!ParseFixedPoint(value.getValStr(), decimals, &v)) {
520+
throw std::runtime_error(strprintf("Parse error ('%s')", value.getValStr()));
521+
}
522+
return v;
523+
}
524+
506525
bool ArgsManager::GetBoolArg(const std::string& strArg, bool fDefault) const
507526
{
508527
return GetBoolArg(strArg).value_or(fDefault);

src/common/args.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ std::optional<std::string> SettingToString(const common::SettingsValue&);
9090
int64_t SettingToInt(const common::SettingsValue&, int64_t);
9191
std::optional<int64_t> SettingToInt(const common::SettingsValue&);
9292

93+
std::optional<int64_t> SettingToFixedPoint(const common::SettingsValue&, int decimals);
94+
9395
bool SettingToBool(const common::SettingsValue&, bool);
9496
std::optional<bool> SettingToBool(const common::SettingsValue&);
9597

@@ -293,6 +295,15 @@ class ArgsManager
293295
int64_t GetIntArg(const std::string& strArg, int64_t nDefault) const;
294296
std::optional<int64_t> GetIntArg(const std::string& strArg) const;
295297

298+
/**
299+
* Return fixed-point argument
300+
*
301+
* @param arg Argument to get (e.g. "-foo")
302+
* @param decimals Number of fractional decimal digits to accept
303+
* @return Command-line argument (0 if invalid number) multiplied by 10**decimals
304+
*/
305+
std::optional<int64_t> GetFixedPointArg(const std::string& arg, int decimals) const;
306+
296307
/**
297308
* Return boolean argument or default value
298309
*

test/lint/check-doc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
FOLDER_GREP = 'src'
1717
FOLDER_TEST = 'src/test/'
18-
REGEX_ARG = r'\b(?:GetArg|GetArgs|GetBoolArg|GetIntArg|GetPathArg|IsArgSet|get_net)\("(-[^"]+)"'
18+
REGEX_ARG = r'\b(?:GetArg|GetArgs|GetBoolArg|GetIntArg|GetFixedPointArg|GetPathArg|IsArgSet|get_net)\("(-[^"]+)"'
1919
REGEX_DOC = r'AddArg\("(-[^"=]+?)(?:=|")'
2020
CMD_ROOT_DIR = '$(git rev-parse --show-toplevel)/{}'.format(FOLDER_GREP)
2121
CMD_GREP_ARGS = r"git grep --perl-regexp '{}' -- {} ':(exclude){}'".format(REGEX_ARG, CMD_ROOT_DIR, FOLDER_TEST)

0 commit comments

Comments
 (0)