Skip to content

Commit 5dd2374

Browse files
committed
Fixed #5 No show 'Subcommand. Try help for more.' when no options
1 parent 8c62b30 commit 5dd2374

8 files changed

+72
-44
lines changed

src/ArgumentProcessors/argument_processor_main.cpp

+19-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ ArgumentProcessorMain::ArgumentProcessorMain()
1414
registryParameterArgument("-param2", "P", "Param 2");
1515
registryExample("wsjcpp --single1 -param1 1");
1616
registryProcessor(new ArgumentProcessorSubcommand1());
17+
registryProcessor(new ArgumentProcessorNothing());
1718
}
1819

1920
// ---------------------------------------------------------------------
@@ -41,7 +42,6 @@ int ArgumentProcessorMain::exec(const std::vector<std::string> &vRoutes, const s
4142
return -1;
4243
}
4344

44-
4545
// ---------------------------------------------------------------------
4646
// ArgumentProcessorSubcommand1
4747

@@ -97,3 +97,21 @@ int ArgumentProcessorSubcommand1::exec(const std::vector<std::string> &vRoutes,
9797
return 0;
9898
}
9999

100+
// ---------------------------------------------------------------------
101+
// ArgumentProcessorNothing
102+
103+
ArgumentProcessorNothing::ArgumentProcessorNothing()
104+
: WsjcppArgumentProcessor(
105+
{"nothing", "n"},
106+
"nothing",
107+
"Example of nothing with long description must be here 12345678901234567890 lol again"
108+
) {
109+
TAG = "ArgumentProcessorSubcommand1";
110+
}
111+
112+
// ---------------------------------------------------------------------
113+
114+
int ArgumentProcessorNothing::exec(const std::vector<std::string> &vRoutes, const std::vector<std::string> &vSubParams) {
115+
std::cout << "Nothing" << std::endl;
116+
return 0;
117+
}

src/ArgumentProcessors/argument_processor_main.h

+6
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,10 @@ class ArgumentProcessorSubcommand1 : public WsjcppArgumentProcessor {
2626
int m_nTimesTest;
2727
};
2828

29+
class ArgumentProcessorNothing : public WsjcppArgumentProcessor {
30+
public:
31+
ArgumentProcessorNothing();
32+
virtual int exec(const std::vector<std::string> &vRoutes, const std::vector<std::string> &vSubParams) override;
33+
};
34+
2935
#endif // ARGUMENT_PROCESSOR_MAIN_H

src/wsjcpp_arguments.cpp

+25-7
Original file line numberDiff line numberDiff line change
@@ -293,20 +293,38 @@ int WsjcppArgumentProcessor::help(
293293

294294
for (int i = 0; i < m_vProcessors.size(); i++) {
295295
WsjcppArgumentProcessor *pProc = m_vProcessors[i];
296-
297-
std::cout
298-
<< " " << pProc->getNamesAsString() << " [<options>...]"
299-
<< std::endl
300-
<< " Subcommand. Try help for more. " << pProc->getDescription()
301-
<< std::endl
302-
<< std::endl;
296+
std::cout << " " << pProc->getNamesAsString();
297+
298+
if (pProc->hasMoreOptions()) {
299+
std::cout
300+
<< " [<options>...]"
301+
<< std::endl
302+
<< " Subcommand. Try help for more. " << pProc->getDescription()
303+
<< std::endl
304+
;
305+
} else {
306+
std::cout
307+
<< std::endl
308+
<< " " << pProc->getDescription()
309+
<< std::endl
310+
;
311+
}
312+
std::cout << std::endl;
303313
}
304314
}
305315
return 0;
306316
}
307317

308318
// ---------------------------------------------------------------------
309319

320+
bool WsjcppArgumentProcessor::hasMoreOptions() {
321+
return m_vProcessors.size() > 0
322+
|| m_vSingleArguments.size() > 0
323+
|| m_vParameterArguments.size() > 0;
324+
}
325+
326+
// ---------------------------------------------------------------------
327+
310328
bool WsjcppArgumentProcessor::applySingleArgument(const std::string &sProgramName, const std::string &sArgumentName) {
311329
WsjcppLog::throw_err(TAG, "No support single argument '" + sArgumentName + "'");
312330
return false;

src/wsjcpp_arguments.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,14 @@ class WsjcppArgumentProcessor {
6767
WsjcppArgumentParameter *findRegisteredParameterArgument(const std::string &sArgumentName);
6868

6969
bool hasRegisteredArgumentName(const std::string &sArgumentName);
70-
70+
7171
bool getValueOfParam(const std::string &sArgumentName);
7272
int help(
7373
const std::vector<std::string> &vRoutes,
7474
const std::vector<std::string> &vSubParams
7575
);
76-
76+
bool hasMoreOptions();
77+
7778
virtual bool applySingleArgument(const std::string &sProgramName, const std::string &sArgumentName);
7879
virtual bool applyParameterArgument(const std::string &sProgramName, const std::string &sArgumentName, const std::string &sValue);
7980
virtual int exec(const std::vector<std::string> &vRoutes, const std::vector<std::string> &vSubParams);

unit-tests.wsjcpp/src/unit_test_arguments_with_params.cpp

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "unit_test_arguments_with_params.h"
1+
#include <wsjcpp_unit_tests.h>
22
#include <vector>
33
#include <wsjcpp_core.h>
44
#include <wsjcpp_arguments.h>
@@ -52,6 +52,15 @@ class ArgumentProcessorUninstall : public WsjcppArgumentProcessor {
5252
};
5353

5454
// ---------------------------------------------------------------------
55+
// UnitTestArgumentsWithParams
56+
57+
class UnitTestArgumentsWithParams : public WsjcppUnitTestBase {
58+
public:
59+
UnitTestArgumentsWithParams();
60+
virtual bool doBeforeTest() override;
61+
virtual void executeTest() override;
62+
virtual bool doAfterTest() override;
63+
};
5564

5665
REGISTRY_WSJCPP_UNIT_TEST(UnitTestArgumentsWithParams)
5766

unit-tests.wsjcpp/src/unit_test_arguments_with_params.h

-16
This file was deleted.

unit-tests.wsjcpp/src/unit_test_simple_arguments.cpp

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "unit_test_simple_arguments.h"
1+
#include <wsjcpp_unit_tests.h>
22
#include <vector>
33
#include <wsjcpp_core.h>
44
#include <wsjcpp_arguments.h>
@@ -171,6 +171,14 @@ int ArgumentProcessorProgram1::exec(const std::vector<std::string> &vRoutes, con
171171
// ---------------------------------------------------------------------
172172
// UnitTestSimpleArguments
173173

174+
class UnitTestSimpleArguments : public WsjcppUnitTestBase {
175+
public:
176+
UnitTestSimpleArguments();
177+
virtual bool doBeforeTest() override;
178+
virtual void executeTest() override;
179+
virtual bool doAfterTest() override;
180+
};
181+
174182
REGISTRY_WSJCPP_UNIT_TEST(UnitTestSimpleArguments)
175183

176184
UnitTestSimpleArguments::UnitTestSimpleArguments()

unit-tests.wsjcpp/src/unit_test_simple_arguments.h

-16
This file was deleted.

0 commit comments

Comments
 (0)