11#include " wsjcpp_arguments.h"
22#include < wsjcpp_core.h>
33
4+ // ---------------------------------------------------------------------
5+ // WsjcppArgumentSingle
6+
47WsjcppArgumentSingle::WsjcppArgumentSingle (const std::string &sName , const std::string &sDescription ) {
58 TAG = " WsjcppArgumentSingle-" + sName ;
69 m_sName = sName ;
@@ -20,6 +23,7 @@ std::string WsjcppArgumentSingle::getDescription() {
2023}
2124
2225// ---------------------------------------------------------------------
26+ // WsjcppArgumentParameter
2327
2428WsjcppArgumentParameter::WsjcppArgumentParameter (
2529 const std::string &sName ,
@@ -63,6 +67,7 @@ void WsjcppArgumentParameter::setValue(const std::string &sValue) {
6367}
6468
6569// ---------------------------------------------------------------------
70+ // WsjcppArgumentProcessor
6671
6772WsjcppArgumentProcessor::WsjcppArgumentProcessor (
6873 const std::vector<std::string> &vNames,
@@ -293,20 +298,38 @@ int WsjcppArgumentProcessor::help(
293298
294299 for (int i = 0 ; i < m_vProcessors.size (); i++) {
295300 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;
303318 }
304319 }
305320 return 0 ;
306321}
307322
308323// ---------------------------------------------------------------------
309324
325+ bool WsjcppArgumentProcessor::hasMoreOptions () {
326+ return m_vProcessors.size () > 0
327+ || m_vSingleArguments.size () > 0
328+ || m_vParameterArguments.size () > 0 ;
329+ }
330+
331+ // ---------------------------------------------------------------------
332+
310333bool WsjcppArgumentProcessor::applySingleArgument (const std::string &sProgramName , const std::string &sArgumentName ) {
311334 WsjcppLog::throw_err (TAG, " No support single argument '" + sArgumentName + " '" );
312335 return false ;
@@ -315,18 +338,19 @@ bool WsjcppArgumentProcessor::applySingleArgument(const std::string &sProgramNam
315338// ---------------------------------------------------------------------
316339
317340bool 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 () + " '" );
319342 return false ;
320343}
321344
322345// ---------------------------------------------------------------------
323346
324347int 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 ;
327350}
328351
329352// ---------------------------------------------------------------------
353+ // WsjcppArguments
330354
331355WsjcppArguments::WsjcppArguments (int argc, const char * argv[], WsjcppArgumentProcessor *pRoot) {
332356 TAG = " WsjcppArguments" ;
@@ -336,12 +360,21 @@ WsjcppArguments::WsjcppArguments(int argc, const char* argv[], WsjcppArgumentPro
336360 m_sProgramName = m_vArguments[0 ];
337361 m_vArguments.erase (m_vArguments.begin ());
338362 m_pRoot = pRoot;
363+ m_bEnablePrintAutoHelp = true ;
339364}
340365
341366// ---------------------------------------------------------------------
342367
343368WsjcppArguments::~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;
345378}
346379
347380// ---------------------------------------------------------------------
@@ -398,8 +431,12 @@ int WsjcppArguments::recursiveExec(
398431 if (vSubArguments.size () > 0 && vSubArguments[0 ] == " help" ) {
399432 return pArgumentProcessor->help (vRoutes, vSubArguments);
400433 }
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;
403440}
404441
405442// ---------------------------------------------------------------------
0 commit comments