Introduce (and use) an IsArgSet accessor method

This commit is contained in:
Matt Corallo 2016-11-29 17:51:30 -08:00
parent 2b5f085ad1
commit 0cf86a6678
12 changed files with 44 additions and 31 deletions

View file

@ -76,9 +76,9 @@ static int AppInitRPC(int argc, char* argv[])
// Parameters // Parameters
// //
ParseParameters(argc, argv); ParseParameters(argc, argv);
if (argc<2 || mapArgs.count("-?") || mapArgs.count("-h") || mapArgs.count("-help") || mapArgs.count("-version")) { if (argc<2 || IsArgSet("-?") || IsArgSet("-h") || IsArgSet("-help") || IsArgSet("-version")) {
std::string strUsage = strprintf(_("%s RPC client version"), _(PACKAGE_NAME)) + " " + FormatFullVersion() + "\n"; std::string strUsage = strprintf(_("%s RPC client version"), _(PACKAGE_NAME)) + " " + FormatFullVersion() + "\n";
if (!mapArgs.count("-version")) { if (!IsArgSet("-version")) {
strUsage += "\n" + _("Usage:") + "\n" + strUsage += "\n" + _("Usage:") + "\n" +
" bitcoin-cli [options] <command> [params] " + strprintf(_("Send command to %s"), _(PACKAGE_NAME)) + "\n" + " bitcoin-cli [options] <command> [params] " + strprintf(_("Send command to %s"), _(PACKAGE_NAME)) + "\n" +
" bitcoin-cli [options] help " + _("List commands") + "\n" + " bitcoin-cli [options] help " + _("List commands") + "\n" +

View file

@ -51,7 +51,7 @@ static int AppInitRawTx(int argc, char* argv[])
fCreateBlank = GetBoolArg("-create", false); fCreateBlank = GetBoolArg("-create", false);
if (argc<2 || mapArgs.count("-?") || mapArgs.count("-h") || mapArgs.count("-help")) if (argc<2 || IsArgSet("-?") || IsArgSet("-h") || IsArgSet("-help"))
{ {
// First part of help message is specific to this utility // First part of help message is specific to this utility
std::string strUsage = strprintf(_("%s bitcoin-tx utility version"), _(PACKAGE_NAME)) + " " + FormatFullVersion() + "\n\n" + std::string strUsage = strprintf(_("%s bitcoin-tx utility version"), _(PACKAGE_NAME)) + " " + FormatFullVersion() + "\n\n" +

View file

@ -75,11 +75,11 @@ bool AppInit(int argc, char* argv[])
ParseParameters(argc, argv); ParseParameters(argc, argv);
// Process help and version before taking care about datadir // Process help and version before taking care about datadir
if (mapArgs.count("-?") || mapArgs.count("-h") || mapArgs.count("-help") || mapArgs.count("-version")) if (IsArgSet("-?") || IsArgSet("-h") || IsArgSet("-help") || IsArgSet("-version"))
{ {
std::string strUsage = strprintf(_("%s Daemon"), _(PACKAGE_NAME)) + " " + _("version") + " " + FormatFullVersion() + "\n"; std::string strUsage = strprintf(_("%s Daemon"), _(PACKAGE_NAME)) + " " + _("version") + " " + FormatFullVersion() + "\n";
if (mapArgs.count("-version")) if (IsArgSet("-version"))
{ {
strUsage += FormatParagraph(LicenseInfo()); strUsage += FormatParagraph(LicenseInfo());
} }

View file

@ -322,10 +322,10 @@ static bool HTTPBindAddresses(struct evhttp* http)
std::vector<std::pair<std::string, uint16_t> > endpoints; std::vector<std::pair<std::string, uint16_t> > endpoints;
// Determine what addresses to bind to // Determine what addresses to bind to
if (!mapArgs.count("-rpcallowip")) { // Default to loopback if not allowing external IPs if (!IsArgSet("-rpcallowip")) { // Default to loopback if not allowing external IPs
endpoints.push_back(std::make_pair("::1", defaultPort)); endpoints.push_back(std::make_pair("::1", defaultPort));
endpoints.push_back(std::make_pair("127.0.0.1", defaultPort)); endpoints.push_back(std::make_pair("127.0.0.1", defaultPort));
if (mapArgs.count("-rpcbind")) { if (IsArgSet("-rpcbind")) {
LogPrintf("WARNING: option -rpcbind was ignored because -rpcallowip was not specified, refusing to allow everyone to connect\n"); LogPrintf("WARNING: option -rpcbind was ignored because -rpcallowip was not specified, refusing to allow everyone to connect\n");
} }
} else if (mapMultiArgs.count("-rpcbind")) { // Specific bind address } else if (mapMultiArgs.count("-rpcbind")) { // Specific bind address

View file

@ -708,11 +708,11 @@ void InitParameterInteraction()
{ {
// when specifying an explicit binding address, you want to listen on it // when specifying an explicit binding address, you want to listen on it
// even when -connect or -proxy is specified // even when -connect or -proxy is specified
if (mapArgs.count("-bind")) { if (IsArgSet("-bind")) {
if (SoftSetBoolArg("-listen", true)) if (SoftSetBoolArg("-listen", true))
LogPrintf("%s: parameter interaction: -bind set -> setting -listen=1\n", __func__); LogPrintf("%s: parameter interaction: -bind set -> setting -listen=1\n", __func__);
} }
if (mapArgs.count("-whitebind")) { if (IsArgSet("-whitebind")) {
if (SoftSetBoolArg("-listen", true)) if (SoftSetBoolArg("-listen", true))
LogPrintf("%s: parameter interaction: -whitebind set -> setting -listen=1\n", __func__); LogPrintf("%s: parameter interaction: -whitebind set -> setting -listen=1\n", __func__);
} }
@ -725,7 +725,7 @@ void InitParameterInteraction()
LogPrintf("%s: parameter interaction: -connect set -> setting -listen=0\n", __func__); LogPrintf("%s: parameter interaction: -connect set -> setting -listen=0\n", __func__);
} }
if (mapArgs.count("-proxy")) { if (IsArgSet("-proxy")) {
// to protect privacy, do not listen by default if a default proxy server is specified // to protect privacy, do not listen by default if a default proxy server is specified
if (SoftSetBoolArg("-listen", false)) if (SoftSetBoolArg("-listen", false))
LogPrintf("%s: parameter interaction: -proxy set -> setting -listen=0\n", __func__); LogPrintf("%s: parameter interaction: -proxy set -> setting -listen=0\n", __func__);
@ -748,7 +748,7 @@ void InitParameterInteraction()
LogPrintf("%s: parameter interaction: -listen=0 -> setting -listenonion=0\n", __func__); LogPrintf("%s: parameter interaction: -listen=0 -> setting -listenonion=0\n", __func__);
} }
if (mapArgs.count("-externalip")) { if (IsArgSet("-externalip")) {
// if an explicit public IP is specified, do not try to find others // if an explicit public IP is specified, do not try to find others
if (SoftSetBoolArg("-discover", false)) if (SoftSetBoolArg("-discover", false))
LogPrintf("%s: parameter interaction: -externalip set -> setting -discover=0\n", __func__); LogPrintf("%s: parameter interaction: -externalip set -> setting -discover=0\n", __func__);
@ -892,7 +892,7 @@ bool AppInitParameterInteraction()
if (GetBoolArg("-debugnet", false)) if (GetBoolArg("-debugnet", false))
InitWarning(_("Unsupported argument -debugnet ignored, use -debug=net.")); InitWarning(_("Unsupported argument -debugnet ignored, use -debug=net."));
// Check for -socks - as this is a privacy risk to continue, exit here // Check for -socks - as this is a privacy risk to continue, exit here
if (mapArgs.count("-socks")) if (IsArgSet("-socks"))
return InitError(_("Unsupported argument -socks found. Setting SOCKS version isn't possible anymore, only SOCKS5 proxies are supported.")); return InitError(_("Unsupported argument -socks found. Setting SOCKS version isn't possible anymore, only SOCKS5 proxies are supported."));
// Check for -tor - as this is a privacy risk to continue, exit here // Check for -tor - as this is a privacy risk to continue, exit here
if (GetBoolArg("-tor", false)) if (GetBoolArg("-tor", false))
@ -904,7 +904,7 @@ bool AppInitParameterInteraction()
if (GetBoolArg("-whitelistalwaysrelay", false)) if (GetBoolArg("-whitelistalwaysrelay", false))
InitWarning(_("Unsupported argument -whitelistalwaysrelay ignored, use -whitelistrelay and/or -whitelistforcerelay.")); InitWarning(_("Unsupported argument -whitelistalwaysrelay ignored, use -whitelistrelay and/or -whitelistforcerelay."));
if (mapArgs.count("-blockminsize")) if (IsArgSet("-blockminsize"))
InitWarning("Unsupported argument -blockminsize ignored."); InitWarning("Unsupported argument -blockminsize ignored.");
// Checkmempool and checkblockindex default to true in regtest mode // Checkmempool and checkblockindex default to true in regtest mode
@ -959,7 +959,7 @@ bool AppInitParameterInteraction()
// a transaction spammer can cheaply fill blocks using // a transaction spammer can cheaply fill blocks using
// 1-satoshi-fee transactions. It should be set above the real // 1-satoshi-fee transactions. It should be set above the real
// cost to you of processing a transaction. // cost to you of processing a transaction.
if (mapArgs.count("-minrelaytxfee")) if (IsArgSet("-minrelaytxfee"))
{ {
CAmount n = 0; CAmount n = 0;
if (!ParseMoney(mapArgs["-minrelaytxfee"], n) || 0 == n) if (!ParseMoney(mapArgs["-minrelaytxfee"], n) || 0 == n)
@ -997,7 +997,7 @@ bool AppInitParameterInteraction()
nMaxTipAge = GetArg("-maxtipage", DEFAULT_MAX_TIP_AGE); nMaxTipAge = GetArg("-maxtipage", DEFAULT_MAX_TIP_AGE);
fEnableReplacement = GetBoolArg("-mempoolreplacement", DEFAULT_ENABLE_REPLACEMENT); fEnableReplacement = GetBoolArg("-mempoolreplacement", DEFAULT_ENABLE_REPLACEMENT);
if ((!fEnableReplacement) && mapArgs.count("-mempoolreplacement")) { if ((!fEnableReplacement) && IsArgSet("-mempoolreplacement")) {
// Minimal effort at forwards compatibility // Minimal effort at forwards compatibility
std::string strReplacementModeList = GetArg("-mempoolreplacement", ""); // default is impossible std::string strReplacementModeList = GetArg("-mempoolreplacement", ""); // default is impossible
std::vector<std::string> vstrReplacementModes; std::vector<std::string> vstrReplacementModes;
@ -1291,7 +1291,7 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
uint64_t nMaxOutboundLimit = 0; //unlimited unless -maxuploadtarget is set uint64_t nMaxOutboundLimit = 0; //unlimited unless -maxuploadtarget is set
uint64_t nMaxOutboundTimeframe = MAX_UPLOAD_TIMEFRAME; uint64_t nMaxOutboundTimeframe = MAX_UPLOAD_TIMEFRAME;
if (mapArgs.count("-maxuploadtarget")) { if (IsArgSet("-maxuploadtarget")) {
nMaxOutboundLimit = GetArg("-maxuploadtarget", DEFAULT_MAX_UPLOAD_TARGET)*1024*1024; nMaxOutboundLimit = GetArg("-maxuploadtarget", DEFAULT_MAX_UPLOAD_TARGET)*1024*1024;
} }
@ -1523,7 +1523,7 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
fHaveGenesis = true; fHaveGenesis = true;
} }
if (mapArgs.count("-blocknotify")) if (IsArgSet("-blocknotify"))
uiInterface.NotifyBlockTip.connect(BlockNotifyCallback); uiInterface.NotifyBlockTip.connect(BlockNotifyCallback);
std::vector<boost::filesystem::path> vImportFiles; std::vector<boost::filesystem::path> vImportFiles;

View file

@ -84,12 +84,12 @@ BlockAssembler::BlockAssembler(const CChainParams& _chainparams)
nBlockMaxWeight = DEFAULT_BLOCK_MAX_WEIGHT; nBlockMaxWeight = DEFAULT_BLOCK_MAX_WEIGHT;
nBlockMaxSize = DEFAULT_BLOCK_MAX_SIZE; nBlockMaxSize = DEFAULT_BLOCK_MAX_SIZE;
bool fWeightSet = false; bool fWeightSet = false;
if (mapArgs.count("-blockmaxweight")) { if (IsArgSet("-blockmaxweight")) {
nBlockMaxWeight = GetArg("-blockmaxweight", DEFAULT_BLOCK_MAX_WEIGHT); nBlockMaxWeight = GetArg("-blockmaxweight", DEFAULT_BLOCK_MAX_WEIGHT);
nBlockMaxSize = MAX_BLOCK_SERIALIZED_SIZE; nBlockMaxSize = MAX_BLOCK_SERIALIZED_SIZE;
fWeightSet = true; fWeightSet = true;
} }
if (mapArgs.count("-blockmaxsize")) { if (IsArgSet("-blockmaxsize")) {
nBlockMaxSize = GetArg("-blockmaxsize", DEFAULT_BLOCK_MAX_SIZE); nBlockMaxSize = GetArg("-blockmaxsize", DEFAULT_BLOCK_MAX_SIZE);
if (!fWeightSet) { if (!fWeightSet) {
nBlockMaxWeight = nBlockMaxSize * WITNESS_SCALE_FACTOR; nBlockMaxWeight = nBlockMaxSize * WITNESS_SCALE_FACTOR;

View file

@ -1060,7 +1060,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
unsigned int nMaxSendBufferSize = connman.GetSendBufferSize(); unsigned int nMaxSendBufferSize = connman.GetSendBufferSize();
LogPrint("net", "received: %s (%u bytes) peer=%d\n", SanitizeString(strCommand), vRecv.size(), pfrom->id); LogPrint("net", "received: %s (%u bytes) peer=%d\n", SanitizeString(strCommand), vRecv.size(), pfrom->id);
if (mapArgs.count("-dropmessagestest") && GetRand(atoi(mapArgs["-dropmessagestest"])) == 0) if (IsArgSet("-dropmessagestest") && GetRand(atoi(mapArgs["-dropmessagestest"])) == 0)
{ {
LogPrintf("dropmessagestest DROPPING RECV MESSAGE\n"); LogPrintf("dropmessagestest DROPPING RECV MESSAGE\n");
return true; return true;

View file

@ -587,9 +587,9 @@ int main(int argc, char *argv[])
// Show help message immediately after parsing command-line options (for "-lang") and setting locale, // Show help message immediately after parsing command-line options (for "-lang") and setting locale,
// but before showing splash screen. // but before showing splash screen.
if (mapArgs.count("-?") || mapArgs.count("-h") || mapArgs.count("-help") || mapArgs.count("-version")) if (IsArgSet("-?") || IsArgSet("-h") || IsArgSet("-help") || IsArgSet("-version"))
{ {
HelpMessageDialog help(NULL, mapArgs.count("-version")); HelpMessageDialog help(NULL, IsArgSet("-version"));
help.showOrPrint(); help.showOrPrint();
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
@ -672,7 +672,7 @@ int main(int argc, char *argv[])
// Allow parameter interaction before we create the options model // Allow parameter interaction before we create the options model
app.parameterSetup(); app.parameterSetup();
// Load GUI settings from QSettings // Load GUI settings from QSettings
app.createOptionsModel(mapArgs.count("-resetguisettings") != 0); app.createOptionsModel(IsArgSet("-resetguisettings"));
// Subscribe to global signals from core // Subscribe to global signals from core
uiInterface.InitMessage.connect(InitMessage); uiInterface.InitMessage.connect(InitMessage);

View file

@ -115,8 +115,8 @@ BOOST_AUTO_TEST_CASE(util_ParseParameters)
// -a, -b and -ccc end up in map, -d ignored because it is after // -a, -b and -ccc end up in map, -d ignored because it is after
// a non-option argument (non-GNU option parsing) // a non-option argument (non-GNU option parsing)
BOOST_CHECK(mapArgs.size() == 3 && mapMultiArgs.size() == 3); BOOST_CHECK(mapArgs.size() == 3 && mapMultiArgs.size() == 3);
BOOST_CHECK(mapArgs.count("-a") && mapArgs.count("-b") && mapArgs.count("-ccc") BOOST_CHECK(IsArgSet("-a") && IsArgSet("-b") && IsArgSet("-ccc")
&& !mapArgs.count("f") && !mapArgs.count("-d")); && !IsArgSet("f") && !IsArgSet("-d"));
BOOST_CHECK(mapMultiArgs.count("-a") && mapMultiArgs.count("-b") && mapMultiArgs.count("-ccc") BOOST_CHECK(mapMultiArgs.count("-a") && mapMultiArgs.count("-b") && mapMultiArgs.count("-ccc")
&& !mapMultiArgs.count("f") && !mapMultiArgs.count("-d")); && !mapMultiArgs.count("f") && !mapMultiArgs.count("-d"));

View file

@ -379,6 +379,11 @@ void ParseParameters(int argc, const char* const argv[])
} }
} }
bool IsArgSet(const std::string& strArg)
{
return mapArgs.count(strArg);
}
std::string GetArg(const std::string& strArg, const std::string& strDefault) std::string GetArg(const std::string& strArg, const std::string& strDefault)
{ {
if (mapArgs.count(strArg)) if (mapArgs.count(strArg))
@ -498,7 +503,7 @@ const boost::filesystem::path &GetDataDir(bool fNetSpecific)
if (!path.empty()) if (!path.empty())
return path; return path;
if (mapArgs.count("-datadir")) { if (IsArgSet("-datadir")) {
path = fs::system_complete(mapArgs["-datadir"]); path = fs::system_complete(mapArgs["-datadir"]);
if (!fs::is_directory(path)) { if (!fs::is_directory(path)) {
path = ""; path = "";

View file

@ -123,6 +123,14 @@ inline bool IsSwitchChar(char c)
#endif #endif
} }
/**
* Return true if the given argument has been manually set
*
* @param strArg Argument to get (e.g. "-foo")
* @return true if the argument has been set
*/
bool IsArgSet(const std::string& strArg);
/** /**
* Return string argument or default value * Return string argument or default value
* *

View file

@ -3494,7 +3494,7 @@ bool CWallet::InitLoadWallet()
walletInstance->SetBestChain(chainActive.GetLocator()); walletInstance->SetBestChain(chainActive.GetLocator());
} }
else if (mapArgs.count("-usehd")) { else if (IsArgSet("-usehd")) {
bool useHD = GetBoolArg("-usehd", DEFAULT_USE_HD_WALLET); bool useHD = GetBoolArg("-usehd", DEFAULT_USE_HD_WALLET);
if (walletInstance->IsHDEnabled() && !useHD) if (walletInstance->IsHDEnabled() && !useHD)
return InitError(strprintf(_("Error loading %s: You can't disable HD on a already existing HD wallet"), walletFile)); return InitError(strprintf(_("Error loading %s: You can't disable HD on a already existing HD wallet"), walletFile));
@ -3618,7 +3618,7 @@ bool CWallet::ParameterInteraction()
InitWarning(AmountHighWarn("-minrelaytxfee") + " " + InitWarning(AmountHighWarn("-minrelaytxfee") + " " +
_("The wallet will avoid paying less than the minimum relay fee.")); _("The wallet will avoid paying less than the minimum relay fee."));
if (mapArgs.count("-mintxfee")) if (IsArgSet("-mintxfee"))
{ {
CAmount n = 0; CAmount n = 0;
if (!ParseMoney(mapArgs["-mintxfee"], n) || 0 == n) if (!ParseMoney(mapArgs["-mintxfee"], n) || 0 == n)
@ -3628,7 +3628,7 @@ bool CWallet::ParameterInteraction()
_("This is the minimum transaction fee you pay on every transaction.")); _("This is the minimum transaction fee you pay on every transaction."));
CWallet::minTxFee = CFeeRate(n); CWallet::minTxFee = CFeeRate(n);
} }
if (mapArgs.count("-fallbackfee")) if (IsArgSet("-fallbackfee"))
{ {
CAmount nFeePerK = 0; CAmount nFeePerK = 0;
if (!ParseMoney(mapArgs["-fallbackfee"], nFeePerK)) if (!ParseMoney(mapArgs["-fallbackfee"], nFeePerK))
@ -3638,7 +3638,7 @@ bool CWallet::ParameterInteraction()
_("This is the transaction fee you may pay when fee estimates are not available.")); _("This is the transaction fee you may pay when fee estimates are not available."));
CWallet::fallbackFee = CFeeRate(nFeePerK); CWallet::fallbackFee = CFeeRate(nFeePerK);
} }
if (mapArgs.count("-paytxfee")) if (IsArgSet("-paytxfee"))
{ {
CAmount nFeePerK = 0; CAmount nFeePerK = 0;
if (!ParseMoney(mapArgs["-paytxfee"], nFeePerK)) if (!ParseMoney(mapArgs["-paytxfee"], nFeePerK))
@ -3654,7 +3654,7 @@ bool CWallet::ParameterInteraction()
mapArgs["-paytxfee"], ::minRelayTxFee.ToString())); mapArgs["-paytxfee"], ::minRelayTxFee.ToString()));
} }
} }
if (mapArgs.count("-maxtxfee")) if (IsArgSet("-maxtxfee"))
{ {
CAmount nMaxFee = 0; CAmount nMaxFee = 0;
if (!ParseMoney(mapArgs["-maxtxfee"], nMaxFee)) if (!ParseMoney(mapArgs["-maxtxfee"], nMaxFee))