diff options
author | Matthias Schiffer <matthias@gamezock.de> | 2008-09-10 04:46:03 +0200 |
---|---|---|
committer | Matthias Schiffer <matthias@gamezock.de> | 2008-09-10 04:46:03 +0200 |
commit | c1322fb9461ad14241271251dd3551afd7fc5bc8 (patch) | |
tree | a6cf12166a003cfd8c4e6d1fe1f137fbcba21f0c /src | |
parent | 707344b4d161cb5a11e7f78d7622cb7cd1a791f6 (diff) | |
download | mad-c1322fb9461ad14241271251dd3551afd7fc5bc8.tar mad-c1322fb9461ad14241271251dd3551afd7fc5bc8.zip |
Help-Kommando implementiert
Diffstat (limited to 'src')
-rw-r--r-- | src/Client/CommandParser.cpp | 69 | ||||
-rw-r--r-- | src/Client/CommandParser.h | 19 |
2 files changed, 71 insertions, 17 deletions
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<std::string> &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<std::string>&) { + activeRequests++; + + Common::Request::CoreStatusRequest::send(connection, *requestManager, sigc::mem_fun(this, &CommandParser::requestFinished)); +} + +void CommandParser::quitCommand(const std::vector<std::string>&) { + activeRequests++; + disconnect = true; + + Common::Request::DisconnectRequest::send(connection, *requestManager, sigc::mem_fun(this, &CommandParser::requestFinished)); +} + bool CommandParser::split(const std::string &str, std::vector<std::string> &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<std::string> &args); + }; + + static const Command commands[]; + sigc::signal<void> finished; Common::RequestManager *requestManager; @@ -54,6 +65,10 @@ class CommandParser { finished(); } + void helpCommand(const std::vector<std::string> &args); + void statusCommand(const std::vector<std::string>&); + void quitCommand(const std::vector<std::string>&); + 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<std::string>()); + } sigc::signal<void> signalFinished() const {return finished;} }; |