Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/libraries/core/CommandLineParser.h @ 8143

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

merged doc branch back to trunk

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