Orxonox  0.0.5 Codename: Arcturus
CommandLineParser.h
Go to the documentation of this file.
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 
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 
46 namespace orxonox
47 {
66  {
67  friend class CommandLineParser;
68 
69  public:
72  : bHasDefaultValue_(true)
73  , name_(name)
74  , value_(defaultValue)
75  , defaultValue_(defaultValue)
76  { }
78 
80  bool hasDefaultValue() const { return bHasDefaultValue_; }
82  const std::string& getName() const { return name_; }
83 
86  const std::string& getShortcut() const { return shortcut_; }
89  { this->shortcut_ = shortcut; return *this; }
90 
92  const std::string& getInformation() const { return this->usageInformation_; }
95  { this->usageInformation_ = usage; return *this; }
96 
98  const MultiType& getValue() const { return value_; }
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 
108  void parse(const std::string& value);
109 
112 
113  private:
117 
120  };
121 
122 
132  class _CoreExport CommandLineParser : public Singleton<CommandLineParser>
133  {
135 
136  public:
138  CommandLineParser() : bFirstTimeParse_(true) { }
140 
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);
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:
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 
180 
182  std::map<std::string, CommandLineArgument*> cmdLineArgs_;
184  std::map<std::string, CommandLineArgument*> cmdLineArgsShortcut_;
185 
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__ */
std::string shortcut_
Shortcut of the argument.
Definition: CommandLineParser.h:115
static void parse(const std::string &cmdLine)
Parse redirection to internal member method.
Definition: CommandLineParser.h:142
CommandLineParser()
Constructor initialises bFirstTimeParse_ with true.
Definition: CommandLineParser.h:138
const std::string & getInformation() const
Returns the usage information.
Definition: CommandLineParser.h:92
Shared library macros, enums, constants and forward declarations for the core library ...
::std::string string
Definition: gtest-port.h:756
CommandLineArgument & shortcut(const std::string &shortcut)
Sets the shortcut for the argument.
Definition: CommandLineParser.h:88
bool hasDefaultValue() const
Tells whether the value has been changed by the command line.
Definition: CommandLineParser.h:80
const std::string & getName() const
Returns the name of the argument.
Definition: CommandLineParser.h:82
Global interface to command line options.
Definition: CommandLineParser.h:132
MultiType defaultValue_
Default value. Should not be changed.
Definition: CommandLineParser.h:119
std::string usageInformation_
Tells about the usage of this parameter.
Definition: CommandLineParser.h:116
false defaultValue(2, false).argumentCompleter(0
CommandLineArgument & information(const std::string &usage)
Sets the option information when displaying orxonox usage.
Definition: CommandLineParser.h:94
MultiType value_
The actual value.
Definition: CommandLineParser.h:118
Base for singleton classes.
Definition: Singleton.h:114
CommandLineArgument(const std::string &name, const MultiType &defaultValue)
Constructor initialises both value_ and defaultValue_ with defaultValue.
Definition: CommandLineParser.h:71
Die Wagnis Klasse hat die folgenden Aufgaben:
Definition: ApplicationPaths.cc:66
Declaration of the MultiType and some helper constructs.
#define _CoreExport
Definition: CorePrereqs.h:61
The MultiType can hold a value of many possible types and convert them to other types.
Definition: MultiType.h:130
std::map< std::string, CommandLineArgument * > cmdLineArgs_
Holds all pointers to the arguments and serves as a search map by name.
Definition: CommandLineParser.h:182
Definition: InputPrereqs.h:78
static bool existsArgument(const std::string &name)
Definition: CommandLineParser.h:158
const MultiType & getValue() const
Returns the actual value of the argument. Can be equal to default value.
Definition: CommandLineParser.h:98
std::string name_
Name of the argument.
Definition: CommandLineParser.h:114
Definition of the Singleton template that is used as base class for classes that allow only one insta...
static CommandLineParser * singletonPtr_s
Definition: CommandLineParser.h:186
static void getValue(const std::string &name, T *value)
Writes the argument value in the given parameter.
Definition: CommandLineParser.h:150
const std::string & getShortcut() const
Returns the shortcut (example: "-p 22" for "--port 22") of the argument.
Definition: CommandLineParser.h:86
bool bFirstTimeParse_
Tells whether we parsed for the first time.
Definition: CommandLineParser.h:179
~CommandLineArgument()
Definition: CommandLineParser.h:77
internal::String name_
Definition: gtest.cc:2289
std::map< std::string, CommandLineArgument * > cmdLineArgsShortcut_
Search map by shortcut for the arguments.
Definition: CommandLineParser.h:184
Declaration of custom assertion facilities
Container class for a command line argument of any type supported by MultiType.
Definition: CommandLineParser.h:65
static const MultiType & getValue(const std::string &name)
Definition: CommandLineParser.h:152
bool bHasDefaultValue_
Tells whether the value has been changed by the command line.
Definition: CommandLineParser.h:111
const MultiType & getDefaultValue() const
Returns the given default value as type T.
Definition: CommandLineParser.h:100