Skip to content

Commit f2957ce

Browse files
committed
Util: Create ArgsManager class...
- Introduce ArgsManager::GetArgs() - Adapt util_tests.cpp to ArgsManager
1 parent 35da2ae commit f2957ce

File tree

3 files changed

+120
-46
lines changed

3 files changed

+120
-46
lines changed

src/test/util_tests.cpp

Lines changed: 45 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717

1818
#include <boost/test/unit_test.hpp>
1919

20-
extern std::map<std::string, std::string> mapArgs;
21-
2220
BOOST_FIXTURE_TEST_SUITE(util_tests, BasicTestingSetup)
2321

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

101+
class TestArgsManager : public ArgsManager
102+
{
103+
public:
104+
std::map<std::string, std::string>& GetMapArgs()
105+
{
106+
return mapArgs;
107+
};
108+
const std::map<std::string, std::vector<std::string> >& GetMapMultiArgs()
109+
{
110+
return mapMultiArgs;
111+
};
112+
};
113+
103114
BOOST_AUTO_TEST_CASE(util_ParseParameters)
104115
{
116+
TestArgsManager testArgs;
105117
const char *argv_test[] = {"-ignored", "-a", "-b", "-ccc=argument", "-ccc=multiple", "f", "-d=e"};
106118

107-
ParseParameters(0, (char**)argv_test);
108-
BOOST_CHECK(mapArgs.empty() && mapMultiArgs.empty());
119+
testArgs.ParseParameters(0, (char**)argv_test);
120+
BOOST_CHECK(testArgs.GetMapArgs().empty() && testArgs.GetMapMultiArgs().empty());
109121

110-
ParseParameters(1, (char**)argv_test);
111-
BOOST_CHECK(mapArgs.empty() && mapMultiArgs.empty());
122+
testArgs.ParseParameters(1, (char**)argv_test);
123+
BOOST_CHECK(testArgs.GetMapArgs().empty() && testArgs.GetMapMultiArgs().empty());
112124

113-
ParseParameters(5, (char**)argv_test);
125+
testArgs.ParseParameters(5, (char**)argv_test);
114126
// expectation: -ignored is ignored (program name argument),
115127
// -a, -b and -ccc end up in map, -d ignored because it is after
116128
// a non-option argument (non-GNU option parsing)
117-
BOOST_CHECK(mapArgs.size() == 3 && mapMultiArgs.size() == 3);
118-
BOOST_CHECK(IsArgSet("-a") && IsArgSet("-b") && IsArgSet("-ccc")
119-
&& !IsArgSet("f") && !IsArgSet("-d"));
120-
BOOST_CHECK(mapMultiArgs.count("-a") && mapMultiArgs.count("-b") && mapMultiArgs.count("-ccc")
121-
&& !mapMultiArgs.count("f") && !mapMultiArgs.count("-d"));
122-
123-
BOOST_CHECK(mapArgs["-a"] == "" && mapArgs["-ccc"] == "multiple");
124-
BOOST_CHECK(mapMultiArgs.at("-ccc").size() == 2);
129+
BOOST_CHECK(testArgs.GetMapArgs().size() == 3 && testArgs.GetMapMultiArgs().size() == 3);
130+
BOOST_CHECK(testArgs.IsArgSet("-a") && testArgs.IsArgSet("-b") && testArgs.IsArgSet("-ccc")
131+
&& !testArgs.IsArgSet("f") && !testArgs.IsArgSet("-d"));
132+
BOOST_CHECK(testArgs.GetMapMultiArgs().count("-a") && testArgs.GetMapMultiArgs().count("-b") && testArgs.GetMapMultiArgs().count("-ccc")
133+
&& !testArgs.GetMapMultiArgs().count("f") && !testArgs.GetMapMultiArgs().count("-d"));
134+
135+
BOOST_CHECK(testArgs.GetMapArgs()["-a"] == "" && testArgs.GetMapArgs()["-ccc"] == "multiple");
136+
BOOST_CHECK(testArgs.GetArgs("-ccc").size() == 2);
125137
}
126138

127139
BOOST_AUTO_TEST_CASE(util_GetArg)
128140
{
129-
mapArgs.clear();
130-
mapArgs["strtest1"] = "string...";
141+
TestArgsManager testArgs;
142+
testArgs.GetMapArgs().clear();
143+
testArgs.GetMapArgs()["strtest1"] = "string...";
131144
// strtest2 undefined on purpose
132-
mapArgs["inttest1"] = "12345";
133-
mapArgs["inttest2"] = "81985529216486895";
145+
testArgs.GetMapArgs()["inttest1"] = "12345";
146+
testArgs.GetMapArgs()["inttest2"] = "81985529216486895";
134147
// inttest3 undefined on purpose
135-
mapArgs["booltest1"] = "";
148+
testArgs.GetMapArgs()["booltest1"] = "";
136149
// booltest2 undefined on purpose
137-
mapArgs["booltest3"] = "0";
138-
mapArgs["booltest4"] = "1";
139-
140-
BOOST_CHECK_EQUAL(GetArg("strtest1", "default"), "string...");
141-
BOOST_CHECK_EQUAL(GetArg("strtest2", "default"), "default");
142-
BOOST_CHECK_EQUAL(GetArg("inttest1", -1), 12345);
143-
BOOST_CHECK_EQUAL(GetArg("inttest2", -1), 81985529216486895LL);
144-
BOOST_CHECK_EQUAL(GetArg("inttest3", -1), -1);
145-
BOOST_CHECK_EQUAL(GetBoolArg("booltest1", false), true);
146-
BOOST_CHECK_EQUAL(GetBoolArg("booltest2", false), false);
147-
BOOST_CHECK_EQUAL(GetBoolArg("booltest3", false), false);
148-
BOOST_CHECK_EQUAL(GetBoolArg("booltest4", false), true);
150+
testArgs.GetMapArgs()["booltest3"] = "0";
151+
testArgs.GetMapArgs()["booltest4"] = "1";
152+
153+
BOOST_CHECK_EQUAL(testArgs.GetArg("strtest1", "default"), "string...");
154+
BOOST_CHECK_EQUAL(testArgs.GetArg("strtest2", "default"), "default");
155+
BOOST_CHECK_EQUAL(testArgs.GetArg("inttest1", -1), 12345);
156+
BOOST_CHECK_EQUAL(testArgs.GetArg("inttest2", -1), 81985529216486895LL);
157+
BOOST_CHECK_EQUAL(testArgs.GetArg("inttest3", -1), -1);
158+
BOOST_CHECK_EQUAL(testArgs.GetBoolArg("booltest1", false), true);
159+
BOOST_CHECK_EQUAL(testArgs.GetBoolArg("booltest2", false), false);
160+
BOOST_CHECK_EQUAL(testArgs.GetBoolArg("booltest3", false), false);
161+
BOOST_CHECK_EQUAL(testArgs.GetBoolArg("booltest4", false), true);
149162
}
150163

151164
BOOST_AUTO_TEST_CASE(util_FormatMoney)

src/util.cpp

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#include "fs.h"
1414
#include "random.h"
1515
#include "serialize.h"
16-
#include "sync.h"
1716
#include "utilstrencodings.h"
1817
#include "utiltime.h"
1918

@@ -92,8 +91,7 @@
9291
const char * const BITCOIN_CONF_FILENAME = "bitcoin.conf";
9392
const char * const BITCOIN_PID_FILENAME = "bitcoind.pid";
9493

95-
CCriticalSection cs_args;
96-
std::map<std::string, std::string> mapArgs;
94+
ArgsManager gArgs;
9795
static std::map<std::string, std::vector<std::string> > _mapMultiArgs;
9896
const std::map<std::string, std::vector<std::string> >& mapMultiArgs = _mapMultiArgs;
9997
bool fPrintToConsole = false;
@@ -384,7 +382,7 @@ static void InterpretNegativeSetting(std::string& strKey, std::string& strValue)
384382
}
385383
}
386384

387-
void ParseParameters(int argc, const char* const argv[])
385+
void ArgsManager::ParseParameters(int argc, const char* const argv[])
388386
{
389387
LOCK(cs_args);
390388
mapArgs.clear();
@@ -420,37 +418,43 @@ void ParseParameters(int argc, const char* const argv[])
420418
}
421419
}
422420

423-
bool IsArgSet(const std::string& strArg)
421+
std::vector<std::string> ArgsManager::GetArgs(const std::string& strArg)
422+
{
423+
LOCK(cs_args);
424+
return mapMultiArgs.at(strArg);
425+
}
426+
427+
bool ArgsManager::IsArgSet(const std::string& strArg)
424428
{
425429
LOCK(cs_args);
426430
return mapArgs.count(strArg);
427431
}
428432

429-
std::string GetArg(const std::string& strArg, const std::string& strDefault)
433+
std::string ArgsManager::GetArg(const std::string& strArg, const std::string& strDefault)
430434
{
431435
LOCK(cs_args);
432436
if (mapArgs.count(strArg))
433437
return mapArgs[strArg];
434438
return strDefault;
435439
}
436440

437-
int64_t GetArg(const std::string& strArg, int64_t nDefault)
441+
int64_t ArgsManager::GetArg(const std::string& strArg, int64_t nDefault)
438442
{
439443
LOCK(cs_args);
440444
if (mapArgs.count(strArg))
441445
return atoi64(mapArgs[strArg]);
442446
return nDefault;
443447
}
444448

445-
bool GetBoolArg(const std::string& strArg, bool fDefault)
449+
bool ArgsManager::GetBoolArg(const std::string& strArg, bool fDefault)
446450
{
447451
LOCK(cs_args);
448452
if (mapArgs.count(strArg))
449453
return InterpretBool(mapArgs[strArg]);
450454
return fDefault;
451455
}
452456

453-
bool SoftSetArg(const std::string& strArg, const std::string& strValue)
457+
bool ArgsManager::SoftSetArg(const std::string& strArg, const std::string& strValue)
454458
{
455459
LOCK(cs_args);
456460
if (mapArgs.count(strArg))
@@ -459,15 +463,15 @@ bool SoftSetArg(const std::string& strArg, const std::string& strValue)
459463
return true;
460464
}
461465

462-
bool SoftSetBoolArg(const std::string& strArg, bool fValue)
466+
bool ArgsManager::SoftSetBoolArg(const std::string& strArg, bool fValue)
463467
{
464468
if (fValue)
465469
return SoftSetArg(strArg, std::string("1"));
466470
else
467471
return SoftSetArg(strArg, std::string("0"));
468472
}
469473

470-
void ForceSetArg(const std::string& strArg, const std::string& strValue)
474+
void ArgsManager::ForceSetArg(const std::string& strArg, const std::string& strValue)
471475
{
472476
LOCK(cs_args);
473477
mapArgs[strArg] = strValue;
@@ -589,7 +593,7 @@ fs::path GetConfigFile(const std::string& confPath)
589593
return pathConfigFile;
590594
}
591595

592-
void ReadConfigFile(const std::string& confPath)
596+
void ArgsManager::ReadConfigFile(const std::string& confPath)
593597
{
594598
fs::ifstream streamConfig(GetConfigFile(confPath));
595599
if (!streamConfig.good())

src/util.h

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include "compat.h"
1818
#include "fs.h"
19+
#include "sync.h"
1920
#include "tinyformat.h"
2021
#include "utiltime.h"
2122

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

150151
void PrintExceptionContinue(const std::exception *pex, const char* pszThread);
151-
void ParseParameters(int argc, const char*const argv[]);
152152
void FileCommit(FILE *file);
153153
bool TruncateFile(FILE *file, unsigned int length);
154154
int RaiseFileDescriptorLimit(int nMinFD);
@@ -163,7 +163,6 @@ fs::path GetConfigFile(const std::string& confPath);
163163
fs::path GetPidFile();
164164
void CreatePidFile(const fs::path &path, pid_t pid);
165165
#endif
166-
void ReadConfigFile(const std::string& confPath);
167166
#ifdef WIN32
168167
fs::path GetSpecialFolderPath(int nFolder, bool fCreate = true);
169168
#endif
@@ -180,6 +179,15 @@ inline bool IsSwitchChar(char c)
180179
#endif
181180
}
182181

182+
class ArgsManager
183+
{
184+
protected:
185+
CCriticalSection cs_args;
186+
std::map<std::string, std::string> mapArgs;
187+
public:
188+
void ParseParameters(int argc, const char*const argv[]);
189+
void ReadConfigFile(const std::string& confPath);
190+
std::vector<std::string> GetArgs(const std::string& strArg);
183191
/**
184192
* Return true if the given argument has been manually set
185193
*
@@ -235,6 +243,55 @@ bool SoftSetBoolArg(const std::string& strArg, bool fValue);
235243

236244
// Forces a arg setting, used only in testing
237245
void ForceSetArg(const std::string& strArg, const std::string& strValue);
246+
};
247+
248+
extern ArgsManager gArgs;
249+
250+
// wrappers using the global ArgsManager:
251+
static inline void ParseParameters(int argc, const char*const argv[])
252+
{
253+
gArgs.ParseParameters(argc, argv);
254+
}
255+
256+
static inline void ReadConfigFile(const std::string& confPath)
257+
{
258+
gArgs.ReadConfigFile(confPath);
259+
}
260+
261+
static inline bool SoftSetArg(const std::string& strArg, const std::string& strValue)
262+
{
263+
return gArgs.SoftSetArg(strArg, strValue);
264+
}
265+
266+
static inline void ForceSetArg(const std::string& strArg, const std::string& strValue)
267+
{
268+
gArgs.ForceSetArg(strArg, strValue);
269+
}
270+
271+
static inline bool IsArgSet(const std::string& strArg)
272+
{
273+
return gArgs.IsArgSet(strArg);
274+
}
275+
276+
static inline std::string GetArg(const std::string& strArg, const std::string& strDefault)
277+
{
278+
return gArgs.GetArg(strArg, strDefault);
279+
}
280+
281+
static inline int64_t GetArg(const std::string& strArg, int64_t nDefault)
282+
{
283+
return gArgs.GetArg(strArg, nDefault);
284+
}
285+
286+
static inline bool GetBoolArg(const std::string& strArg, bool fDefault)
287+
{
288+
return gArgs.GetBoolArg(strArg, fDefault);
289+
}
290+
291+
static inline bool SoftSetBoolArg(const std::string& strArg, bool fValue)
292+
{
293+
return gArgs.SoftSetBoolArg(strArg, fValue);
294+
}
238295

239296
/**
240297
* Format a string to be used as group of options in help messages

0 commit comments

Comments
 (0)