Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7361 was 7361, checked in by landauf, 14 years ago

added documentation

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