1
1
#include " wsjcpp_arguments.h"
2
2
#include < wsjcpp_core.h>
3
3
4
+ // ---------------------------------------------------------------------
5
+ // WsjcppArgumentSingle
6
+
4
7
WsjcppArgumentSingle::WsjcppArgumentSingle (const std::string &sName , const std::string &sDescription ) {
5
8
TAG = " WsjcppArgumentSingle-" + sName ;
6
9
m_sName = sName ;
@@ -20,6 +23,7 @@ std::string WsjcppArgumentSingle::getDescription() {
20
23
}
21
24
22
25
// ---------------------------------------------------------------------
26
+ // WsjcppArgumentParameter
23
27
24
28
WsjcppArgumentParameter::WsjcppArgumentParameter (
25
29
const std::string &sName ,
@@ -63,6 +67,7 @@ void WsjcppArgumentParameter::setValue(const std::string &sValue) {
63
67
}
64
68
65
69
// ---------------------------------------------------------------------
70
+ // WsjcppArgumentProcessor
66
71
67
72
WsjcppArgumentProcessor::WsjcppArgumentProcessor (
68
73
const std::vector<std::string> &vNames,
@@ -293,20 +298,38 @@ int WsjcppArgumentProcessor::help(
293
298
294
299
for (int i = 0 ; i < m_vProcessors.size (); i++) {
295
300
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;
301
+ std::cout << " " << pProc->getNamesAsString ();
302
+
303
+ if (pProc->hasMoreOptions ()) {
304
+ std::cout
305
+ << " [<options>...]"
306
+ << std::endl
307
+ << " Subcommand. Try help for more. " << pProc->getDescription ()
308
+ << std::endl
309
+ ;
310
+ } else {
311
+ std::cout
312
+ << std::endl
313
+ << " " << pProc->getDescription ()
314
+ << std::endl
315
+ ;
316
+ }
317
+ std::cout << std::endl;
303
318
}
304
319
}
305
320
return 0 ;
306
321
}
307
322
308
323
// ---------------------------------------------------------------------
309
324
325
+ bool WsjcppArgumentProcessor::hasMoreOptions () {
326
+ return m_vProcessors.size () > 0
327
+ || m_vSingleArguments.size () > 0
328
+ || m_vParameterArguments.size () > 0 ;
329
+ }
330
+
331
+ // ---------------------------------------------------------------------
332
+
310
333
bool WsjcppArgumentProcessor::applySingleArgument (const std::string &sProgramName , const std::string &sArgumentName ) {
311
334
WsjcppLog::throw_err (TAG, " No support single argument '" + sArgumentName + " '" );
312
335
return false ;
@@ -315,18 +338,19 @@ bool WsjcppArgumentProcessor::applySingleArgument(const std::string &sProgramNam
315
338
// ---------------------------------------------------------------------
316
339
317
340
bool WsjcppArgumentProcessor::applyParameterArgument (const std::string &sProgramName , const std::string &sArgumentName , const std::string &sValue ) {
318
- WsjcppLog::throw_err (TAG, " No support parameter argument '" + sArgumentName + " ' for '" + getNamesAsString () + " '" );
341
+ WsjcppLog::err (TAG, " No support parameter argument '" + sArgumentName + " ' for '" + getNamesAsString () + " '" );
319
342
return false ;
320
343
}
321
344
322
345
// ---------------------------------------------------------------------
323
346
324
347
int WsjcppArgumentProcessor::exec (const std::vector<std::string> &vRoutes, const std::vector<std::string> &vSubParams) {
325
- WsjcppLog::throw_err (TAG, " Processor '" + getNamesAsString () + " ' has not implementation" );
326
- return -1 ;
348
+ WsjcppLog::err (TAG, " Processor '" + getNamesAsString () + " ' has not implementation" );
349
+ return -10 ;
327
350
}
328
351
329
352
// ---------------------------------------------------------------------
353
+ // WsjcppArguments
330
354
331
355
WsjcppArguments::WsjcppArguments (int argc, const char * argv[], WsjcppArgumentProcessor *pRoot) {
332
356
TAG = " WsjcppArguments" ;
@@ -336,12 +360,21 @@ WsjcppArguments::WsjcppArguments(int argc, const char* argv[], WsjcppArgumentPro
336
360
m_sProgramName = m_vArguments[0 ];
337
361
m_vArguments.erase (m_vArguments.begin ());
338
362
m_pRoot = pRoot;
363
+ m_bEnablePrintAutoHelp = true ;
339
364
}
340
365
341
366
// ---------------------------------------------------------------------
342
367
343
368
WsjcppArguments::~WsjcppArguments () {
344
- // TODO
369
+ for (int i = 0 ; i < m_vProcessors.size (); i++) {
370
+ delete m_vProcessors[i];
371
+ }
372
+ }
373
+
374
+ // ---------------------------------------------------------------------
375
+
376
+ void WsjcppArguments::enablePrintAutoHelp (bool bEnablePrintAutoHelp) {
377
+ m_bEnablePrintAutoHelp = bEnablePrintAutoHelp;
345
378
}
346
379
347
380
// ---------------------------------------------------------------------
@@ -398,8 +431,12 @@ int WsjcppArguments::recursiveExec(
398
431
if (vSubArguments.size () > 0 && vSubArguments[0 ] == " help" ) {
399
432
return pArgumentProcessor->help (vRoutes, vSubArguments);
400
433
}
401
-
402
- return pArgumentProcessor->exec (vRoutes, vSubArguments);
434
+
435
+ int nResult = pArgumentProcessor->exec (vRoutes, vSubArguments);
436
+ if (nResult == -10 && m_bEnablePrintAutoHelp) {
437
+ pArgumentProcessor->help (vRoutes, vSubArguments);
438
+ }
439
+ return nResult;
403
440
}
404
441
405
442
// ---------------------------------------------------------------------
0 commit comments