Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/ogre1.9/src/libraries/core/commandline/CommandLineParser.h @ 11859

Last change on this file since 11859 was 11099, checked in by muemart, 9 years ago

Fix loads of doxygen warnings and other documentation issues

  • Property svn:eol-style set to native
File size: 7.4 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    @file
31    @ingroup Config CmdArgs
32    @brief Declaration of CommandLineParser and CommandLineArgument, definition of the SetCommandLineArgument() macros.
33*/
34
35#ifndef _CommandLine_H__
36#define _CommandLine_H__
37
38#include "core/CorePrereqs.h"
39
40#include <fstream>
41#include <map>
42#include "util/OrxAssert.h"
43#include "util/MultiType.h"
44#include "util/Singleton.h"
45
46namespace orxonox
47{
48    /**
49    @brief
50        Container class for a command line argument of any type supported by MultiType.
51
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        CommandLineParser class. It is a Singleton, but the public interface is static.
64    */
65    class _CoreExport CommandLineArgument
66    {
67        friend class CommandLineParser;
68
69    public:
70        //! Constructor initialises both value_ and defaultValue_ with defaultValue.
71        CommandLineArgument(const std::string& name, const MultiType& defaultValue)
72            : bHasDefaultValue_(true)
73            , name_(name)
74            , value_(defaultValue)
75            , defaultValue_(defaultValue)
76        { }
77        ~CommandLineArgument() { }
78
79        //! Tells whether the value has been changed by the command line.
80        bool hasDefaultValue() const { return bHasDefaultValue_; }
81        //! Returns the name of the argument.
82        const std::string& getName() const { return name_; }
83
84        //! Returns the shortcut (example: "-p 22" for "--port 22") of the argument.
85        //! Evaluates to "" if there is none.
86        const std::string& getShortcut() const { return shortcut_; }
87        //! Sets the shortcut for the argument
88        CommandLineArgument& shortcut(const std::string& shortcut)
89        { this->shortcut_ = shortcut; return *this; }
90
91        //! Returns the usage information
92        const std::string& getInformation() const { return this->usageInformation_; }
93        //! Sets the option information when displaying orxonox usage.
94        CommandLineArgument& information(const std::string& usage)
95        { this->usageInformation_ = usage; return *this; }
96
97        //! Returns the actual value of the argument. Can be equal to default value.
98        const MultiType& getValue() const { return value_; }
99        //! Returns the given default value as type T.
100        const MultiType& getDefaultValue() const { return defaultValue_; }
101
102    private:
103        // non-copyable:
104        CommandLineArgument(const CommandLineArgument&) = delete;
105        CommandLineArgument& operator=(const CommandLineArgument&) = delete;
106
107        //! Parses the value string of a command line argument.
108        void parse(const std::string& value);
109
110        //! Tells whether the value has been changed by the command line.
111        bool bHasDefaultValue_;
112
113    private:
114        std::string name_;             //!< Name of the argument
115        std::string shortcut_;         //!< Shortcut of the argument. @see getShortcut().
116        std::string usageInformation_; //!< Tells about the usage of this parameter
117
118        MultiType   value_;            //!< The actual value
119        MultiType   defaultValue_;     //!< Default value. Should not be changed.
120    };
121
122
123    /**
124    @brief
125        Global interface to command line options.
126        Allows to add and retrieve command line arguments. Also does the parsing.
127    @note
128        Internally it is a Singleton, but the public interface is static.
129    @see
130        CommandLineArgument
131    */
132    class _CoreExport CommandLineParser : public Singleton<CommandLineParser>
133    {
134        friend class Singleton<CommandLineParser>;
135
136    public:
137        //! Constructor initialises bFirstTimeParse_ with true.
138        CommandLineParser() : bFirstTimeParse_(true) { }
139        ~CommandLineParser();
140
141        //! Parse redirection to internal member method.
142        static void parse(const std::string& cmdLine)
143        { getInstance()._parse(cmdLine); }
144
145        static std::string getUsageInformation();
146
147        static const CommandLineArgument* getArgument(const std::string& name);
148        //! Writes the argument value in the given parameter.
149        template <class T>
150        static void getValue(const std::string& name, T* value)
151        { *value = (T)(getArgument(name)->getValue()); }
152        static const MultiType& getValue(const std::string& name)
153        { return getArgument(name)->getValue(); }
154
155        static void addArgument(CommandLineArgument* argument);
156        static void removeArgument(CommandLineArgument* argument);
157
158        static bool existsArgument(const std::string& name)
159        {
160            std::map<std::string, CommandLineArgument*>::const_iterator it = getInstance().cmdLineArgs_.find(name);
161            return !(it == getInstance().cmdLineArgs_.end());
162        }
163
164        static void generateDoc(std::ofstream& file);
165
166    private:
167        //! Undefined copy constructor
168        CommandLineParser(const CommandLineParser& instance);
169
170        void _parse(const std::string& cmdLine);
171        void checkFullArgument(const std::string& name, const std::string& value);
172        void checkShortcut(const std::string& shortcut, const std::string& value);
173
174        /**
175            Tells whether we parsed for the first time. The CommmandLineArguments are added before main().
176            So when we call parse() the first time, we need to create a map with all shortcuts since these
177            get added after addCommandLineArgument().
178        */
179        bool bFirstTimeParse_;
180
181        //! Holds all pointers to the arguments and serves as a search map by name.
182        std::map<std::string, CommandLineArgument*> cmdLineArgs_;
183        //! Search map by shortcut for the arguments.
184        std::map<std::string, CommandLineArgument*> cmdLineArgsShortcut_;
185
186        static CommandLineParser* singletonPtr_s;
187    };
188
189    template <>
190    inline void CommandLineParser::getValue<std::string>(const std::string& name, std::string* value)
191    {
192        *value = getArgument(name)->getValue().get<std::string>();
193    }
194}
195
196#endif /* _CommandLine_H__ */
Note: See TracBrowser for help on using the repository browser.