Skip to content

Commit

Permalink
Util: Create ArgsManager class...
Browse files Browse the repository at this point in the history
- Introduce ArgsManager::GetArgs()
- Adapt util_tests.cpp to ArgsManager
  • Loading branch information
jtimon committed May 9, 2017
1 parent 35da2ae commit f2957ce
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 46 deletions.
77 changes: 45 additions & 32 deletions src/test/util_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

#include <boost/test/unit_test.hpp>

extern std::map<std::string, std::string> mapArgs;

BOOST_FIXTURE_TEST_SUITE(util_tests, BasicTestingSetup)

BOOST_AUTO_TEST_CASE(util_criticalsection)
Expand Down Expand Up @@ -100,52 +98,67 @@ BOOST_AUTO_TEST_CASE(util_DateTimeStrFormat)
BOOST_CHECK_EQUAL(DateTimeStrFormat("%a, %d %b %Y %H:%M:%S +0000", 1317425777), "Fri, 30 Sep 2011 23:36:17 +0000");
}

class TestArgsManager : public ArgsManager
{
public:
std::map<std::string, std::string>& GetMapArgs()
{
return mapArgs;
};
const std::map<std::string, std::vector<std::string> >& GetMapMultiArgs()
{
return mapMultiArgs;
};
};

BOOST_AUTO_TEST_CASE(util_ParseParameters)
{
TestArgsManager testArgs;
const char *argv_test[] = {"-ignored", "-a", "-b", "-ccc=argument", "-ccc=multiple", "f", "-d=e"};

ParseParameters(0, (char**)argv_test);
BOOST_CHECK(mapArgs.empty() && mapMultiArgs.empty());
testArgs.ParseParameters(0, (char**)argv_test);
BOOST_CHECK(testArgs.GetMapArgs().empty() && testArgs.GetMapMultiArgs().empty());

ParseParameters(1, (char**)argv_test);
BOOST_CHECK(mapArgs.empty() && mapMultiArgs.empty());
testArgs.ParseParameters(1, (char**)argv_test);
BOOST_CHECK(testArgs.GetMapArgs().empty() && testArgs.GetMapMultiArgs().empty());

ParseParameters(5, (char**)argv_test);
testArgs.ParseParameters(5, (char**)argv_test);
// expectation: -ignored is ignored (program name argument),
// -a, -b and -ccc end up in map, -d ignored because it is after
// a non-option argument (non-GNU option parsing)
BOOST_CHECK(mapArgs.size() == 3 && mapMultiArgs.size() == 3);
BOOST_CHECK(IsArgSet("-a") && IsArgSet("-b") && IsArgSet("-ccc")
&& !IsArgSet("f") && !IsArgSet("-d"));
BOOST_CHECK(mapMultiArgs.count("-a") && mapMultiArgs.count("-b") && mapMultiArgs.count("-ccc")
&& !mapMultiArgs.count("f") && !mapMultiArgs.count("-d"));

BOOST_CHECK(mapArgs["-a"] == "" && mapArgs["-ccc"] == "multiple");
BOOST_CHECK(mapMultiArgs.at("-ccc").size() == 2);
BOOST_CHECK(testArgs.GetMapArgs().size() == 3 && testArgs.GetMapMultiArgs().size() == 3);
BOOST_CHECK(testArgs.IsArgSet("-a") && testArgs.IsArgSet("-b") && testArgs.IsArgSet("-ccc")
&& !testArgs.IsArgSet("f") && !testArgs.IsArgSet("-d"));
BOOST_CHECK(testArgs.GetMapMultiArgs().count("-a") && testArgs.GetMapMultiArgs().count("-b") && testArgs.GetMapMultiArgs().count("-ccc")
&& !testArgs.GetMapMultiArgs().count("f") && !testArgs.GetMapMultiArgs().count("-d"));

BOOST_CHECK(testArgs.GetMapArgs()["-a"] == "" && testArgs.GetMapArgs()["-ccc"] == "multiple");
BOOST_CHECK(testArgs.GetArgs("-ccc").size() == 2);
}

BOOST_AUTO_TEST_CASE(util_GetArg)
{
mapArgs.clear();
mapArgs["strtest1"] = "string...";
TestArgsManager testArgs;
testArgs.GetMapArgs().clear();
testArgs.GetMapArgs()["strtest1"] = "string...";
// strtest2 undefined on purpose
mapArgs["inttest1"] = "12345";
mapArgs["inttest2"] = "81985529216486895";
testArgs.GetMapArgs()["inttest1"] = "12345";
testArgs.GetMapArgs()["inttest2"] = "81985529216486895";
// inttest3 undefined on purpose
mapArgs["booltest1"] = "";
testArgs.GetMapArgs()["booltest1"] = "";
// booltest2 undefined on purpose
mapArgs["booltest3"] = "0";
mapArgs["booltest4"] = "1";

BOOST_CHECK_EQUAL(GetArg("strtest1", "default"), "string...");
BOOST_CHECK_EQUAL(GetArg("strtest2", "default"), "default");
BOOST_CHECK_EQUAL(GetArg("inttest1", -1), 12345);
BOOST_CHECK_EQUAL(GetArg("inttest2", -1), 81985529216486895LL);
BOOST_CHECK_EQUAL(GetArg("inttest3", -1), -1);
BOOST_CHECK_EQUAL(GetBoolArg("booltest1", false), true);
BOOST_CHECK_EQUAL(GetBoolArg("booltest2", false), false);
BOOST_CHECK_EQUAL(GetBoolArg("booltest3", false), false);
BOOST_CHECK_EQUAL(GetBoolArg("booltest4", false), true);
testArgs.GetMapArgs()["booltest3"] = "0";
testArgs.GetMapArgs()["booltest4"] = "1";

BOOST_CHECK_EQUAL(testArgs.GetArg("strtest1", "default"), "string...");
BOOST_CHECK_EQUAL(testArgs.GetArg("strtest2", "default"), "default");
BOOST_CHECK_EQUAL(testArgs.GetArg("inttest1", -1), 12345);
BOOST_CHECK_EQUAL(testArgs.GetArg("inttest2", -1), 81985529216486895LL);
BOOST_CHECK_EQUAL(testArgs.GetArg("inttest3", -1), -1);
BOOST_CHECK_EQUAL(testArgs.GetBoolArg("booltest1", false), true);
BOOST_CHECK_EQUAL(testArgs.GetBoolArg("booltest2", false), false);
BOOST_CHECK_EQUAL(testArgs.GetBoolArg("booltest3", false), false);
BOOST_CHECK_EQUAL(testArgs.GetBoolArg("booltest4", false), true);
}

BOOST_AUTO_TEST_CASE(util_FormatMoney)
Expand Down
28 changes: 16 additions & 12 deletions src/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include "fs.h"
#include "random.h"
#include "serialize.h"
#include "sync.h"
#include "utilstrencodings.h"
#include "utiltime.h"

Expand Down Expand Up @@ -92,8 +91,7 @@
const char * const BITCOIN_CONF_FILENAME = "bitcoin.conf";
const char * const BITCOIN_PID_FILENAME = "bitcoind.pid";

CCriticalSection cs_args;
std::map<std::string, std::string> mapArgs;
ArgsManager gArgs;
static std::map<std::string, std::vector<std::string> > _mapMultiArgs;
const std::map<std::string, std::vector<std::string> >& mapMultiArgs = _mapMultiArgs;
bool fPrintToConsole = false;
Expand Down Expand Up @@ -384,7 +382,7 @@ static void InterpretNegativeSetting(std::string& strKey, std::string& strValue)
}
}

void ParseParameters(int argc, const char* const argv[])
void ArgsManager::ParseParameters(int argc, const char* const argv[])
{
LOCK(cs_args);
mapArgs.clear();
Expand Down Expand Up @@ -420,37 +418,43 @@ void ParseParameters(int argc, const char* const argv[])
}
}

bool IsArgSet(const std::string& strArg)
std::vector<std::string> ArgsManager::GetArgs(const std::string& strArg)
{
LOCK(cs_args);
return mapMultiArgs.at(strArg);
}

bool ArgsManager::IsArgSet(const std::string& strArg)
{
LOCK(cs_args);
return mapArgs.count(strArg);
}

std::string GetArg(const std::string& strArg, const std::string& strDefault)
std::string ArgsManager::GetArg(const std::string& strArg, const std::string& strDefault)
{
LOCK(cs_args);
if (mapArgs.count(strArg))
return mapArgs[strArg];
return strDefault;
}

int64_t GetArg(const std::string& strArg, int64_t nDefault)
int64_t ArgsManager::GetArg(const std::string& strArg, int64_t nDefault)
{
LOCK(cs_args);
if (mapArgs.count(strArg))
return atoi64(mapArgs[strArg]);
return nDefault;
}

bool GetBoolArg(const std::string& strArg, bool fDefault)
bool ArgsManager::GetBoolArg(const std::string& strArg, bool fDefault)
{
LOCK(cs_args);
if (mapArgs.count(strArg))
return InterpretBool(mapArgs[strArg]);
return fDefault;
}

bool SoftSetArg(const std::string& strArg, const std::string& strValue)
bool ArgsManager::SoftSetArg(const std::string& strArg, const std::string& strValue)
{
LOCK(cs_args);
if (mapArgs.count(strArg))
Expand All @@ -459,15 +463,15 @@ bool SoftSetArg(const std::string& strArg, const std::string& strValue)
return true;
}

bool SoftSetBoolArg(const std::string& strArg, bool fValue)
bool ArgsManager::SoftSetBoolArg(const std::string& strArg, bool fValue)
{
if (fValue)
return SoftSetArg(strArg, std::string("1"));
else
return SoftSetArg(strArg, std::string("0"));
}

void ForceSetArg(const std::string& strArg, const std::string& strValue)
void ArgsManager::ForceSetArg(const std::string& strArg, const std::string& strValue)
{
LOCK(cs_args);
mapArgs[strArg] = strValue;
Expand Down Expand Up @@ -589,7 +593,7 @@ fs::path GetConfigFile(const std::string& confPath)
return pathConfigFile;
}

void ReadConfigFile(const std::string& confPath)
void ArgsManager::ReadConfigFile(const std::string& confPath)
{
fs::ifstream streamConfig(GetConfigFile(confPath));
if (!streamConfig.good())
Expand Down
61 changes: 59 additions & 2 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "compat.h"
#include "fs.h"
#include "sync.h"
#include "tinyformat.h"
#include "utiltime.h"

Expand Down Expand Up @@ -148,7 +149,6 @@ bool error(const char* fmt, const Args&... args)
}

void PrintExceptionContinue(const std::exception *pex, const char* pszThread);
void ParseParameters(int argc, const char*const argv[]);
void FileCommit(FILE *file);
bool TruncateFile(FILE *file, unsigned int length);
int RaiseFileDescriptorLimit(int nMinFD);
Expand All @@ -163,7 +163,6 @@ fs::path GetConfigFile(const std::string& confPath);
fs::path GetPidFile();
void CreatePidFile(const fs::path &path, pid_t pid);
#endif
void ReadConfigFile(const std::string& confPath);
#ifdef WIN32
fs::path GetSpecialFolderPath(int nFolder, bool fCreate = true);
#endif
Expand All @@ -180,6 +179,15 @@ inline bool IsSwitchChar(char c)
#endif
}

class ArgsManager
{
protected:
CCriticalSection cs_args;
std::map<std::string, std::string> mapArgs;
public:
void ParseParameters(int argc, const char*const argv[]);
void ReadConfigFile(const std::string& confPath);
std::vector<std::string> GetArgs(const std::string& strArg);
/**
* Return true if the given argument has been manually set
*
Expand Down Expand Up @@ -235,6 +243,55 @@ bool SoftSetBoolArg(const std::string& strArg, bool fValue);

// Forces a arg setting, used only in testing
void ForceSetArg(const std::string& strArg, const std::string& strValue);
};

extern ArgsManager gArgs;

// wrappers using the global ArgsManager:
static inline void ParseParameters(int argc, const char*const argv[])
{
gArgs.ParseParameters(argc, argv);
}

static inline void ReadConfigFile(const std::string& confPath)
{
gArgs.ReadConfigFile(confPath);
}

static inline bool SoftSetArg(const std::string& strArg, const std::string& strValue)
{
return gArgs.SoftSetArg(strArg, strValue);
}

static inline void ForceSetArg(const std::string& strArg, const std::string& strValue)
{
gArgs.ForceSetArg(strArg, strValue);
}

static inline bool IsArgSet(const std::string& strArg)
{
return gArgs.IsArgSet(strArg);
}

static inline std::string GetArg(const std::string& strArg, const std::string& strDefault)
{
return gArgs.GetArg(strArg, strDefault);
}

static inline int64_t GetArg(const std::string& strArg, int64_t nDefault)
{
return gArgs.GetArg(strArg, nDefault);
}

static inline bool GetBoolArg(const std::string& strArg, bool fDefault)
{
return gArgs.GetBoolArg(strArg, fDefault);
}

static inline bool SoftSetBoolArg(const std::string& strArg, bool fValue)
{
return gArgs.SoftSetBoolArg(strArg, fValue);
}

/**
* Format a string to be used as group of options in help messages
Expand Down

0 comments on commit f2957ce

Please sign in to comment.