Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/core/CommandLine.h @ 3200

Last change on this file since 3200 was 3196, checked in by rgrieder, 15 years ago

Merged pch branch back to trunk.

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