| 1 | /* | 
|---|
| 2 | *   ORXONOX - the hottest 3D action shooter ever to exist | 
|---|
| 3 | *                    > www.orxonox.net < | 
|---|
| 4 | * | 
|---|
| 5 | * | 
|---|
| 6 | *   License notice: | 
|---|
| 7 | * | 
|---|
| 8 | *   This program is free software; you can redistribute it and/or | 
|---|
| 9 | *   modify it under the terms of the GNU General Public License | 
|---|
| 10 | *   as published by the Free Software Foundation; either version 2 | 
|---|
| 11 | *   of the License, or (at your option) any later version. | 
|---|
| 12 | * | 
|---|
| 13 | *   This program is distributed in the hope that it will be useful, | 
|---|
| 14 | *   but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 15 | *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 16 | *   GNU General Public License for more details. | 
|---|
| 17 | * | 
|---|
| 18 | *   You should have received a copy of the GNU General Public License | 
|---|
| 19 | *   along with this program; if not, write to the Free Software | 
|---|
| 20 | *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. | 
|---|
| 21 | * | 
|---|
| 22 | *   Author: | 
|---|
| 23 | *      Reto Grieder | 
|---|
| 24 | *   Co-authors: | 
|---|
| 25 | *      ... | 
|---|
| 26 | * | 
|---|
| 27 | */ | 
|---|
| 28 |  | 
|---|
| 29 | /** | 
|---|
| 30 | @defgroup CmdArgs Commandline arguments | 
|---|
| 31 | @ingroup Config | 
|---|
| 32 | @brief For a reference of all commandline arguments see @ref cmdargspage | 
|---|
| 33 | */ | 
|---|
| 34 |  | 
|---|
| 35 | /** | 
|---|
| 36 | @file | 
|---|
| 37 | @ingroup Config CmdArgs | 
|---|
| 38 | @brief Declaration of CommandLineParser and CommandLineArgument, definition of the SetCommandLineArgument() macros. | 
|---|
| 39 | */ | 
|---|
| 40 |  | 
|---|
| 41 | #ifndef _CommandLine_H__ | 
|---|
| 42 | #define _CommandLine_H__ | 
|---|
| 43 |  | 
|---|
| 44 | #include "core/CorePrereqs.h" | 
|---|
| 45 |  | 
|---|
| 46 | #include <fstream> | 
|---|
| 47 | #include <map> | 
|---|
| 48 | #include "util/OrxAssert.h" | 
|---|
| 49 | #include "util/MultiType.h" | 
|---|
| 50 | #include "util/Singleton.h" | 
|---|
| 51 |  | 
|---|
| 52 | namespace orxonox | 
|---|
| 53 | { | 
|---|
| 54 | /** | 
|---|
| 55 | @brief | 
|---|
| 56 | Container class for a command line argument of any type supported by MultiType. | 
|---|
| 57 |  | 
|---|
| 58 | Whenever you want to have an option specified by a command line switch, | 
|---|
| 59 | you need to first define it with SetCommandLineArgument(name, defaultValue). | 
|---|
| 60 | It is then added to a map and possibly changed when the command line is being parsed. | 
|---|
| 61 | If the option was not given, you can detect this by asking hasDefaultValue(). | 
|---|
| 62 |  | 
|---|
| 63 | There is a possibility to define a short cut so you can write "-p 20" instead of "--port 20". | 
|---|
| 64 | Note the difference between "-" and "--"! | 
|---|
| 65 | Also, there is no restriction to the number of strings you add after --name. | 
|---|
| 66 | So "--startVector {2, 4, 5}" is perfectly legal. | 
|---|
| 67 |  | 
|---|
| 68 | Retrieving an argument is possible with the getCommandLineArgument function of the | 
|---|
| 69 | CommandLineParser class. It is a Singleton, but the public interface is static. | 
|---|
| 70 | */ | 
|---|
| 71 | class _CoreExport CommandLineArgument | 
|---|
| 72 | { | 
|---|
| 73 | friend class CommandLineParser; | 
|---|
| 74 |  | 
|---|
| 75 | public: | 
|---|
| 76 | //! Constructor initialises both value_ and defaultValue_ with defaultValue. | 
|---|
| 77 | CommandLineArgument(const std::string& name, const MultiType& defaultValue) | 
|---|
| 78 | : bHasDefaultValue_(true) | 
|---|
| 79 | , name_(name) | 
|---|
| 80 | , value_(defaultValue) | 
|---|
| 81 | , defaultValue_(defaultValue) | 
|---|
| 82 | { } | 
|---|
| 83 | ~CommandLineArgument() { } | 
|---|
| 84 |  | 
|---|
| 85 | //! Tells whether the value has been changed by the command line. | 
|---|
| 86 | bool hasDefaultValue() const { return bHasDefaultValue_; } | 
|---|
| 87 | //! Returns the name of the argument. | 
|---|
| 88 | const std::string& getName() const { return name_; } | 
|---|
| 89 |  | 
|---|
| 90 | //! Returns the shortcut (example: "-p 22" for "--port 22") of the argument. | 
|---|
| 91 | //! Evaluates to "" if there is none. | 
|---|
| 92 | const std::string& getShortcut() const { return shortcut_; } | 
|---|
| 93 | //! Sets the shortcut for the argument | 
|---|
| 94 | CommandLineArgument& shortcut(const std::string& shortcut) | 
|---|
| 95 | { this->shortcut_ = shortcut; return *this; } | 
|---|
| 96 |  | 
|---|
| 97 | //! Returns the usage information | 
|---|
| 98 | const std::string& getInformation() const { return this->usageInformation_; } | 
|---|
| 99 | //! Sets the option information when displaying orxonox usage. | 
|---|
| 100 | CommandLineArgument& information(const std::string& usage) | 
|---|
| 101 | { this->usageInformation_ = usage; return *this; } | 
|---|
| 102 |  | 
|---|
| 103 | //! Returns the actual value of the argument. Can be equal to default value. | 
|---|
| 104 | const MultiType& getValue() const { return value_; } | 
|---|
| 105 | //! Returns the given default value as type T. | 
|---|
| 106 | const MultiType& getDefaultValue() const { return defaultValue_; } | 
|---|
| 107 |  | 
|---|
| 108 | private: | 
|---|
| 109 | //! Undefined copy constructor | 
|---|
| 110 | CommandLineArgument(const CommandLineArgument& instance); | 
|---|
| 111 |  | 
|---|
| 112 | //! Parses the value string of a command line argument. | 
|---|
| 113 | void parse(const std::string& value); | 
|---|
| 114 |  | 
|---|
| 115 | //! Tells whether the value has been changed by the command line. | 
|---|
| 116 | bool bHasDefaultValue_; | 
|---|
| 117 |  | 
|---|
| 118 | private: | 
|---|
| 119 | std::string name_;             //!< Name of the argument | 
|---|
| 120 | std::string shortcut_;         //!< Shortcut of the argument. @see getShortcut(). | 
|---|
| 121 | std::string usageInformation_; //!< Tells about the usage of this parameter | 
|---|
| 122 |  | 
|---|
| 123 | MultiType   value_;            //!< The actual value | 
|---|
| 124 | MultiType   defaultValue_;     //!< Default value. Should not be changed. | 
|---|
| 125 | }; | 
|---|
| 126 |  | 
|---|
| 127 |  | 
|---|
| 128 | /** | 
|---|
| 129 | @brief | 
|---|
| 130 | Global interface to command line options. | 
|---|
| 131 | Allows to add and retrieve command line arguments. Also does the parsing. | 
|---|
| 132 | @note | 
|---|
| 133 | Internally it is a Singleton, but the public interface is static. | 
|---|
| 134 | @see | 
|---|
| 135 | CommandLineArgument | 
|---|
| 136 | */ | 
|---|
| 137 | class _CoreExport CommandLineParser : public Singleton<CommandLineParser> | 
|---|
| 138 | { | 
|---|
| 139 | friend class Singleton<CommandLineParser>; | 
|---|
| 140 |  | 
|---|
| 141 | public: | 
|---|
| 142 | //! Constructor initialises bFirstTimeParse_ with true. | 
|---|
| 143 | CommandLineParser() : bFirstTimeParse_(true) { } | 
|---|
| 144 | ~CommandLineParser(); | 
|---|
| 145 |  | 
|---|
| 146 | //! Parse redirection to internal member method. | 
|---|
| 147 | static void parse(const std::string& cmdLine) | 
|---|
| 148 | { getInstance()._parse(cmdLine); } | 
|---|
| 149 |  | 
|---|
| 150 | static std::string getUsageInformation(); | 
|---|
| 151 |  | 
|---|
| 152 | static const CommandLineArgument* getArgument(const std::string& name); | 
|---|
| 153 | //! Writes the argument value in the given parameter. | 
|---|
| 154 | template <class T> | 
|---|
| 155 | static void getValue(const std::string& name, T* value) | 
|---|
| 156 | { *value = (T)(getArgument(name)->getValue()); } | 
|---|
| 157 | static const MultiType& getValue(const std::string& name) | 
|---|
| 158 | { return getArgument(name)->getValue(); } | 
|---|
| 159 |  | 
|---|
| 160 | static void addArgument(CommandLineArgument* argument); | 
|---|
| 161 | static void removeArgument(CommandLineArgument* argument); | 
|---|
| 162 |  | 
|---|
| 163 | static bool existsArgument(const std::string& name) | 
|---|
| 164 | { | 
|---|
| 165 | std::map<std::string, CommandLineArgument*>::const_iterator it = getInstance().cmdLineArgs_.find(name); | 
|---|
| 166 | return !(it == getInstance().cmdLineArgs_.end()); | 
|---|
| 167 | } | 
|---|
| 168 |  | 
|---|
| 169 | static void generateDoc(std::ofstream& file); | 
|---|
| 170 |  | 
|---|
| 171 | private: | 
|---|
| 172 | //! Undefined copy constructor | 
|---|
| 173 | CommandLineParser(const CommandLineParser& instance); | 
|---|
| 174 |  | 
|---|
| 175 | void _parse(const std::string& cmdLine); | 
|---|
| 176 | void checkFullArgument(const std::string& name, const std::string& value); | 
|---|
| 177 | void checkShortcut(const std::string& shortcut, const std::string& value); | 
|---|
| 178 |  | 
|---|
| 179 | /** | 
|---|
| 180 | Tells whether we parsed for the first time. The CommmandLineArguments are added before main(). | 
|---|
| 181 | So when we call parse() the first time, we need to create a map with all shortcuts since these | 
|---|
| 182 | get added after addCommandLineArgument(). | 
|---|
| 183 | */ | 
|---|
| 184 | bool bFirstTimeParse_; | 
|---|
| 185 |  | 
|---|
| 186 | //! Holds all pointers to the arguments and serves as a search map by name. | 
|---|
| 187 | std::map<std::string, CommandLineArgument*> cmdLineArgs_; | 
|---|
| 188 | //! Search map by shortcut for the arguments. | 
|---|
| 189 | std::map<std::string, CommandLineArgument*> cmdLineArgsShortcut_; | 
|---|
| 190 |  | 
|---|
| 191 | static CommandLineParser* singletonPtr_s; | 
|---|
| 192 | }; | 
|---|
| 193 |  | 
|---|
| 194 | template <> | 
|---|
| 195 | inline void CommandLineParser::getValue<std::string>(const std::string& name, std::string* value) | 
|---|
| 196 | { | 
|---|
| 197 | *value = getArgument(name)->getValue().get<std::string>(); | 
|---|
| 198 | } | 
|---|
| 199 | } | 
|---|
| 200 |  | 
|---|
| 201 | #endif /* _CommandLine_H__ */ | 
|---|