-
Notifications
You must be signed in to change notification settings - Fork 0
/
CommandLine.cpp
153 lines (139 loc) · 4.2 KB
/
CommandLine.cpp
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include "CommandLine.h"
#include "Conversion.h"
#include "version.h"
CommandLine::CommandLine()
{
mArguments.clear();
}
CommandLine::~CommandLine()
{
}
void CommandLine::Help()
{
wcout << VER_PRODUCTNAME_STR << _T(" ") << VER_FILE_VERSION_STR << endl;
wcout << _T("\t") << VER_FILE_DESCRIPTION_STR << endl << endl;
wcout << _T("Usage:") << endl;
wcout << _T("\t") << VER_ORIGINAL_FILENAME_STR << _T(" [OPTIONS]") << endl << endl;
wcout << _T("Options:") << endl;
for (const auto& it : mArguments) {
for (const auto& text : it.text) {
if (it.type != _TRUE)
wcout << (_T(" ") + text + _T(" xxx")) << endl;
else
wcout << (_T(" ") + text) << endl;
}
wcout << (_T(" ") + it.help) << endl;
}
}
void CommandLine::Add(ARGUMENT_TYPE _type, int _num, ...)
{
ARGUMENT arg;
arg.text.clear();
arg.type = _type;
int argCount = _num;
_num += 2; //help & data pointer
if (_type == _ENUM) _num++; //convert table
va_list arglist;
va_start(arglist, _num);
for (int x = 0; x < argCount; x++) {
LPCWSTR tmp = va_arg(arglist, LPCWSTR);
arg.text.push_back(Conversion::ToLower(Conversion::TrimWhiteChar(tmp)));
#ifdef DEBUG
for (const auto& itArg : mArguments) {
for (const auto& itParam : itArg.text) {
if (itParam == Conversion::ToLower(Conversion::TrimWhiteChar(tmp)))
wcout << _T("Error - the same switch, you will not be able to use it") << endl;
}
}
#endif // DEBUG
}
arg.help = va_arg(arglist, LPCWSTR);
arg.pVar = static_cast <void*> (va_arg(arglist, void*));
if (_type == _ENUM) arg.pTable = static_cast <map<wstring, UINT> *> (va_arg(arglist, void*));
else arg.pTable = nullptr;
va_end(arglist);
mArguments.push_back(arg);
}
int CommandLine::ParseCommandLine(int _argc, _TCHAR* _pArgv[], int& _correctParameters)
{
_correctParameters = 0;
for (int i = 1; i < _argc; i++) {
bool unknown = true;
for (unsigned int a = 0; a < mArguments.size(); a++) {
wstring tmpCommand = Conversion::ToLower(Conversion::TrimWhiteChar(_pArgv[i]));
tmpCommand = Conversion::TrimString(tmpCommand, L"-\\/");
if (find(mArguments[a].text.begin(), mArguments[a].text.end(), tmpCommand) != mArguments[a].text.end()) {
if (mArguments[a].type == _STRING) {
i++;
if (i >= _argc) {
wcout << _T("Error - missing argument: ") + (wstring)_pArgv[i-1] << endl;
return 1;
}
wstring tmp = _pArgv[i];
Conversion::StringReplaceAll(tmp, _T("\\n"), _T("\n"));
*((wstring*)mArguments[a].pVar) = tmp;
_correctParameters++;
unknown = false;
break;
}
else if (mArguments[a].type == _TRUE) {
*((bool*)mArguments[a].pVar) = true;
_correctParameters++;
unknown = false;
break;
}
else if (mArguments[a].type == _INT) {
i++;
if (i >= _argc) {
wcout << _T("Error - missing argument: ") + (wstring)_pArgv[i - 1] << endl;
return 1;
}
*((int*)mArguments[a].pVar) = Conversion::ToInt(Conversion::TrimWhiteChar(_pArgv[i]));
_correctParameters++;
unknown = false;
break;
}
else if (mArguments[a].type == _COLOR) {
i++;
if (i >= _argc) {
wcout << _T("Error - missing argument: ") + (wstring)_pArgv[i - 1] << endl;
return 1;
}
((pair<bool, wstring>*)mArguments[a].pVar)->first = true;
((pair<bool, wstring>*)mArguments[a].pVar)->second = Conversion::TrimWhiteChar(_pArgv[i]);
_correctParameters++;
unknown = false;
break;
}
else if (mArguments[a].type == _ENUM) {
i++;
if (i >= _argc) {
wcout << _T("Error - missing argument: ") + (wstring)_pArgv[i - 1] << endl;
return 1;
}
wstring key = Conversion::ToLower(Conversion::TrimWhiteChar(_pArgv[i]));
auto search = mArguments[a].pTable->find(key);
if (search != mArguments[a].pTable->end()) {
*((UINT*)mArguments[a].pVar) = search->second;
unknown = false;
}
else {
wcout << _T("Error - bad argument: ") + mArguments[a].text[0] + _T(" ") + key << endl;
return 1;
}
_correctParameters++;
break;
}
else {
wcout << _T("Error - unknown type") << endl;
return 1;
}
}
}
if (unknown) {
wcout << _T("Error - unknown argument: ") + (wstring)_pArgv[i] << endl;
return 1;
}
}
return 0;
}