int TCommandHandler::TryExec(const std::string &commandName, const std::vector<std::string> &commandArgs) { const auto it = Commands.find(commandName); if (it == Commands.end()) { std::cerr << "Invalid command " << commandName << "!" << std::endl; return EXIT_FAILURE; } ICmd *cmd = it->second.get(); PORTO_ASSERT(cmd); if (!cmd->ValidArgs(commandArgs)) { Usage(cmd->GetName().c_str()); return EXIT_FAILURE; } CurrentCmd = cmd; // in case client closes pipe we are writing to in the protobuf code (void)RegisterSignal(SIGPIPE, SIG_IGN); (void)RegisterSignal(SIGINT, SigInt); (void)RegisterSignal(SIGTERM, SigInt); TCommandEnviroment commandEnv{*this, commandArgs}; int ret = cmd->Execute(&commandEnv); if (cmd->GotSignal()) return -cmd->InterruptedSignal; else return ret; }
static int TryExec(int argc, char *argv[]) { string name(argv[1]); if (commands.find(name) == commands.end()) { std::cerr << "Invalid command " << name << "!" << std::endl; return EXIT_FAILURE; } ICmd *cmd = commands[name]; if (!cmd->ValidArgs(argc - 2, argv + 2)) { Usage(cmd->GetName().c_str()); return EXIT_FAILURE; } CurrentCmd = cmd; // in case client closes pipe we are writing to in the protobuf code (void)RegisterSignal(SIGPIPE, SIG_IGN); (void)RegisterSignal(SIGINT, SigInt); (void)RegisterSignal(SIGTERM, SigInt); int ret = cmd->Execute(argc - 2, argv + 2); if (cmd->GotSignal()) return -cmd->InterruptedSignal; else return ret; }
void TCommandHandler::Usage(const char *command) { ICmd *cmd = Commands["help"].get(); PORTO_ASSERT(cmd); std::vector<std::string> args; if (command) args.push_back(command); TCommandEnviroment commandEnv{*this, args}; cmd->Execute(&commandEnv); }
BOOL CommandManager::Execute( LPCTSTR cmdline, tstring& replyText ) { //分割并整理命令行的各个部分 TStringVector parts; splitByChar(cmdline, parts, ' '); TStringVector::iterator iter = parts.begin(); while (iter != parts.end()) { tstring& part = *iter; trim(part); if (part.size() == 0) { iter = parts.erase(iter); } else { iter++; } } //检查是否有可用的部分 if (parts.size() == 0) { replyText = _T("invalid command."); return FALSE; } //查找可用的命令 tstring& cmdname = parts[0]; makeLower(cmdname); CommandMap::iterator cmdIter = m_cmdMap.find(cmdname); if (cmdIter == m_cmdMap.end()) { replyText = _T("no such command."); return FALSE; } //执行命令 ICmd* pCmd = cmdIter->second; BOOL bExecuteOK = pCmd->Execute(parts, replyText, m_env); return bExecuteOK; }
int TCommandHandler::TryExec(const std::string &commandName, const std::vector<std::string> &commandArgs) { const auto it = Commands.find(commandName); if (it == Commands.end()) { std::cerr << "Invalid command " << commandName << "!" << std::endl; return EXIT_FAILURE; } ICmd *cmd = it->second.get(); PORTO_ASSERT(cmd); if (!cmd->ValidArgs(commandArgs)) { Usage(cmd->GetName().c_str()); return EXIT_FAILURE; } // in case client closes pipe we are writing to in the protobuf code Signal(SIGPIPE, SIG_IGN); TCommandEnviroment commandEnv{*this, commandArgs}; commandEnv.NeedArgs = cmd->NeedArgs; return cmd->Execute(&commandEnv); }
static void Usage(const char *command) { ICmd *cmd = commands["help"]; char *argv[] = { (char *)command, NULL }; cmd->Execute(command ? 1 : 0, argv); }