Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/cpp11_v2/src/libraries/core/commandline/CommandLineParser.h @ 10990

Last change on this file since 10990 was 10990, checked in by landauf, 8 years ago

use '= delete' to explicitly delete unimplemented copy-constructors (for non-copyable classes).
use '= default' to explicitly implement default constructors/destructors.

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