Skip to content

Commit 48923f6

Browse files
committed
Some redesign
1 parent 3db794f commit 48923f6

File tree

6 files changed

+216
-36
lines changed

6 files changed

+216
-36
lines changed

src/wsjcpp_arguments.cpp

Lines changed: 79 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,22 @@
33

44
// ---------------------------------------------------------------------
55

6-
WSJCppArgumentProcessor::WSJCppArgumentProcessor() {
7-
//
6+
WSJCppArgumentProcessor::WSJCppArgumentProcessor(const std::string &sName, const std::string &sDescription) {
7+
TAG = "WSJCppArgumentProcessor-" + sName;
8+
m_sName = sName;
9+
m_sDescription = sDescription;
10+
}
11+
12+
// ---------------------------------------------------------------------
13+
14+
std::string WSJCppArgumentProcessor::getName() {
15+
return m_sName;
16+
}
17+
18+
// ---------------------------------------------------------------------
19+
20+
std::string WSJCppArgumentProcessor::getDescription() {
21+
return m_sDescription;
822
}
923

1024
// ---------------------------------------------------------------------
@@ -22,68 +36,106 @@ bool WSJCppArgumentProcessor::canHandle(const std::vector<std::string> &vSubPara
2236

2337
// ---------------------------------------------------------------------
2438

25-
void WSJCppArgumentProcessor::addProcessor(WSJCppArgumentProcessor *p) {
39+
void WSJCppArgumentProcessor::registryProcessor(WSJCppArgumentProcessor *p) {
40+
for (int i = 0; i < m_vProcessors.size(); i++) {
41+
if (m_vProcessors[i]->getName() == p->getName()) {
42+
WSJCppLog::throw_err(TAG, "Processor '" + p->getName() + "' already registered");
43+
}
44+
}
2645
m_vProcessors.push_back(p);
2746
}
2847

2948
// ---------------------------------------------------------------------
3049

50+
void WSJCppArgumentProcessor::registrySingleArgument(const std::string &sArgumentName, const std::string &sDescription) {
51+
52+
}
53+
54+
// ---------------------------------------------------------------------
55+
56+
void WSJCppArgumentProcessor::registryParamArgument(const std::string &sArgumentName, const std::string &sDescription) {
57+
58+
}
59+
60+
// ---------------------------------------------------------------------
61+
62+
std::string WSJCppArgumentProcessor::help(const std::string &sProgramName, const std::string &sPrefix) {
63+
std::string sRet = "";
64+
if (sProgramName == m_sName) {
65+
sRet = "Usage: " + m_sName + " <params> <command> <subcomands/args>\r\n";
66+
}
67+
sRet += sPrefix + " <params>: \r\n";
68+
sRet += sPrefix + " [] - description \r\n"; // TODO
69+
70+
if (m_vProcessors.size() > 0) {
71+
sRet += sPrefix + " <commands>: \r\n";
72+
for (int i = 0; i < m_vProcessors.size(); i++) {
73+
WSJCppArgumentProcessor *p = m_vProcessors[i];
74+
sRet += sPrefix + " " + p->getName() + " - " + p->getDescription() + "\r\n";
75+
sRet += p->help(sProgramName, sPrefix + " ");
76+
}
77+
}
78+
sRet += "\r\n"; // TODO
79+
return sRet;
80+
}
81+
82+
// ---------------------------------------------------------------------
83+
84+
WSJCppArgumentProcessor *WSJCppArgumentProcessor::findProcessor(const std::vector<std::string> &vSubParams) {
85+
WSJCppArgumentProcessor *pRet = nullptr;
86+
bool bCanHandle = false;
87+
for (int i = 0; i < m_vProcessors.size(); i++) {
88+
if (m_vProcessors[i]->canHandle(vSubParams)) {
89+
if (pRet == nullptr) {
90+
pRet = m_vProcessors[i];
91+
} else {
92+
WSJCppLog::throw_err(TAG, "Several processors can handle this arguments");
93+
}
94+
}
95+
}
96+
return pRet;
97+
}
98+
99+
// ---------------------------------------------------------------------
100+
31101
WSJCppArguments::WSJCppArguments(int argc, const char* argv[]) {
32102
TAG = "WSJCppArguments";
33103
for (int i = 0; i < argc; i++) {
34104
m_vArguments.push_back(std::string(argv[i]));
35105
}
36106
m_sProgramName = m_vArguments[0];
37107
m_vArguments.erase(m_vArguments.begin());
38-
}
39-
40-
// ---------------------------------------------------------------------
41-
42-
void WSJCppArguments::addProcessor(WSJCppArgumentProcessor *p) {
43-
m_vProcessors.push_back(p);
108+
m_pRoot = new WSJCppArgumentProcessor(m_sProgramName, "");
44109
}
45110

46111
// ---------------------------------------------------------------------
47112

48113
int WSJCppArguments::handle() {
49-
WSJCppArgumentProcessor *pArgumentProcessor = this->findProcessor();
114+
WSJCppArgumentProcessor *pArgumentProcessor = m_pRoot->findProcessor(m_vArguments);
50115
if (pArgumentProcessor != nullptr) {
51116
return pArgumentProcessor->handle(m_sProgramName, m_vArguments);
52117
}
53118
WSJCppLog::err(TAG, "Not found argument processor");
54119
return -1;
55-
56120
}
57121

58122
// ---------------------------------------------------------------------
59123

60124
bool WSJCppArguments::canHandle() {
61-
WSJCppArgumentProcessor *pArgumentProcessor = this->findProcessor();
125+
WSJCppArgumentProcessor *pArgumentProcessor = m_pRoot->findProcessor(m_vArguments);
62126
return pArgumentProcessor != nullptr;
63127
}
64128

65129
// ---------------------------------------------------------------------
66130

67-
WSJCppArgumentProcessor *WSJCppArguments::findProcessor() {
68-
WSJCppArgumentProcessor *pRet = nullptr;
69-
bool bCanHandle = false;
70-
for (int i = 0; i < m_vProcessors.size(); i++) {
71-
if (m_vProcessors[i]->canHandle(m_vArguments)) {
72-
if (pRet == nullptr) {
73-
pRet = m_vProcessors[i];
74-
} else {
75-
WSJCppLog::throw_err(TAG, "Several processors can handle this arguments");
76-
}
77-
}
78-
}
79-
return pRet;
131+
WSJCppArgumentProcessor &WSJCppArguments::getRoot() {
132+
return *m_pRoot;
80133
}
81134

82135
// ---------------------------------------------------------------------
83136

84137
std::string WSJCppArguments::help() {
85-
// TODO
86-
return "";
138+
return m_pRoot->help(m_sProgramName, "");
87139
}
88140

89141
// ---------------------------------------------------------------------

src/wsjcpp_arguments.h

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,56 @@
1-
#ifndef WSJCPP_PARSE_ARGUMENTS_H
2-
#define WSJCPP_PARSE_ARGUMENTS_H
1+
#ifndef WSJCPP_ARGUMENTS_H
2+
#define WSJCPP_ARGUMENTS_H
33

44
#include <string>
55
#include <vector>
6+
#include <map>
67

78
// ---------------------------------------------------------------------
89

910
class WSJCppArgumentProcessor {
1011
public:
11-
WSJCppArgumentProcessor();
12+
WSJCppArgumentProcessor(const std::string &sName, const std::string &sDescription);
13+
std::string getName();
14+
std::string getDescription();
15+
16+
void registryProcessor(WSJCppArgumentProcessor *p);
17+
void registrySingleArgument(const std::string &sArgumentName, const std::string &sDescription);
18+
void registryParamArgument(const std::string &sArgumentName, const std::string &sDescription);
19+
20+
std::string help(const std::string &sProgramName, const std::string &sPrefix);
21+
22+
WSJCppArgumentProcessor *findProcessor(const std::vector<std::string> &vSubParams);
23+
24+
25+
bool hasSingleArgument(const std::string &sArgumentName);
26+
bool getValueOfParam(const std::string &sArgumentName);
27+
bool hasParamArgument(const std::string &sArgumentName, const std::string &sDescription);
28+
29+
1230
virtual int handle(const std::string &sProgramName, const std::vector<std::string> &vSubParams);
1331
virtual bool canHandle(const std::vector<std::string> &vSubParams);
14-
void addProcessor(WSJCppArgumentProcessor *p);
1532
private:
33+
std::string TAG;
34+
std::string m_sName;
35+
std::string m_sDescription;
1636
std::vector<WSJCppArgumentProcessor *> m_vProcessors;
37+
std::map<std::string, std::string> m_vSingleArguments;
38+
std::map<std::string, std::string> m_vParamArguments;
1739
};
1840

1941
// ---------------------------------------------------------------------
2042

2143
class WSJCppArguments {
2244
public:
2345
WSJCppArguments(int argc, const char* argv[]);
24-
void addProcessor(WSJCppArgumentProcessor *p);
2546
int handle();
2647
bool canHandle();
2748
std::string help();
49+
WSJCppArgumentProcessor &getRoot();
2850

2951
private:
30-
WSJCppArgumentProcessor *findProcessor();
52+
WSJCppArgumentProcessor *m_pRoot;
53+
3154
std::string TAG;
3255
std::vector<std::string> m_vArguments;
3356
std::string m_sProgramName;
@@ -36,4 +59,4 @@ class WSJCppArguments {
3659

3760
// ---------------------------------------------------------------------
3861

39-
#endif // WSJCPP_PARSE_ARGUMENTS_H
62+
#endif // WSJCPP_ARGUMENTS_H

unit-tests/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ list (APPEND WSJCPP_SOURCES "./src/unit_tests.h")
3030
list (APPEND WSJCPP_SOURCES "./src/unit_tests.cpp")
3131
list (APPEND WSJCPP_SOURCES "./src/unit_test_simple_arguments.h")
3232
list (APPEND WSJCPP_SOURCES "./src/unit_test_simple_arguments.cpp")
33+
list (APPEND WSJCPP_SOURCES "./src/unit_test_arguments_with_params.h")
34+
list (APPEND WSJCPP_SOURCES "./src/unit_test_arguments_with_params.cpp")
3335

3436
include_directories(${WSJCPP_INCLUDE_DIRS})
3537

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#include "unit_test_arguments_with_params.h"
2+
#include <vector>
3+
#include <iostream>
4+
#include <wsjcpp_core.h>
5+
#include <wsjcpp_arguments.h>
6+
7+
REGISTRY_UNIT_TEST(UnitTestArgumentsWithParams)
8+
9+
UnitTestArgumentsWithParams::UnitTestArgumentsWithParams()
10+
: UnitTestBase("UnitTestArgumentsWithParams") {
11+
//
12+
}
13+
14+
// ---------------------------------------------------------------------
15+
16+
void UnitTestArgumentsWithParams::init() {
17+
// nothing
18+
}
19+
20+
// ---------------------------------------------------------------------
21+
22+
class ArgumentProcessorUninstall : public WSJCppArgumentProcessor {
23+
public:
24+
ArgumentProcessorUninstall() : WSJCppArgumentProcessor("uninstall", "uninstall something") {
25+
TAG = "ArgumentProcessorUninstall";
26+
};
27+
28+
virtual int handle(const std::string &sProgramName, const std::vector<std::string> &vSubParams) {
29+
if (vSubParams.size() != 3) {
30+
WSJCppLog::err(TAG, "Expected 3 args");
31+
return -1;
32+
}
33+
34+
if (sProgramName != "./program") {
35+
WSJCppLog::err(TAG, "sProgramName expected as './program'");
36+
return -1;
37+
}
38+
39+
if (vSubParams[0] != "install") {
40+
WSJCppLog::err(TAG, "vSubParams[0] expected as 'uninstall'");
41+
return -1;
42+
}
43+
44+
if (vSubParams[1] != "1") {
45+
WSJCppLog::err(TAG, "vSubParams[1] expected as '1'");
46+
return -1;
47+
}
48+
49+
if (vSubParams[2] != "2") {
50+
WSJCppLog::err(TAG, "vSubParams[2] expected as '2'");
51+
return -1;
52+
}
53+
return 0;
54+
}
55+
56+
virtual bool canHandle(const std::vector<std::string> &vSubParams) {
57+
if (vSubParams.size() > 0 && vSubParams[0] == "uninstall") {
58+
return true;
59+
}
60+
return false;
61+
}
62+
63+
private:
64+
std::string TAG;
65+
};
66+
67+
// ---------------------------------------------------------------------
68+
69+
bool UnitTestArgumentsWithParams::run() {
70+
bool bTestSuccess = true;
71+
72+
const int argc1 = 5;
73+
const char *argv1[argc1] = {"./program", "-v", "uninstall", "1", "2"};
74+
WSJCppArguments args1(argc1, argv1);
75+
args1.getRoot().registryProcessor(new ArgumentProcessorUninstall());
76+
args1.getRoot().registrySingleArgument("-v", "verbose");
77+
78+
79+
compareB(bTestSuccess, "canHandle-1", args1.canHandle(), true);
80+
compareN(bTestSuccess, "handle-1", args1.handle(), 0);
81+
82+
const int argc2 = 4;
83+
const char *argv2[argc2] = {"./program2", "some", "1", "2"};
84+
WSJCppArguments args2(argc2, argv2);
85+
compareB(bTestSuccess, "canHandle-2", args2.canHandle(), false);
86+
compareN(bTestSuccess, "handle-2", args2.handle(), -1);
87+
88+
89+
return bTestSuccess;
90+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#ifndef UNIT_TEST_AURGUMENTS_WITH_PARAMS_H
2+
#define UNIT_TEST_AURGUMENTS_WITH_PARAMS_H
3+
4+
#include <unit_tests.h>
5+
6+
class UnitTestArgumentsWithParams : public UnitTestBase {
7+
public:
8+
UnitTestArgumentsWithParams();
9+
virtual void init();
10+
virtual bool run();
11+
};
12+
13+
#endif // #ifndef UNIT_TEST_AURGUMENTS_WITH_PARAMS_H

unit-tests/src/unit_test_simple_arguments.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ void UnitTestSimpleArguments::init() {
2121

2222
class ArgumentProcessorInstall : public WSJCppArgumentProcessor {
2323
public:
24-
ArgumentProcessorInstall() {
24+
ArgumentProcessorInstall() : WSJCppArgumentProcessor("install", "install something") {
2525
TAG = "ArgumentProcessorInstall";
2626
};
2727

@@ -72,7 +72,7 @@ bool UnitTestSimpleArguments::run() {
7272
const int argc1 = 4;
7373
const char *argv1[argc1] = {"./program", "install", "1", "2"};
7474
WSJCppArguments args1(argc1, argv1);
75-
args1.addProcessor(new ArgumentProcessorInstall());
75+
args1.getRoot().registryProcessor(new ArgumentProcessorInstall());
7676

7777
compareB(bTestSuccess, "canHandle-1", args1.canHandle(), true);
7878
compareN(bTestSuccess, "handle-1", args1.handle(), 0);

0 commit comments

Comments
 (0)