[1663] | 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 | #ifndef _CommandLine_H__ |
---|
| 30 | #define _CommandLine_H__ |
---|
| 31 | |
---|
| 32 | #include "CorePrereqs.h" |
---|
| 33 | #include <map> |
---|
| 34 | #include "util/Convert.h" |
---|
[1755] | 35 | #include "util/Debug.h" |
---|
[1764] | 36 | #include "util/Exception.h" |
---|
[2003] | 37 | #include "util/MultiType.h" |
---|
[1663] | 38 | |
---|
| 39 | #define SetCommandLineArgument(name, defaultValue) \ |
---|
[2003] | 40 | CommandLineArgument& CmdArgumentDummyBoolVar##name \ |
---|
[1670] | 41 | = orxonox::CommandLine::addArgument(#name, defaultValue) |
---|
[1664] | 42 | #define SetCommandLineSwitch(name) \ |
---|
[2003] | 43 | CommandLineArgument& CmdArgumentDummyBoolVar##name \ |
---|
[1670] | 44 | = orxonox::CommandLine::addArgument(#name, false) |
---|
[1663] | 45 | |
---|
[1664] | 46 | |
---|
[1663] | 47 | namespace orxonox |
---|
| 48 | { |
---|
[1664] | 49 | /** |
---|
| 50 | @brief |
---|
| 51 | Template struct which creates a distinct type for every integer constant. |
---|
| 52 | @note |
---|
| 53 | This allows to select a different function depending on a boolean value |
---|
| 54 | when creating a new CommandLineArgument. |
---|
| 55 | From 'Modern C++ Design' (Alexandrescu 2001). |
---|
| 56 | */ |
---|
| 57 | template <int v> |
---|
| 58 | struct Int2Type |
---|
| 59 | { |
---|
| 60 | enum { value = v }; |
---|
| 61 | }; |
---|
| 62 | |
---|
| 63 | /** |
---|
| 64 | @brief |
---|
[2003] | 65 | Container class for a command line argument of any type supported by MultiType. |
---|
| 66 | |
---|
| 67 | Whenever you want to have an option specified by a command line switch, |
---|
| 68 | you need to first define it with SetCommandLineArgument(name, defaultValue). |
---|
| 69 | It is then added to a map and possibly changed when the command line is being parsed. |
---|
| 70 | If the option was not given, you can detect this by asking hasDefaultValue(). |
---|
| 71 | |
---|
| 72 | There is a possibility to define a short cut so you can write "-p 20" instead of "--port 20". |
---|
| 73 | Note the difference between "-" and "--"! |
---|
| 74 | Also, there is no restriction to the number of strings you add after --name. |
---|
| 75 | So "--startVector (2, 4, 5)" is perfectly legal. |
---|
| 76 | |
---|
| 77 | Retrieving an argument is possible with the getCommandLineArgument function of the |
---|
| 78 | CommandLine class. It is a Singleton, but the public interface is static. |
---|
[1664] | 79 | */ |
---|
[2003] | 80 | class _CoreExport CommandLineArgument |
---|
[1663] | 81 | { |
---|
| 82 | friend class CommandLine; |
---|
| 83 | |
---|
| 84 | public: |
---|
[1664] | 85 | //! Tells whether the value has been changed by the command line. |
---|
[1663] | 86 | bool hasDefaultValue() const { return bHasDefaultValue_; } |
---|
[1664] | 87 | //! Returns the name of the argument. |
---|
[1663] | 88 | const std::string& getName() const { return name_; } |
---|
[1664] | 89 | |
---|
| 90 | //! Returns the shortcut (example: "-p 22" for "--port 22") of the argument. |
---|
| 91 | //! Evaluates to "" if none there is none. |
---|
[1663] | 92 | const std::string& getShortcut() const { return shortcut_; } |
---|
[1664] | 93 | //! Sets the shortcut for the argument |
---|
[2003] | 94 | CommandLineArgument& shortcut(const std::string& shortcut) |
---|
[1663] | 95 | { this->shortcut_ = shortcut; return *this; } |
---|
| 96 | |
---|
[1664] | 97 | //! Returns the usage information |
---|
| 98 | const std::string& getInformation() const { return this->usageInformation_; } |
---|
| 99 | //! Sets the option information when displaying orxonox usage. |
---|
[2003] | 100 | CommandLineArgument& information(const std::string& usage) |
---|
[1664] | 101 | { this->usageInformation_ = usage; return *this; } |
---|
| 102 | |
---|
[2003] | 103 | //! Returns the actual value of the argument. Can be equal to default value. |
---|
| 104 | MultiType getValue() const { return value_; } |
---|
| 105 | //! Returns the given default value as type T. |
---|
| 106 | MultiType getDefaultValue() const { return defaultValue_; } |
---|
| 107 | |
---|
| 108 | private: |
---|
| 109 | //! Constructor initialises both value_ and defaultValue_ with defaultValue. |
---|
| 110 | CommandLineArgument(const std::string& name, const MultiType& defaultValue) |
---|
[1670] | 111 | : bHasDefaultValue_(true) |
---|
| 112 | , name_(name) |
---|
[2003] | 113 | , value_(defaultValue) |
---|
| 114 | , defaultValue_(defaultValue) |
---|
[1663] | 115 | { } |
---|
| 116 | |
---|
[1664] | 117 | //! Undefined copy constructor |
---|
[2003] | 118 | CommandLineArgument(const CommandLineArgument& instance); |
---|
| 119 | ~CommandLineArgument() { } |
---|
[1663] | 120 | |
---|
[1664] | 121 | //! Parses the value string of a command line argument. |
---|
[2003] | 122 | void parse(const std::string& value); |
---|
[1663] | 123 | |
---|
[1664] | 124 | //! Tells whether the value has been changed by the command line. |
---|
[1663] | 125 | bool bHasDefaultValue_; |
---|
| 126 | |
---|
| 127 | private: |
---|
[1664] | 128 | std::string name_; //!< Name of the argument |
---|
| 129 | std::string shortcut_; //!< Shortcut of the argument. @see getShortcut(). |
---|
| 130 | std::string usageInformation_; //!< Tells about the usage of this parameter |
---|
[1663] | 131 | |
---|
[2003] | 132 | MultiType value_; //!< The actual value |
---|
| 133 | MultiType defaultValue_; //!< Default value. Should not be changed. |
---|
[1663] | 134 | }; |
---|
| 135 | |
---|
| 136 | |
---|
[1664] | 137 | /** |
---|
| 138 | @brief |
---|
| 139 | Global interface to command line options. |
---|
| 140 | Allows to add and retrieve command line arguments. Also does the parsing. |
---|
| 141 | @note |
---|
| 142 | Internally it is a Singleton, but the public interface is static. |
---|
| 143 | @see |
---|
| 144 | CommandLineArgument |
---|
| 145 | */ |
---|
[1663] | 146 | class _CoreExport CommandLine |
---|
| 147 | { |
---|
| 148 | public: |
---|
| 149 | |
---|
[1664] | 150 | //! Parse redirection to internal member method. |
---|
| 151 | static void parse(const std::vector<std::string>& arguments) { _getInstance()._parse(arguments); } |
---|
[1663] | 152 | |
---|
[1664] | 153 | static std::string getUsageInformation(); |
---|
| 154 | |
---|
[2003] | 155 | static const CommandLineArgument* getArgument(const std::string& name); |
---|
[1664] | 156 | //! Writes the argument value in the given parameter. |
---|
[1663] | 157 | template <class T> |
---|
[1670] | 158 | static void getValue(const std::string& name, T* value) |
---|
[2003] | 159 | { *value = (T)(getArgument(name)->getValue()); } |
---|
| 160 | static MultiType getValue(const std::string& name) |
---|
| 161 | { return getArgument(name)->getValue(); } |
---|
[1664] | 162 | template <class T> |
---|
[2003] | 163 | static CommandLineArgument& addArgument(const std::string& name, T defaultValue); |
---|
[1663] | 164 | |
---|
[2003] | 165 | static bool existsArgument(const std::string& name) |
---|
| 166 | { |
---|
| 167 | std::map<std::string, CommandLineArgument*>::const_iterator it = _getInstance().cmdLineArgs_.find(name); |
---|
| 168 | return !(it == _getInstance().cmdLineArgs_.end()); |
---|
| 169 | } |
---|
| 170 | |
---|
| 171 | |
---|
[1663] | 172 | private: |
---|
[1664] | 173 | //! Constructor initialises bFirstTimeParse_ with true. |
---|
[1663] | 174 | CommandLine() : bFirstTimeParse_(true) { } |
---|
[1664] | 175 | //! Undefined copy constructor |
---|
[1663] | 176 | CommandLine(const CommandLine& instance); |
---|
| 177 | ~CommandLine(); |
---|
| 178 | |
---|
| 179 | static CommandLine& _getInstance(); |
---|
| 180 | |
---|
[1664] | 181 | void _parse(const std::vector<std::string>& arguments); |
---|
[1663] | 182 | void checkFullArgument(const std::string& name, const std::string& value); |
---|
| 183 | void checkShortcut(const std::string& shortcut, const std::string& value); |
---|
| 184 | |
---|
[1664] | 185 | /** |
---|
[2003] | 186 | Tells whether we parsed for the first time. The CommmandLineArguments are added before main(). |
---|
[1664] | 187 | So when we call parse() the first time, we need to create a map with all shortcuts since these |
---|
| 188 | get added after addCommandLineArgument(). |
---|
| 189 | */ |
---|
[1663] | 190 | bool bFirstTimeParse_; |
---|
| 191 | |
---|
[1664] | 192 | //! Holds all pointers to the arguments and serves as a search map by name. |
---|
[2003] | 193 | std::map<std::string, CommandLineArgument*> cmdLineArgs_; |
---|
[1664] | 194 | //! Search map by chortcut for the arguments. |
---|
[2003] | 195 | std::map<std::string, CommandLineArgument*> cmdLineArgsShortcut_; |
---|
[1663] | 196 | }; |
---|
| 197 | |
---|
| 198 | |
---|
[1664] | 199 | /** |
---|
| 200 | @brief |
---|
| 201 | Adds a new CommandLineArgument to the internal map. |
---|
| 202 | Note that only such arguments are actually valid. |
---|
| 203 | @param name |
---|
| 204 | Name of the argument. Shortcut can be added later. |
---|
| 205 | @param defaultValue |
---|
| 206 | Default value that is used when argument was not given. |
---|
| 207 | */ |
---|
[1663] | 208 | template <class T> |
---|
[2003] | 209 | CommandLineArgument& CommandLine::addArgument(const std::string& name, T defaultValue) |
---|
[1663] | 210 | { |
---|
[2003] | 211 | std::map<std::string, CommandLineArgument*>::const_iterator it = _getInstance().cmdLineArgs_.find(name); |
---|
[1663] | 212 | OrxAssert(it == _getInstance().cmdLineArgs_.end(), |
---|
| 213 | "Cannot add a command line argument with name '" + name + "' twice."); |
---|
| 214 | |
---|
[2003] | 215 | return *(_getInstance().cmdLineArgs_[name] = new CommandLineArgument(name, defaultValue)); |
---|
[1663] | 216 | } |
---|
| 217 | } |
---|
| 218 | |
---|
| 219 | #endif /* _CommandLine_H__ */ |
---|