From 7fa61dd4d0e4352d0c78ae7ac7c4feee1140c20f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Tim=C3=B3n?= Date: Mon, 4 Apr 2016 15:41:25 +0200 Subject: [PATCH] Upstream? Globals: Allow to parse arguments without implicitly using the argMap global --- src/util.cpp | 47 +++++++++++++++++++++++++++++++++++++++++------ src/util.h | 21 +++++++++++++++++++++ 2 files changed, 62 insertions(+), 6 deletions(-) diff --git a/src/util.cpp b/src/util.cpp index 6207b8f0c4..555e29dfdd 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -13,6 +13,7 @@ #include "random.h" #include "serialize.h" #include "sync.h" +#include "utilmoneystr.h" #include "utilstrencodings.h" #include "utiltime.h" @@ -455,25 +456,53 @@ void ParseParameters(int argc, const char* const argv[]) std::string GetArg(const std::string& strArg, const std::string& strDefault) { - if (mapArgs.count(strArg)) - return mapArgs[strArg]; + return GetArg(strArg, strDefault, mapArgs); +} + +std::string GetArg(const std::string& strArg, const std::string& strDefault, const std::map& mapArgs) +{ + std::map::const_iterator it = mapArgs.find(strArg); + if (it != mapArgs.end()) + return it->second; return strDefault; } int64_t GetArg(const std::string& strArg, int64_t nDefault) { - if (mapArgs.count(strArg)) - return atoi64(mapArgs[strArg]); + return GetArg(strArg, nDefault, mapArgs); +} + +int64_t GetArg(const std::string& strArg, int64_t nDefault, const std::map& mapArgs) +{ + std::map::const_iterator it = mapArgs.find(strArg); + if (it != mapArgs.end()) + return atoi64(it->second); return nDefault; } bool GetBoolArg(const std::string& strArg, bool fDefault) { - if (mapArgs.count(strArg)) - return InterpretBool(mapArgs[strArg]); + return GetBoolArg(strArg, fDefault, mapArgs); +} + +bool GetBoolArg(const std::string& strArg, bool fDefault, const std::map& mapArgs) +{ + std::map::const_iterator it = mapArgs.find(strArg); + if (it != mapArgs.end()) + return InterpretBool(it->second); return fDefault; } +CAmount ParseAmountFromArgs(const std::string& strArg, CAmount nDefault, const std::map& mapArgs) +{ + CAmount n = nDefault; + std::string strAmount = GetArg(strArg, "", mapArgs); + if ("" != strAmount) + if (!ParseMoney(strAmount, n) || !MoneyRange(n)) + throw std::runtime_error(strprintf(_("Invalid amount for %s=: '%s'"), strArg, strAmount)); + return n; +} + bool SoftSetArg(const std::string& strArg, const std::string& strValue) { if (mapArgs.count(strArg)) @@ -505,6 +534,12 @@ std::string HelpMessageOpt(const std::string &option, const std::string &message std::string("\n\n"); } +void AppendMessagesOpt(std::string& strUsage, const std::vector >& optionsHelp) +{ + for (unsigned int i=0; i < optionsHelp.size(); i++) + strUsage += HelpMessageOpt(optionsHelp[i].first, optionsHelp[i].second); +} + static std::string FormatException(const std::exception* pex, const char* pszThread) { #ifdef WIN32 diff --git a/src/util.h b/src/util.h index c9cd4068cb..6dbed1181a 100644 --- a/src/util.h +++ b/src/util.h @@ -14,6 +14,7 @@ #include "config/bitcoin-config.h" #endif +#include "amount.h" #include "compat.h" #include "tinyformat.h" #include "utiltime.h" @@ -160,6 +161,7 @@ inline bool IsSwitchChar(char c) * @return command-line argument or default value */ std::string GetArg(const std::string& strArg, const std::string& strDefault); +std::string GetArg(const std::string& strArg, const std::string& strDefault, const std::map& mapArgs); /** * Return integer argument or default value @@ -169,6 +171,7 @@ std::string GetArg(const std::string& strArg, const std::string& strDefault); * @return command-line argument (0 if invalid number) or default value */ int64_t GetArg(const std::string& strArg, int64_t nDefault); +int64_t GetArg(const std::string& strArg, int64_t nDefault, const std::map& mapArgs); /** * Return boolean argument or default value @@ -178,6 +181,24 @@ int64_t GetArg(const std::string& strArg, int64_t nDefault); * @return command-line argument or default value */ bool GetBoolArg(const std::string& strArg, bool fDefault); +bool GetBoolArg(const std::string& strArg, bool fDefault, const std::map& mapArgs); + +/** + * @param strUsage a string where the options' help with me appended + * @param optionsHelp a vector of string pairs to iteratively call HelpMessageOpt + */ +void AppendMessagesOpt(std::string& strUsage, const std::vector >& optionsHelp); + +/** + * Return an CAmount argument or default value + * + * @param strArg Argument to get (e.g. "-foo") + * @param nDefault a string with an amount + * @param mapArgs with the arguments and their values. + * @return command-line argument or default value + * @throw std::runtime_error if there's a parsing error. + */ +CAmount ParseAmountFromArgs(const std::string& strArg, CAmount nDefault, const std::map& mapArgs); /** * Set an argument if it doesn't already have a value