From c1322fb9461ad14241271251dd3551afd7fc5bc8 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Wed, 10 Sep 2008 04:46:03 +0200 Subject: Help-Kommando implementiert --- src/Client/CommandParser.cpp | 69 ++++++++++++++++++++++++++++++++++---------- src/Client/CommandParser.h | 19 +++++++++++- 2 files changed, 71 insertions(+), 17 deletions(-) (limited to 'src/Client') diff --git a/src/Client/CommandParser.cpp b/src/Client/CommandParser.cpp index ebf4b2c..2cd80be 100644 --- a/src/Client/CommandParser.cpp +++ b/src/Client/CommandParser.cpp @@ -26,6 +26,52 @@ namespace Mad { namespace Client { +const CommandParser::Command CommandParser::commands[] = { + {"help", "help [command]", "Displays usage information about commands", "Displays usage information about a command. If no command is given, a list of all available commands is displayed.", &CommandParser::helpCommand}, + {"status", "status", "Displays server status information", "Displays server status information, like uptime.", &CommandParser::statusCommand}, + {"quit", "quit", "Closes the connection and quits the client", "Closes the connection and quits the client.", &CommandParser::quitCommand}, + {0, 0, 0, 0, 0} +}; + +void CommandParser::helpCommand(const std::vector &args) { + if(args.size() == 1) { + std::cout << "Available commands:" << std::endl << std::endl; + + for(int i = 0; commands[i].command != 0; ++i) { + std::cout << commands[i].command << std::endl; + std::cout << "\t" << commands[i].desc << std::endl; + } + } + else if(args.size() == 2) { + for(int i = 0; commands[i].command != 0; ++i) { + if(args[1] == commands[i].command) { + std::cout << "Usage: " << commands[i].cmdline << std::endl << std::endl; + std::cout << commands[i].longdesc << std::endl; + return; + } + } + + std::cerr << args[0] << ": Command '" << args[1] << "' doesn't exist." << std::endl; + } + else { + std::cerr << args[0] << ": Too many arguments." << std::endl; + std::cerr << "Usage: help [command]" << std::endl; + } +} + +void CommandParser::statusCommand(const std::vector&) { + activeRequests++; + + Common::Request::CoreStatusRequest::send(connection, *requestManager, sigc::mem_fun(this, &CommandParser::requestFinished)); +} + +void CommandParser::quitCommand(const std::vector&) { + activeRequests++; + disconnect = true; + + Common::Request::DisconnectRequest::send(connection, *requestManager, sigc::mem_fun(this, &CommandParser::requestFinished)); +} + bool CommandParser::split(const std::string &str, std::vector &ret) { std::string temp; bool quoteSingle = false, quoteDouble = false, escape = false; @@ -91,25 +137,16 @@ bool CommandParser::parse(const std::string &cmd) { if(splitCmd.empty()) return true; - if(splitCmd[0] == "quit") { - requestDisconnect(); - } - else if(splitCmd[0] == "status") { - activeRequests++; - Common::Request::CoreStatusRequest::send(connection, *requestManager, sigc::mem_fun(this, &CommandParser::requestFinished)); - } - else { - std::cerr << "Unknown command \"" << splitCmd[0] << "\"." << std::endl; + for(int i = 0; commands[i].command != 0; ++i) { + if(splitCmd[0] == commands[i].command) { + (this->*commands[i].funcPtr)(splitCmd); + return true; + } } - return true; -} + std::cerr << "Unknown command '" << splitCmd[0] << "'." << std::endl; -void CommandParser::requestDisconnect() { - activeRequests++; - disconnect = true; - - Common::Request::DisconnectRequest::send(connection, *requestManager, sigc::mem_fun(this, &CommandParser::requestFinished)); + return true; } } diff --git a/src/Client/CommandParser.h b/src/Client/CommandParser.h index 81c3289..8b3cc69 100644 --- a/src/Client/CommandParser.h +++ b/src/Client/CommandParser.h @@ -38,6 +38,17 @@ namespace Client { class CommandParser { private: + struct Command { + const char* command; + const char* cmdline; + const char* desc; + const char* longdesc; + + void (CommandParser::*funcPtr) (const std::vector &args); + }; + + static const Command commands[]; + sigc::signal finished; Common::RequestManager *requestManager; @@ -54,6 +65,10 @@ class CommandParser { finished(); } + void helpCommand(const std::vector &args); + void statusCommand(const std::vector&); + void quitCommand(const std::vector&); + public: CommandParser(Common::RequestManager *requestManager0, Net::Connection *connection0) : requestManager(requestManager0), connection(connection0), activeRequests(0), disconnect(false) {} @@ -62,7 +77,9 @@ class CommandParser { bool parse(const std::string &cmd); - void requestDisconnect(); + void requestDisconnect() { + quitCommand(std::vector()); + } sigc::signal signalFinished() const {return finished;} }; -- cgit v1.2.3