drogon_ctl add help command

This commit is contained in:
an-tao 2018-05-28 18:29:14 +08:00
parent 56542a68d2
commit aea79fd9a8
6 changed files with 56 additions and 2 deletions

View File

@ -8,5 +8,6 @@ class CommandHandler:public virtual drogon::DrObjectBase {
public:
virtual void handleCommand(const std::vector<std::string> &parameters)=0;
virtual std::string script(){return "";}
virtual ~CommandHandler(){}
};

29
drogon_ctl/help.cc Executable file
View File

@ -0,0 +1,29 @@
#include "help.h"
#include <drogon/DrClassMap.h>
#include <iostream>
#include <memory>
using namespace drogon_ctl;
void help::handleCommand(const std::vector<std::string> &parameters)
{
std::cout<<"usage: drogon_ctl <command> [<args>]"<<std::endl;
std::cout<<"commands list:"<<std::endl;
for(auto className:drogon::DrClassMap::getAllClassName())
{
auto classPtr=std::shared_ptr<DrObjectBase>(drogon::DrClassMap::newObject(className));
if(classPtr)
{
auto cmdHdlPtr=std::dynamic_pointer_cast<CommandHandler>(classPtr);
if(cmdHdlPtr)
{
auto pos=className.rfind("::");
if(pos!=std::string::npos)
{
className=className.substr(pos+2);
}
while(className.length()<24)
className.append(" ");
std::cout<<className<<cmdHdlPtr->script()<<std::endl;
}
}
}
}

14
drogon_ctl/help.h Executable file
View File

@ -0,0 +1,14 @@
#pragma once
#include <drogon/DrObject.h>
#include "CommandHandler.h"
using namespace drogon;
namespace drogon_ctl
{
class help:public DrObject<help>,public CommandHandler
{
public:
void handleCommand(const std::vector<std::string> &parameters);
std::string script(){return "display this message";}
};
}

View File

@ -9,6 +9,7 @@ namespace drogon_ctl
{
public:
void handleCommand(const std::vector<std::string> &parameters);
std::string script(){return "display version of this tool";}
version(){}
};
}

View File

@ -27,9 +27,10 @@ class DrClassMap
public:
static void registerClass(const std::string &className,const DrAllocFunc &func);
static DrObjectBase* newObject(const std::string &className);
static std::vector<std::string> getAllClassName();
protected:
static std::map <std::string,DrAllocFunc> &getMap();
};
}
#endif /* DrClassMap_hpp */

View File

@ -27,7 +27,15 @@ DrObjectBase* DrClassMap::newObject(const std::string &className)
else
return nullptr;
}
std::vector<std::string> DrClassMap::getAllClassName()
{
std::vector<std::string> ret;
for(auto iter:getMap())
{
ret.push_back(iter.first);
}
return ret;
}
std::map <std::string,DrAllocFunc> & DrClassMap::getMap() {
static std::map <std::string,DrAllocFunc> map;
return map;