Skip to content

Commit 95407d0

Browse files
committed
Redesign WSJCppArgumentProcessor names (could be more then one value)
1 parent a6a1b4e commit 95407d0

6 files changed

+71
-37
lines changed

scripts.wsjcpp/generate.WSJCppArgumentProcessor

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ concat content_source "
5757
// " class_name "
5858

5959
" class_name "::" class_name "()
60-
: WSJCppArgumentProcessor(\"" argument_processor_name "\", \"TODO description\") {
61-
TAG = \"" argument_processor_name "\";
60+
: WSJCppArgumentProcessor({\"" argument_processor_name "\"}, \"TODO description\") {
61+
TAG = \"" class_name "\";
6262
// registrySingleArgument(\"--single\", \"What exactly do this single param?\");
6363
// registryParameterArgument(\"-param\", \"What need this param?\");
6464
// registryExample(\"here example of command\");

src/argument_processor_main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
// ArgumentProcessorMain
77

88
ArgumentProcessorMain::ArgumentProcessorMain()
9-
: WSJCppArgumentProcessor("main", "TODO description") {
10-
TAG = "main";
9+
: WSJCppArgumentProcessor({"main"}, "TODO description") {
10+
TAG = "ArgumentProcessorMain";
1111
// registrySingleArgument("--single", "What exactly do this single param?");
1212
// registryParameterArgument("-param", "What need this param?");
1313
// registryExample("here example of command");

src/wsjcpp_arguments.cpp

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -65,28 +65,32 @@ std::string WSJCppArgumentParameter::help(const std::string &sProgramName, const
6565

6666
// ---------------------------------------------------------------------
6767

68-
WSJCppArgumentProcessor::WSJCppArgumentProcessor(const std::string &sName, const std::string &sDescription) {
69-
TAG = "WSJCppArgumentProcessor-" + sName;
70-
m_sName = sName;
71-
m_sDescription = sDescription;
72-
}
73-
74-
// ---------------------------------------------------------------------
75-
7668
WSJCppArgumentProcessor::WSJCppArgumentProcessor(const std::vector<std::string> &vNames, const std::string &sDescription) {
7769
if (vNames.size() < 1) {
7870
WSJCppLog::throw_err(TAG, "Names must have 1 or more values for '" + sDescription + "'");
7971
}
80-
std::string sName = vNames[0];
81-
TAG = "WSJCppArgumentProcessor-" + sName;
82-
m_sName = sName;
72+
m_vNames = vNames;
73+
TAG = "WSJCppArgumentProcessor-" + this->getNamesAsString();
8374
m_sDescription = sDescription;
8475
}
8576

8677
// ---------------------------------------------------------------------
8778

88-
std::string WSJCppArgumentProcessor::getName() {
89-
return m_sName;
79+
const std::vector<std::string> &WSJCppArgumentProcessor::getNames() {
80+
return m_vNames;
81+
}
82+
83+
// ---------------------------------------------------------------------
84+
85+
std::string WSJCppArgumentProcessor::getNamesAsString() {
86+
std::string sRet;
87+
for (int i = 0; i < m_vNames.size(); i++) {
88+
if (sRet.size() > 0) {
89+
sRet += "|";
90+
}
91+
sRet += m_vNames[i];
92+
}
93+
return sRet;
9094
}
9195

9296
// ---------------------------------------------------------------------
@@ -98,8 +102,11 @@ std::string WSJCppArgumentProcessor::getDescription() {
98102
// ---------------------------------------------------------------------
99103

100104
void WSJCppArgumentProcessor::registryProcessor(WSJCppArgumentProcessor *p) {
101-
if (hasRegisteredArgumentName(p->getName())) {
102-
WSJCppLog::throw_err(TAG, "Argument Name '" + p->getName() + "' already registered");
105+
for (int i = 0; i < m_vNames.size(); i++) {
106+
std::string sName = m_vNames[i];
107+
if (hasRegisteredArgumentName(sName)) {
108+
WSJCppLog::throw_err(TAG, "Argument Name '" + sName + "' already registered");
109+
}
103110
}
104111
m_vProcessors.push_back(p);
105112
}
@@ -166,11 +173,14 @@ WSJCppArgumentParameter *WSJCppArgumentProcessor::findRegisteredParameterArgumen
166173
WSJCppArgumentProcessor *WSJCppArgumentProcessor::findRegisteredProcessor(const std::string &sArgumentName) {
167174
WSJCppArgumentProcessor *pRet = nullptr;
168175
for (int i = 0; i < m_vProcessors.size(); i++) {
169-
if (m_vProcessors[i]->getName() == sArgumentName) {
170-
if (pRet == nullptr) {
171-
pRet = m_vProcessors[i];
172-
} else {
173-
WSJCppLog::throw_err(TAG, "Several processors can handle this arguments");
176+
std::vector<std::string> vNames = m_vProcessors[i]->getNames();
177+
for (int n = 0; n < vNames.size(); n++) {
178+
if (vNames[n] == sArgumentName) {
179+
if (pRet == nullptr) {
180+
pRet = m_vProcessors[i];
181+
} else {
182+
WSJCppLog::throw_err(TAG, "Several processors can handle this arguments");
183+
}
174184
}
175185
}
176186
}
@@ -187,9 +197,13 @@ bool WSJCppArgumentProcessor::hasRegisteredArgumentName(const std::string &sArgu
187197
}
188198

189199
for (int i = 0; i < m_vProcessors.size(); i++) {
190-
if (m_vProcessors[i]->getName() == sArgumentName) {
191-
return true;
200+
std::vector<std::string> vNames = m_vProcessors[i]->getNames();
201+
for (int n = 0; n < vNames.size(); n++) {
202+
if (vNames[n] == sArgumentName) {
203+
return true;
204+
}
192205
}
206+
193207
}
194208

195209
for (int i = 0; i < m_vSingleArguments.size(); i++) {
@@ -210,14 +224,14 @@ bool WSJCppArgumentProcessor::applySingleArgument(const std::string &sProgramNam
210224
// ---------------------------------------------------------------------
211225

212226
bool WSJCppArgumentProcessor::applyParameterArgument(const std::string &sProgramName, const std::string &sArgumentName, const std::string &sValue) {
213-
WSJCppLog::throw_err(TAG, "No support parameter argument '" + sArgumentName + "' for '" + getName() + "'");
227+
WSJCppLog::throw_err(TAG, "No support parameter argument '" + sArgumentName + "' for '" + getNamesAsString() + "'");
214228
return false;
215229
}
216230

217231
// ---------------------------------------------------------------------
218232

219233
int WSJCppArgumentProcessor::exec(const std::string &sProgramName, const std::vector<std::string> &vSubParams) {
220-
WSJCppLog::throw_err(TAG, "Processor '" + getName() + "' has not implementation");
234+
WSJCppLog::throw_err(TAG, "Processor '" + getNamesAsString() + "' has not implementation");
221235
return -1;
222236
}
223237

@@ -257,7 +271,7 @@ std::string WSJCppArgumentProcessor::help(const std::string &sProgramName, const
257271
if (nParams > 0) {
258272
sRoute += " <arguments>";
259273
}
260-
sRoute += " " + p->getName();
274+
sRoute += " " + p->getNamesAsString();
261275
// TODO: <package-name>
262276
sRet += sPrefix + " " + sRoute + " - " + p->getDescription() + "\r\n";
263277
sRet += p->help(sRoute, sPrefix + " ");
@@ -318,7 +332,7 @@ int WSJCppArguments::recursiveExec(WSJCppArgumentProcessor *pArgumentProcessor,
318332
for (int i = 0; i < vParameterArguments.size(); i++) {
319333
WSJCppArgumentParameter *p = vParameterArguments[i];
320334
if (!pArgumentProcessor->applyParameterArgument(m_sProgramName, p->getName(), p->getValue())) {
321-
WSJCppLog::err(TAG, "Could not apply parameter argument '" + p->getName() + "' for '" + pArgumentProcessor->getName() + "'");
335+
WSJCppLog::err(TAG, "Could not apply parameter argument '" + p->getName() + "' for '" + pArgumentProcessor->getNamesAsString() + "'");
322336
return -1;
323337
}
324338
}

src/wsjcpp_arguments.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ class WSJCppArgumentParameter {
4040

4141
class WSJCppArgumentProcessor {
4242
public:
43-
WSJCppArgumentProcessor(const std::string &sName, const std::string &sDescription);
4443
WSJCppArgumentProcessor(const std::vector<std::string> &vNames, const std::string &sDescription);
45-
std::string getName();
44+
const std::vector<std::string> &getNames();
45+
std::string getNamesAsString();
4646
std::string getDescription();
4747

4848
void registryProcessor(WSJCppArgumentProcessor *p);
@@ -67,8 +67,7 @@ class WSJCppArgumentProcessor {
6767
std::string TAG;
6868

6969
private:
70-
std::string m_sName;
71-
std::vector<std::string> vNames;
70+
std::vector<std::string> m_vNames;
7271
std::string m_sDescription;
7372
std::vector<WSJCppArgumentProcessor *> m_vProcessors;
7473
std::vector<WSJCppArgumentSingle *> m_vSingleArguments;

unit-tests.wsjcpp/src/unit_test_arguments_with_params.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ UnitTestArgumentsWithParams::UnitTestArgumentsWithParams()
1616

1717
class ArgumentProcessorUninstall : public WSJCppArgumentProcessor {
1818
public:
19-
ArgumentProcessorUninstall() : WSJCppArgumentProcessor("uninstall", "uninstall something") {
19+
ArgumentProcessorUninstall() : WSJCppArgumentProcessor({"uninstall"}, "uninstall something") {
2020
TAG = "ArgumentProcessorUninstall";
2121
};
2222

unit-tests.wsjcpp/src/unit_test_simple_arguments.cpp

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class ArgumentProcessorInstall : public WSJCppArgumentProcessor {
3737
// ---------------------------------------------------------------------
3838

3939
ArgumentProcessorInstall::ArgumentProcessorInstall()
40-
: WSJCppArgumentProcessor("install", "install something") {
40+
: WSJCppArgumentProcessor({"install", "i"}, "install something") {
4141
TAG = "ArgumentProcessorInstall";
4242

4343
registrySingleArgument("--silent", "Silent");
@@ -101,7 +101,7 @@ class ArgumentProcessorProgram1 : public WSJCppArgumentProcessor {
101101
// ---------------------------------------------------------------------
102102

103103
ArgumentProcessorProgram1::ArgumentProcessorProgram1()
104-
: WSJCppArgumentProcessor("program1", "Program for unit-tests") {
104+
: WSJCppArgumentProcessor({"program1"}, "Program for unit-tests") {
105105
TAG = "ArgumentProcessorProgram1";
106106
registrySingleArgument("--hello", "Hello argument");
107107
registrySingleArgument("--hello2", "Hello2 argument");
@@ -221,6 +221,27 @@ bool UnitTestSimpleArguments::run() {
221221
compareS(bTestSuccess, "args2 argument 2", pProgram1->vParams[0], "arg1");
222222
compareS(bTestSuccess, "args2 argument 2", pProgram1->vParams[1], "arg2");
223223

224+
pProgram1->reset();
225+
226+
const int argc3 = 6;
227+
const char *argv3[argc3] = {"./program", "--hello", "i", "--silent", "some1", "some2"};
228+
229+
WSJCppArguments args3(argc3, argv3, pProgram1);
230+
compareN(bTestSuccess, "args3 exec", args3.exec(), 0);
231+
232+
compareB(bTestSuccess, "args3 --hello", pProgram1->hello, true);
233+
compareB(bTestSuccess, "args3 --hello2", pProgram1->hello2, false);
234+
compareB(bTestSuccess, "args3 --hello3", pProgram1->hello3, false);
235+
236+
compareS(bTestSuccess, "args3 -param1", pProgram1->param1, "");
237+
compareS(bTestSuccess, "args3 -param2", pProgram1->param2, "");
238+
239+
compareB(bTestSuccess, "args3 install --silent", pProgram1->pInstall->silent, true);
240+
compareN(bTestSuccess, "args3 install arguments size", pProgram1->pInstall->vParams.size(), 2);
241+
compareS(bTestSuccess, "args3 install argument 1", pProgram1->pInstall->vParams[0], "some1");
242+
compareS(bTestSuccess, "args3 install argument 2", pProgram1->pInstall->vParams[1], "some2");
243+
244+
224245
WSJCppLog::info(TAG, "\r\n" + args2.help());
225246

226247
return bTestSuccess;

0 commit comments

Comments
 (0)