-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathargvContext.cpp
More file actions
62 lines (54 loc) · 1.54 KB
/
argvContext.cpp
File metadata and controls
62 lines (54 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <assert.h>
#include "string_util/stringUtil.h"
#include "argvContext.h"
#include "common.h"
USING_NAMESPACE(std)
NAMESPACE_SETUP(Util)
ArgvContext::ArgvContext(const int argc, const char* const argv[]) {
assert(argc > 0);
assert(argv != NULL);
for(int i = 0; i < argc; i++) {
// options, for example
// 1. chenguolin
// 2. -name chenguolin
// 3. --name chenguolin
if(StringUtil::StartsWith(argv[i], "-")) {
string option = string(argv[i]);
option = StringUtil::Trim(option, '-');
string argument = "";
if((i+1 < argc) && !StringUtil::StartsWith(argv[i+1], "-")) {
argument = string(argv[++i]);
}
mOptionsMap[option] = argument;
}
else {
mArgsVec.push_back(string(argv[i]));
}
}
}
ArgvContext::~ArgvContext() {
}
bool ArgvContext::HasOption(const string& key) const{
if (mOptionsMap.find(key) != mOptionsMap.end()) {
return true;
}
return false;
}
string ArgvContext::operator[](size_t index) const {
assert(index < mArgsVec.size());
return mArgsVec[index];
}
string ArgvContext::operator[](const string& key) const{
map<string, string>::const_iterator it;
it = mOptionsMap.find(key);
if (it != mOptionsMap.end()) {
return it->second;
}
return "";
}
ostream& operator<<(ostream& os, const ArgvContext& argvContext) {
os << argvContext.mOptionsMap;
os << argvContext.mArgsVec;
return os;
}
NAMESPACE_END(Util)