Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Fix loads of doxygen warnings and other documentation issues

  • Property svn:eol-style set to native
File size: 7.4 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    @file
31    @ingroup Config CmdArgs
32    @brief Declaration of CommandLineParser and CommandLineArgument, definition of the SetCommandLineArgument() macros.
33*/
34
[1663]35#ifndef _CommandLine_H__
36#define _CommandLine_H__
37
[9560]38#include "core/CorePrereqs.h"
[3196]39
[7401]40#include <fstream>
[1663]41#include <map>
[3196]42#include "util/OrxAssert.h"
[2087]43#include "util/MultiType.h"
[10542]44#include "util/Singleton.h"
[1663]45
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.
[7284]60        So "--startVector {2, 4, 5}" is perfectly legal.
[2087]61
62        Retrieving an argument is possible with the getCommandLineArgument function of the
[6021]63        CommandLineParser class. It is a Singleton, but the public interface is static.
[1664]64    */
[2087]65    class _CoreExport CommandLineArgument
[1663]66    {
[6021]67        friend class CommandLineParser;
[1663]68
69    public:
[10345]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
[1664]79        //! Tells whether the value has been changed by the command line.
[1663]80        bool hasDefaultValue() const { return bHasDefaultValue_; }
[1664]81        //! Returns the name of the argument.
[1663]82        const std::string& getName() const { return name_; }
[1664]83
84        //! Returns the shortcut (example: "-p 22" for "--port 22") of the argument.
[3280]85        //! Evaluates to "" if there is none.
[1663]86        const std::string& getShortcut() const { return shortcut_; }
[1664]87        //! Sets the shortcut for the argument
[2087]88        CommandLineArgument& shortcut(const std::string& shortcut)
[1663]89        { this->shortcut_ = shortcut; return *this; }
90
[1664]91        //! Returns the usage information
92        const std::string& getInformation() const { return this->usageInformation_; }
93        //! Sets the option information when displaying orxonox usage.
[2087]94        CommandLineArgument& information(const std::string& usage)
[1664]95        { this->usageInformation_ = usage; return *this; }
96
[2087]97        //! Returns the actual value of the argument. Can be equal to default value.
[8729]98        const MultiType& getValue() const { return value_; }
[2087]99        //! Returns the given default value as type T.
[8729]100        const MultiType& getDefaultValue() const { return defaultValue_; }
[2087]101
102    private:
[11071]103        // non-copyable:
104        CommandLineArgument(const CommandLineArgument&) = delete;
105        CommandLineArgument& operator=(const CommandLineArgument&) = delete;
[1663]106
[1664]107        //! Parses the value string of a command line argument.
[8729]108        void parse(const std::string& value);
[1663]109
[1664]110        //! Tells whether the value has been changed by the command line.
[1663]111        bool bHasDefaultValue_;
112
113    private:
[1664]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
[1663]117
[3280]118        MultiType   value_;            //!< The actual value
119        MultiType   defaultValue_;     //!< Default value. Should not be changed.
[1663]120    };
121
122
[1664]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    */
[10542]132    class _CoreExport CommandLineParser : public Singleton<CommandLineParser>
[1663]133    {
[10542]134        friend class Singleton<CommandLineParser>;
135
[1663]136    public:
[10542]137        //! Constructor initialises bFirstTimeParse_ with true.
138        CommandLineParser() : bFirstTimeParse_(true) { }
139        ~CommandLineParser();
[1663]140
[1664]141        //! Parse redirection to internal member method.
[8729]142        static void parse(const std::string& cmdLine)
[10542]143        { getInstance()._parse(cmdLine); }
[1663]144
[1664]145        static std::string getUsageInformation();
146
[2087]147        static const CommandLineArgument* getArgument(const std::string& name);
[1664]148        //! Writes the argument value in the given parameter.
[1663]149        template <class T>
[1670]150        static void getValue(const std::string& name, T* value)
[2087]151        { *value = (T)(getArgument(name)->getValue()); }
[8729]152        static const MultiType& getValue(const std::string& name)
[2087]153        { return getArgument(name)->getValue(); }
[10404]154
[10345]155        static void addArgument(CommandLineArgument* argument);
[10404]156        static void removeArgument(CommandLineArgument* argument);
[1663]157
[2087]158        static bool existsArgument(const std::string& name)
159        {
[10542]160            std::map<std::string, CommandLineArgument*>::const_iterator it = getInstance().cmdLineArgs_.find(name);
161            return !(it == getInstance().cmdLineArgs_.end());
[2087]162        }
163
[7401]164        static void generateDoc(std::ofstream& file);
165
[1663]166    private:
[1664]167        //! Undefined copy constructor
[6021]168        CommandLineParser(const CommandLineParser& instance);
[1663]169
[8729]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);
[1663]173
[1664]174        /**
[2087]175            Tells whether we parsed for the first time. The CommmandLineArguments are added before main().
[1664]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        */
[1663]179        bool bFirstTimeParse_;
180
[1664]181        //! Holds all pointers to the arguments and serves as a search map by name.
[2087]182        std::map<std::string, CommandLineArgument*> cmdLineArgs_;
[2662]183        //! Search map by shortcut for the arguments.
[2087]184        std::map<std::string, CommandLineArgument*> cmdLineArgsShortcut_;
[10542]185
186        static CommandLineParser* singletonPtr_s;
[1663]187    };
188
[2087]189    template <>
[6021]190    inline void CommandLineParser::getValue<std::string>(const std::string& name, std::string* value)
[1663]191    {
[9550]192        *value = getArgument(name)->getValue().get<std::string>();
[1663]193    }
194}
195
196#endif /* _CommandLine_H__ */
Note: See TracBrowser for help on using the repository browser.