Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/strongTurret/src/libraries/core/config/CommandLineParser.h @ 9693

Last change on this file since 9693 was 9560, checked in by landauf, 12 years ago

moved CommandLineParser to core/config/

  • Property svn:eol-style set to native
File size: 8.7 KB
Line 
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
51#define SetCommandLineArgument(name, defaultValue) \
52    orxonox::CommandLineArgument& CmdArgumentDummyBoolVar##name \
53    = orxonox::CommandLineParser::addArgument(#name, defaultValue)
54#define SetCommandLineSwitch(name) \
55    orxonox::CommandLineArgument& CmdArgumentDummyBoolVar##name \
56    = orxonox::CommandLineParser::addArgument(#name, false)
57
58namespace orxonox
59{
60    /**
61    @brief
62        Container class for a command line argument of any type supported by MultiType.
63
64        Whenever you want to have an option specified by a command line switch,
65        you need to first define it with SetCommandLineArgument(name, defaultValue).
66        It is then added to a map and possibly changed when the command line is being parsed.
67        If the option was not given, you can detect this by asking hasDefaultValue().
68
69        There is a possibility to define a short cut so you can write "-p 20" instead of "--port 20".
70        Note the difference between "-" and "--"!
71        Also, there is no restriction to the number of strings you add after --name.
72        So "--startVector {2, 4, 5}" is perfectly legal.
73
74        Retrieving an argument is possible with the getCommandLineArgument function of the
75        CommandLineParser class. It is a Singleton, but the public interface is static.
76    */
77    class _CoreExport CommandLineArgument
78    {
79        friend class CommandLineParser;
80
81    public:
82        //! Tells whether the value has been changed by the command line.
83        bool hasDefaultValue() const { return bHasDefaultValue_; }
84        //! Returns the name of the argument.
85        const std::string& getName() const { return name_; }
86
87        //! Returns the shortcut (example: "-p 22" for "--port 22") of the argument.
88        //! Evaluates to "" if there is none.
89        const std::string& getShortcut() const { return shortcut_; }
90        //! Sets the shortcut for the argument
91        CommandLineArgument& shortcut(const std::string& shortcut)
92        { this->shortcut_ = shortcut; return *this; }
93
94        //! Returns the usage information
95        const std::string& getInformation() const { return this->usageInformation_; }
96        //! Sets the option information when displaying orxonox usage.
97        CommandLineArgument& information(const std::string& usage)
98        { this->usageInformation_ = usage; return *this; }
99
100        //! Returns the actual value of the argument. Can be equal to default value.
101        const MultiType& getValue() const { return value_; }
102        //! Returns the given default value as type T.
103        const MultiType& getDefaultValue() const { return defaultValue_; }
104
105    private:
106        //! Constructor initialises both value_ and defaultValue_ with defaultValue.
107        CommandLineArgument(const std::string& name, const MultiType& defaultValue)
108            : bHasDefaultValue_(true)
109            , name_(name)
110            , value_(defaultValue)
111            , defaultValue_(defaultValue)
112        { }
113
114        //! Undefined copy constructor
115        CommandLineArgument(const CommandLineArgument& instance);
116        ~CommandLineArgument() { }
117
118        //! Parses the value string of a command line argument.
119        void parse(const std::string& value);
120
121        //! Tells whether the value has been changed by the command line.
122        bool bHasDefaultValue_;
123
124    private:
125        std::string name_;             //!< Name of the argument
126        std::string shortcut_;         //!< Shortcut of the argument. @see getShortcut().
127        std::string usageInformation_; //!< Tells about the usage of this parameter
128
129        MultiType   value_;            //!< The actual value
130        MultiType   defaultValue_;     //!< Default value. Should not be changed.
131    };
132
133
134    /**
135    @brief
136        Global interface to command line options.
137        Allows to add and retrieve command line arguments. Also does the parsing.
138    @note
139        Internally it is a Singleton, but the public interface is static.
140    @see
141        CommandLineArgument
142    */
143    class _CoreExport CommandLineParser
144    {
145    public:
146
147        //! Parse redirection to internal member method.
148        static void parse(const std::string& cmdLine)
149        { _getInstance()._parse(cmdLine); }
150
151        static std::string getUsageInformation();
152
153        static const CommandLineArgument* getArgument(const std::string& name);
154        //! Writes the argument value in the given parameter.
155        template <class T>
156        static void getValue(const std::string& name, T* value)
157        { *value = (T)(getArgument(name)->getValue()); }
158        static const MultiType& getValue(const std::string& name)
159        { return getArgument(name)->getValue(); }
160        template <class T>
161        static CommandLineArgument& addArgument(const std::string& name, T defaultValue);
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 destroyAllArguments();
170
171        static void generateDoc(std::ofstream& file);
172
173    private:
174        //! Constructor initialises bFirstTimeParse_ with true.
175        CommandLineParser() : bFirstTimeParse_(true) { }
176        //! Undefined copy constructor
177        CommandLineParser(const CommandLineParser& instance);
178        ~CommandLineParser();
179
180        static CommandLineParser& _getInstance();
181
182        void _parse(const std::string& cmdLine);
183        void checkFullArgument(const std::string& name, const std::string& value);
184        void checkShortcut(const std::string& shortcut, const std::string& value);
185
186        /**
187            Tells whether we parsed for the first time. The CommmandLineArguments are added before main().
188            So when we call parse() the first time, we need to create a map with all shortcuts since these
189            get added after addCommandLineArgument().
190        */
191        bool bFirstTimeParse_;
192
193        //! Holds all pointers to the arguments and serves as a search map by name.
194        std::map<std::string, CommandLineArgument*> cmdLineArgs_;
195        //! Search map by shortcut for the arguments.
196        std::map<std::string, CommandLineArgument*> cmdLineArgsShortcut_;
197    };
198
199    template <>
200    inline void CommandLineParser::getValue<std::string>(const std::string& name, std::string* value)
201    {
202        *value = getArgument(name)->getValue().get<std::string>();
203    }
204
205    /**
206    @brief
207        Adds a new CommandLineArgument to the internal map.
208        Note that only such arguments are actually valid.
209    @param name
210        Name of the argument. Shortcut can be added later.
211    @param defaultValue
212        Default value that is used when argument was not given.
213    */
214    template <class T>
215    CommandLineArgument& CommandLineParser::addArgument(const std::string& name, T defaultValue)
216    {
217        OrxAssert(!_getInstance().existsArgument(name),
218            "Cannot add a command line argument with name '" + name + "' twice.");
219        OrxAssert(!MultiType(defaultValue).isType<bool>() || MultiType(defaultValue).get<bool>() != true,
220               "Boolean command line arguments with positive default values are not supported." << endl
221            << "Please use SetCommandLineSwitch and adjust your argument: " << name);
222
223        return *(_getInstance().cmdLineArgs_[name] = new CommandLineArgument(name, defaultValue));
224    }
225}
226
227#endif /* _CommandLine_H__ */
Note: See TracBrowser for help on using the repository browser.