Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/doc/src/libraries/core/CommandLineParser.h @ 7357

Last change on this file since 7357 was 7357, checked in by rgrieder, 14 years ago

Changed command line arguments reference to a simple Doxygen page containing the usage information text.

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