diff options
author | Matthias Schiffer <matthias@gamezock.de> | 2008-09-10 06:10:21 +0200 |
---|---|---|
committer | Matthias Schiffer <matthias@gamezock.de> | 2008-09-10 06:10:21 +0200 |
commit | 55a6045504bee4bf425870b6b427abd5240e7303 (patch) | |
tree | 43cb314ba77dda958c4ca45ac2e2bd2417898353 | |
parent | c1322fb9461ad14241271251dd3551afd7fc5bc8 (diff) | |
download | mad-55a6045504bee4bf425870b6b427abd5240e7303.tar mad-55a6045504bee4bf425870b6b427abd5240e7303.zip |
Einige Verbesserungen am Kommando-Parser
-rw-r--r-- | src/Client/CommandParser.cpp | 68 | ||||
-rw-r--r-- | src/Client/CommandParser.h | 9 |
2 files changed, 50 insertions, 27 deletions
diff --git a/src/Client/CommandParser.cpp b/src/Client/CommandParser.cpp index 2cd80be..4faec95 100644 --- a/src/Client/CommandParser.cpp +++ b/src/Client/CommandParser.cpp @@ -27,35 +27,57 @@ 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} + {{"help", "?", 0}, "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", "st", 0}, "status", "Displays server status information", "Displays server status information.", &CommandParser::statusCommand}, + {{"exit", "quit", 0}, "exit", "Closes the connection and quits the client", "Closes the connection and quits the client.", &CommandParser::exitCommand}, + {{0}, 0, 0, 0, 0} }; +const CommandParser::Command* CommandParser::findCommand(const std::string& command) { + for(int i = 0; commands[i].commands[0] != 0; ++i) { + for(int j = 0; commands[i].commands[j] != 0; ++j) { + if(command == commands[i].commands[j]) { + return &commands[i]; + } + } + } + + return 0; +} + +void CommandParser::printUsage(const std::string& command) { + const CommandParser::Command *cmd = findCommand(command); + + if(cmd) + std::cerr << "Usage: " << cmd->cmdline << std::endl; +} + 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; + for(int i = 0; commands[i].commands[0] != 0; ++i) { + std::cout << commands[i].commands[0]; + + for(int j = 1; commands[i].commands[j] != 0; ++j) + std::cout << ", " << commands[i].commands[j]; + + std::cout << std::endl << "\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; - } - } + const Command* command = findCommand(args[1]); - std::cerr << args[0] << ": Command '" << args[1] << "' doesn't exist." << std::endl; + if(command) { + std::cout << "Usage: " << command->cmdline << std::endl << std::endl; + std::cout << command->longdesc << std::endl; + } + else + 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; + printUsage("help"); } } @@ -65,7 +87,7 @@ void CommandParser::statusCommand(const std::vector<std::string>&) { Common::Request::CoreStatusRequest::send(connection, *requestManager, sigc::mem_fun(this, &CommandParser::requestFinished)); } -void CommandParser::quitCommand(const std::vector<std::string>&) { +void CommandParser::exitCommand(const std::vector<std::string>&) { activeRequests++; disconnect = true; @@ -137,14 +159,12 @@ bool CommandParser::parse(const std::string &cmd) { if(splitCmd.empty()) return true; - for(int i = 0; commands[i].command != 0; ++i) { - if(splitCmd[0] == commands[i].command) { - (this->*commands[i].funcPtr)(splitCmd); - return true; - } - } + const Command* command = findCommand(splitCmd[0]); - std::cerr << "Unknown command '" << splitCmd[0] << "'." << std::endl; + if(command) + (this->*command->funcPtr)(splitCmd); + else + std::cerr << "Unknown command '" << splitCmd[0] << "'." << std::endl; return true; } diff --git a/src/Client/CommandParser.h b/src/Client/CommandParser.h index 8b3cc69..891323f 100644 --- a/src/Client/CommandParser.h +++ b/src/Client/CommandParser.h @@ -39,7 +39,7 @@ namespace Client { class CommandParser { private: struct Command { - const char* command; + const char* commands[3]; const char* cmdline; const char* desc; const char* longdesc; @@ -65,9 +65,12 @@ class CommandParser { finished(); } + const Command* findCommand(const std::string& command); + void printUsage(const std::string& command); + void helpCommand(const std::vector<std::string> &args); void statusCommand(const std::vector<std::string>&); - void quitCommand(const std::vector<std::string>&); + void exitCommand(const std::vector<std::string>&); public: CommandParser(Common::RequestManager *requestManager0, Net::Connection *connection0) : requestManager(requestManager0), connection(connection0), activeRequests(0), disconnect(false) {} @@ -78,7 +81,7 @@ class CommandParser { bool parse(const std::string &cmd); void requestDisconnect() { - quitCommand(std::vector<std::string>()); + exitCommand(std::vector<std::string>()); } sigc::signal<void> signalFinished() const {return finished;} |