[Orxonox-commit 5683] r10343 - in code/branches/core7/src: libraries/core libraries/core/commandline libraries/core/config libraries/core/input libraries/network orxonox orxonox/gamestates
landauf at orxonox.net
landauf at orxonox.net
Sat Apr 4 13:34:39 CEST 2015
Author: landauf
Date: 2015-04-04 13:34:39 +0200 (Sat, 04 Apr 2015)
New Revision: 10343
Added:
code/branches/core7/src/libraries/core/commandline/
code/branches/core7/src/libraries/core/commandline/CMakeLists.txt
code/branches/core7/src/libraries/core/commandline/CommandLineParser.cc
code/branches/core7/src/libraries/core/commandline/CommandLineParser.h
Removed:
code/branches/core7/src/libraries/core/config/CommandLineParser.cc
code/branches/core7/src/libraries/core/config/CommandLineParser.h
Modified:
code/branches/core7/src/libraries/core/CMakeLists.txt
code/branches/core7/src/libraries/core/Core.cc
code/branches/core7/src/libraries/core/Game.cc
code/branches/core7/src/libraries/core/PathConfig.cc
code/branches/core7/src/libraries/core/config/CMakeLists.txt
code/branches/core7/src/libraries/core/input/InputManager.cc
code/branches/core7/src/libraries/network/Client.cc
code/branches/core7/src/orxonox/LevelManager.cc
code/branches/core7/src/orxonox/Main.cc
code/branches/core7/src/orxonox/gamestates/GSClient.cc
code/branches/core7/src/orxonox/gamestates/GSServer.cc
Log:
moved CommandLineParser into separate subfolder
Modified: code/branches/core7/src/libraries/core/CMakeLists.txt
===================================================================
--- code/branches/core7/src/libraries/core/CMakeLists.txt 2015-04-04 11:24:49 UTC (rev 10342)
+++ code/branches/core7/src/libraries/core/CMakeLists.txt 2015-04-04 11:34:39 UTC (rev 10343)
@@ -63,6 +63,7 @@
ADD_SUBDIRECTORY(class)
ADD_SUBDIRECTORY(command)
+ADD_SUBDIRECTORY(commandline)
ADD_SUBDIRECTORY(config)
ADD_SUBDIRECTORY(input)
ADD_SUBDIRECTORY(module)
Modified: code/branches/core7/src/libraries/core/Core.cc
===================================================================
--- code/branches/core7/src/libraries/core/Core.cc 2015-04-04 11:24:49 UTC (rev 10342)
+++ code/branches/core7/src/libraries/core/Core.cc 2015-04-04 11:34:39 UTC (rev 10343)
@@ -59,7 +59,7 @@
#include "util/ScopedSingletonManager.h"
#include "util/SignalHandler.h"
#include "PathConfig.h"
-#include "config/CommandLineParser.h"
+#include "commandline/CommandLineParser.h"
#include "config/ConfigFileManager.h"
#include "config/ConfigValueIncludes.h"
#include "CoreIncludes.h"
Modified: code/branches/core7/src/libraries/core/Game.cc
===================================================================
--- code/branches/core7/src/libraries/core/Game.cc 2015-04-04 11:24:49 UTC (rev 10342)
+++ code/branches/core7/src/libraries/core/Game.cc 2015-04-04 11:34:39 UTC (rev 10343)
@@ -45,7 +45,7 @@
#include "util/SubString.h"
#include "Core.h"
#include "CoreIncludes.h"
-#include "config/CommandLineParser.h"
+#include "commandline/CommandLineParser.h"
#include "config/ConfigValueIncludes.h"
#include "GameMode.h"
#include "GameState.h"
Modified: code/branches/core7/src/libraries/core/PathConfig.cc
===================================================================
--- code/branches/core7/src/libraries/core/PathConfig.cc 2015-04-04 11:24:49 UTC (rev 10342)
+++ code/branches/core7/src/libraries/core/PathConfig.cc 2015-04-04 11:34:39 UTC (rev 10343)
@@ -52,7 +52,7 @@
#include "SpecialConfig.h"
#include "util/Output.h"
#include "util/Exception.h"
-#include "config/CommandLineParser.h"
+#include "commandline/CommandLineParser.h"
// Differentiate Boost Filesystem v2 and v3
#if (BOOST_FILESYSTEM_VERSION < 3)
Added: code/branches/core7/src/libraries/core/commandline/CMakeLists.txt
===================================================================
--- code/branches/core7/src/libraries/core/commandline/CMakeLists.txt (rev 0)
+++ code/branches/core7/src/libraries/core/commandline/CMakeLists.txt 2015-04-04 11:34:39 UTC (rev 10343)
@@ -0,0 +1,3 @@
+ADD_SOURCE_FILES(CORE_SRC_FILES
+ CommandLineParser.cc
+)
Copied: code/branches/core7/src/libraries/core/commandline/CommandLineParser.cc (from rev 10328, code/branches/core7/src/libraries/core/config/CommandLineParser.cc)
===================================================================
--- code/branches/core7/src/libraries/core/commandline/CommandLineParser.cc (rev 0)
+++ code/branches/core7/src/libraries/core/commandline/CommandLineParser.cc 2015-04-04 11:34:39 UTC (rev 10343)
@@ -0,0 +1,340 @@
+/*
+ * ORXONOX - the hottest 3D action shooter ever to exist
+ * > www.orxonox.net <
+ *
+ *
+ * License notice:
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * Author:
+ * Reto Grieder
+ * Co-authors:
+ * ...
+ *
+ */
+
+#include "CommandLineParser.h"
+
+#include <algorithm>
+#include <sstream>
+
+#include "util/Convert.h"
+#include "util/Output.h"
+#include "util/Exception.h"
+#include "util/StringUtils.h"
+#include "util/SubString.h"
+#include "core/PathConfig.h"
+
+namespace orxonox
+{
+ /**
+ @brief
+ Parses a value string for a command line argument.
+ It simply uses convertValue(Output, Input) to do that.
+ Bools are treated specially. That is necessary
+ so that you can have simple command line switches.
+ */
+ void CommandLineArgument::parse(const std::string& value)
+ {
+ if (value_.isType<bool>())
+ {
+ // simulate command line switch
+ bool temp;
+ if (convertValue(&temp, value))
+ {
+ this->bHasDefaultValue_ = false;
+ this->value_ = temp;
+ }
+ else if (value.empty())
+ {
+ this->bHasDefaultValue_ = false;
+ this->value_ = true;
+ }
+ else
+ ThrowException(Argument, "Could not read command line argument '" + getName() + "'.");
+ }
+ else
+ {
+ if (!value_.set(value))
+ {
+ value_.set(defaultValue_);
+ ThrowException(Argument, "Could not read command line argument '" + getName() + "'.");
+ }
+ else
+ this->bHasDefaultValue_ = false;
+ }
+ }
+
+
+ /**
+ @brief
+ Destructor destroys all CommandLineArguments with it.
+ */
+ CommandLineParser::~CommandLineParser()
+ {
+ CommandLineParser::destroyAllArguments();
+ }
+
+ /**
+ @brief
+ Returns a unique instance (Meyers Singleton).
+ */
+ CommandLineParser& CommandLineParser::_getInstance()
+ {
+ static CommandLineParser instance;
+ return instance;
+ }
+
+ /**
+ @brief
+ Destroys all command line arguments. This should be called at the end
+ of main. Do not use before that.
+ */
+ void CommandLineParser::destroyAllArguments()
+ {
+ for (std::map<std::string, CommandLineArgument*>::const_iterator it = _getInstance().cmdLineArgs_.begin();
+ it != _getInstance().cmdLineArgs_.end(); ++it)
+ delete it->second;
+ _getInstance().cmdLineArgs_.clear();
+ }
+
+ /** Parses the command line string for arguments and stores these.
+ @note
+ The reason that you have to provide the string to be parsed as
+ space separated list is because of argc and argv. If you only have
+ a whole string, simply use getAllStrings() of SubString.
+ @param cmdLine
+ Command line string WITHOUT the execution path.
+ */
+ void CommandLineParser::_parse(const std::string& cmdLine)
+ {
+ std::vector<std::string> arguments;
+ SubString tokens(cmdLine, " ", " ", false, '\\', true, '"', true, '\0', '\0', false);
+ for (unsigned i = 0; i < tokens.size(); ++i)
+ arguments.push_back(tokens[i]);
+
+ try
+ {
+ // why this? See bFirstTimeParse_ declaration.
+ if (bFirstTimeParse_)
+ {
+ // first shove all the shortcuts in a map
+ for (std::map<std::string, CommandLineArgument*>::const_iterator it = cmdLineArgs_.begin();
+ it != cmdLineArgs_.end(); ++it)
+ {
+ OrxAssert(cmdLineArgsShortcut_.find(it->second->getShortcut()) == cmdLineArgsShortcut_.end(),
+ "Cannot have two command line shortcut with the same name.");
+ if (!it->second->getShortcut().empty())
+ cmdLineArgsShortcut_[it->second->getShortcut()] = it->second;
+ }
+ bFirstTimeParse_ = false;
+ }
+
+ std::string name;
+ std::string shortcut;
+ std::string value;
+ for (unsigned int i = 0; i < arguments.size(); ++i)
+ {
+ if (arguments[i].size() != 0)
+ {
+ // sure not ""
+ if (arguments[i][0] == '-')
+ {
+ // start with "-"
+ if (arguments[i].size() == 1)
+ {
+ // argument[i] is "-", probably a minus sign
+ value += "- ";
+ }
+ else if (arguments[i][1] <= 57 && arguments[i][1] >= 48)
+ {
+ // negative number as a value
+ value += arguments[i] + ' ';
+ }
+ else
+ {
+ // can be shortcut or full name argument
+
+ // save old data first
+ value = removeTrailingWhitespaces(value);
+ if (!name.empty())
+ {
+ checkFullArgument(name, value);
+ name.clear();
+ assert(shortcut.empty());
+ }
+ else if (!shortcut.empty())
+ {
+ checkShortcut(shortcut, value);
+ shortcut.clear();
+ assert(name.empty());
+ }
+
+ if (arguments[i][1] == '-')
+ {
+ // full name argument with "--name"
+ name = arguments[i].substr(2);
+ }
+ else
+ {
+ // shortcut with "-s"
+ shortcut = arguments[i].substr(1);
+ }
+
+ // reset value string
+ value.clear();
+ }
+ }
+ else
+ {
+ // value string
+
+ if (name.empty() && shortcut.empty())
+ {
+ ThrowException(Argument, "Expected \"-\" or \"-\" in command line arguments.\n");
+ }
+
+ // Concatenate strings as long as there's no new argument by "-" or "--"
+ value += arguments[i] + ' ';
+ }
+ }
+ }
+
+ // parse last argument
+ value = removeTrailingWhitespaces(value);
+ if (!name.empty())
+ {
+ checkFullArgument(name, value);
+ assert(shortcut.empty());
+ }
+ else if (!shortcut.empty())
+ {
+ checkShortcut(shortcut, value);
+ assert(name.empty());
+ }
+ }
+ catch (const ArgumentException& ex)
+ {
+ orxout(user_error) << "Could not parse command line: " << ex.what() << endl;
+ orxout(user_error) << CommandLineParser::getUsageInformation() << endl;
+ throw GeneralException("");
+ }
+ }
+
+ /**
+ @brief
+ Parses an argument based on its full name.
+ @param name
+ Full name of the argument
+ @param value
+ String containing the value
+ @param bParsingFile
+ Parsing a file or the command line itself
+ */
+ void CommandLineParser::checkFullArgument(const std::string& name, const std::string& value)
+ {
+ std::map<std::string, CommandLineArgument*>::const_iterator it = cmdLineArgs_.find(name);
+ if (it == cmdLineArgs_.end())
+ ThrowException(Argument, "Command line argument '" + name + "' does not exist.");
+
+ it->second->parse(value);
+ }
+
+ /**
+ @brief
+ Parses an argument based on its shortcut.
+ @param shortcut
+ Shortcut to the argument
+ @param value
+ String containing the value
+ @param bParsingFile
+ Parsing a file or the command line itself
+ */
+ void CommandLineParser::checkShortcut(const std::string& shortcut, const std::string& value)
+ {
+ std::map<std::string, CommandLineArgument*>::const_iterator it = cmdLineArgsShortcut_.find(shortcut);
+ if (it == cmdLineArgsShortcut_.end())
+ ThrowException(Argument, "Command line shortcut '" + shortcut + "' does not exist.");
+
+ it->second->parse(value);
+ }
+
+ std::string CommandLineParser::getUsageInformation()
+ {
+ CommandLineParser& inst = _getInstance();
+ std::ostringstream infoStr;
+
+ // determine maximum name size
+ size_t maxNameSize = 0;
+ for (std::map<std::string, CommandLineArgument*>::const_iterator it = inst.cmdLineArgs_.begin();
+ it != inst.cmdLineArgs_.end(); ++it)
+ {
+ maxNameSize = std::max(it->second->getName().size(), maxNameSize);
+ }
+
+ infoStr << endl;
+ infoStr << "Usage: orxonox [options]" << endl;
+ infoStr << "Available options:" << endl;
+
+ for (std::map<std::string, CommandLineArgument*>::const_iterator it = inst.cmdLineArgs_.begin();
+ it != inst.cmdLineArgs_.end(); ++it)
+ {
+ if (!it->second->getShortcut().empty())
+ infoStr << " [-" << it->second->getShortcut() << "] ";
+ else
+ infoStr << " ";
+ infoStr << "--" << it->second->getName() << ' ';
+ if (it->second->getValue().isType<bool>())
+ infoStr << " ";
+ else
+ infoStr << "ARG ";
+ // fill with the necessary amount of blanks
+ infoStr << std::string(maxNameSize - it->second->getName().size(), ' ');
+ infoStr << ": " << it->second->getInformation();
+ infoStr << endl;
+ }
+ return infoStr.str();
+ }
+
+ void CommandLineParser::generateDoc(std::ofstream& file)
+ {
+ file << "/** @page cmdargspage Command Line Arguments Reference" << endl;
+ file << " @verbatim"; /*no endl*/
+ file << getUsageInformation(); /*no endl*/
+ file << " @endverbatim" << endl;
+ file << "*/" << endl;
+ }
+
+ /**
+ @brief
+ Retrieves a CommandLineArgument.
+ The method throws an exception if 'name' was not found or the value could not be converted.
+ @note
+ You should of course not call this method before the command line has been parsed.
+ */
+ const CommandLineArgument* CommandLineParser::getArgument(const std::string& name)
+ {
+ std::map<std::string, CommandLineArgument*>::const_iterator it = _getInstance().cmdLineArgs_.find(name);
+ if (it == _getInstance().cmdLineArgs_.end())
+ {
+ ThrowException(Argument, "Could find command line argument '" + name + "'.");
+ }
+ else
+ {
+ return it->second;
+ }
+ }
+}
Copied: code/branches/core7/src/libraries/core/commandline/CommandLineParser.h (from rev 10328, code/branches/core7/src/libraries/core/config/CommandLineParser.h)
===================================================================
--- code/branches/core7/src/libraries/core/commandline/CommandLineParser.h (rev 0)
+++ code/branches/core7/src/libraries/core/commandline/CommandLineParser.h 2015-04-04 11:34:39 UTC (rev 10343)
@@ -0,0 +1,227 @@
+/*
+ * ORXONOX - the hottest 3D action shooter ever to exist
+ * > www.orxonox.net <
+ *
+ *
+ * License notice:
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * Author:
+ * Reto Grieder
+ * Co-authors:
+ * ...
+ *
+ */
+
+/**
+ @defgroup CmdArgs Commandline arguments
+ @ingroup Config
+ @brief For a reference of all commandline arguments see @ref cmdargspage
+*/
+
+/**
+ @file
+ @ingroup Config CmdArgs
+ @brief Declaration of CommandLineParser and CommandLineArgument, definition of the SetCommandLineArgument() macros.
+*/
+
+#ifndef _CommandLine_H__
+#define _CommandLine_H__
+
+#include "core/CorePrereqs.h"
+
+#include <fstream>
+#include <map>
+#include "util/OrxAssert.h"
+#include "util/MultiType.h"
+
+#define SetCommandLineArgument(name, defaultValue) \
+ orxonox::CommandLineArgument& CmdArgumentDummyBoolVar##name \
+ = orxonox::CommandLineParser::addArgument(#name, defaultValue)
+#define SetCommandLineSwitch(name) \
+ orxonox::CommandLineArgument& CmdArgumentDummyBoolVar##name \
+ = orxonox::CommandLineParser::addArgument(#name, false)
+
+namespace orxonox
+{
+ /**
+ @brief
+ Container class for a command line argument of any type supported by MultiType.
+
+ Whenever you want to have an option specified by a command line switch,
+ you need to first define it with SetCommandLineArgument(name, defaultValue).
+ It is then added to a map and possibly changed when the command line is being parsed.
+ If the option was not given, you can detect this by asking hasDefaultValue().
+
+ There is a possibility to define a short cut so you can write "-p 20" instead of "--port 20".
+ Note the difference between "-" and "--"!
+ Also, there is no restriction to the number of strings you add after --name.
+ So "--startVector {2, 4, 5}" is perfectly legal.
+
+ Retrieving an argument is possible with the getCommandLineArgument function of the
+ CommandLineParser class. It is a Singleton, but the public interface is static.
+ */
+ class _CoreExport CommandLineArgument
+ {
+ friend class CommandLineParser;
+
+ public:
+ //! Tells whether the value has been changed by the command line.
+ bool hasDefaultValue() const { return bHasDefaultValue_; }
+ //! Returns the name of the argument.
+ const std::string& getName() const { return name_; }
+
+ //! Returns the shortcut (example: "-p 22" for "--port 22") of the argument.
+ //! Evaluates to "" if there is none.
+ const std::string& getShortcut() const { return shortcut_; }
+ //! Sets the shortcut for the argument
+ CommandLineArgument& shortcut(const std::string& shortcut)
+ { this->shortcut_ = shortcut; return *this; }
+
+ //! Returns the usage information
+ const std::string& getInformation() const { return this->usageInformation_; }
+ //! Sets the option information when displaying orxonox usage.
+ CommandLineArgument& information(const std::string& usage)
+ { this->usageInformation_ = usage; return *this; }
+
+ //! Returns the actual value of the argument. Can be equal to default value.
+ const MultiType& getValue() const { return value_; }
+ //! Returns the given default value as type T.
+ const MultiType& getDefaultValue() const { return defaultValue_; }
+
+ private:
+ //! Constructor initialises both value_ and defaultValue_ with defaultValue.
+ CommandLineArgument(const std::string& name, const MultiType& defaultValue)
+ : bHasDefaultValue_(true)
+ , name_(name)
+ , value_(defaultValue)
+ , defaultValue_(defaultValue)
+ { }
+
+ //! Undefined copy constructor
+ CommandLineArgument(const CommandLineArgument& instance);
+ ~CommandLineArgument() { }
+
+ //! Parses the value string of a command line argument.
+ void parse(const std::string& value);
+
+ //! Tells whether the value has been changed by the command line.
+ bool bHasDefaultValue_;
+
+ private:
+ std::string name_; //!< Name of the argument
+ std::string shortcut_; //!< Shortcut of the argument. @see getShortcut().
+ std::string usageInformation_; //!< Tells about the usage of this parameter
+
+ MultiType value_; //!< The actual value
+ MultiType defaultValue_; //!< Default value. Should not be changed.
+ };
+
+
+ /**
+ @brief
+ Global interface to command line options.
+ Allows to add and retrieve command line arguments. Also does the parsing.
+ @note
+ Internally it is a Singleton, but the public interface is static.
+ @see
+ CommandLineArgument
+ */
+ class _CoreExport CommandLineParser
+ {
+ public:
+
+ //! Parse redirection to internal member method.
+ static void parse(const std::string& cmdLine)
+ { _getInstance()._parse(cmdLine); }
+
+ static std::string getUsageInformation();
+
+ static const CommandLineArgument* getArgument(const std::string& name);
+ //! Writes the argument value in the given parameter.
+ template <class T>
+ static void getValue(const std::string& name, T* value)
+ { *value = (T)(getArgument(name)->getValue()); }
+ static const MultiType& getValue(const std::string& name)
+ { return getArgument(name)->getValue(); }
+ template <class T>
+ static CommandLineArgument& addArgument(const std::string& name, T defaultValue);
+
+ static bool existsArgument(const std::string& name)
+ {
+ std::map<std::string, CommandLineArgument*>::const_iterator it = _getInstance().cmdLineArgs_.find(name);
+ return !(it == _getInstance().cmdLineArgs_.end());
+ }
+
+ static void destroyAllArguments();
+
+ static void generateDoc(std::ofstream& file);
+
+ private:
+ //! Constructor initialises bFirstTimeParse_ with true.
+ CommandLineParser() : bFirstTimeParse_(true) { }
+ //! Undefined copy constructor
+ CommandLineParser(const CommandLineParser& instance);
+ ~CommandLineParser();
+
+ static CommandLineParser& _getInstance();
+
+ void _parse(const std::string& cmdLine);
+ void checkFullArgument(const std::string& name, const std::string& value);
+ void checkShortcut(const std::string& shortcut, const std::string& value);
+
+ /**
+ Tells whether we parsed for the first time. The CommmandLineArguments are added before main().
+ So when we call parse() the first time, we need to create a map with all shortcuts since these
+ get added after addCommandLineArgument().
+ */
+ bool bFirstTimeParse_;
+
+ //! Holds all pointers to the arguments and serves as a search map by name.
+ std::map<std::string, CommandLineArgument*> cmdLineArgs_;
+ //! Search map by shortcut for the arguments.
+ std::map<std::string, CommandLineArgument*> cmdLineArgsShortcut_;
+ };
+
+ template <>
+ inline void CommandLineParser::getValue<std::string>(const std::string& name, std::string* value)
+ {
+ *value = getArgument(name)->getValue().get<std::string>();
+ }
+
+ /**
+ @brief
+ Adds a new CommandLineArgument to the internal map.
+ Note that only such arguments are actually valid.
+ @param name
+ Name of the argument. Shortcut can be added later.
+ @param defaultValue
+ Default value that is used when argument was not given.
+ */
+ template <class T>
+ CommandLineArgument& CommandLineParser::addArgument(const std::string& name, T defaultValue)
+ {
+ OrxAssert(!_getInstance().existsArgument(name),
+ "Cannot add a command line argument with name '" + name + "' twice.");
+ OrxAssert(!MultiType(defaultValue).isType<bool>() || MultiType(defaultValue).get<bool>() != true,
+ "Boolean command line arguments with positive default values are not supported." << endl
+ << "Please use SetCommandLineSwitch and adjust your argument: " << name);
+
+ return *(_getInstance().cmdLineArgs_[name] = new CommandLineArgument(name, defaultValue));
+ }
+}
+
+#endif /* _CommandLine_H__ */
Modified: code/branches/core7/src/libraries/core/config/CMakeLists.txt
===================================================================
--- code/branches/core7/src/libraries/core/config/CMakeLists.txt 2015-04-04 11:24:49 UTC (rev 10342)
+++ code/branches/core7/src/libraries/core/config/CMakeLists.txt 2015-04-04 11:34:39 UTC (rev 10343)
@@ -1,5 +1,4 @@
ADD_SOURCE_FILES(CORE_SRC_FILES
- CommandLineParser.cc
# ConfigFile.cc is already included in FilesystemBuildUnit.cc
ConfigFileEntryValue.cc
ConfigFileEntryVectorValue.cc
Deleted: code/branches/core7/src/libraries/core/config/CommandLineParser.cc
===================================================================
--- code/branches/core7/src/libraries/core/config/CommandLineParser.cc 2015-04-04 11:24:49 UTC (rev 10342)
+++ code/branches/core7/src/libraries/core/config/CommandLineParser.cc 2015-04-04 11:34:39 UTC (rev 10343)
@@ -1,340 +0,0 @@
-/*
- * ORXONOX - the hottest 3D action shooter ever to exist
- * > www.orxonox.net <
- *
- *
- * License notice:
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- *
- * Author:
- * Reto Grieder
- * Co-authors:
- * ...
- *
- */
-
-#include "CommandLineParser.h"
-
-#include <algorithm>
-#include <sstream>
-
-#include "util/Convert.h"
-#include "util/Output.h"
-#include "util/Exception.h"
-#include "util/StringUtils.h"
-#include "util/SubString.h"
-#include "core/PathConfig.h"
-
-namespace orxonox
-{
- /**
- @brief
- Parses a value string for a command line argument.
- It simply uses convertValue(Output, Input) to do that.
- Bools are treated specially. That is necessary
- so that you can have simple command line switches.
- */
- void CommandLineArgument::parse(const std::string& value)
- {
- if (value_.isType<bool>())
- {
- // simulate command line switch
- bool temp;
- if (convertValue(&temp, value))
- {
- this->bHasDefaultValue_ = false;
- this->value_ = temp;
- }
- else if (value.empty())
- {
- this->bHasDefaultValue_ = false;
- this->value_ = true;
- }
- else
- ThrowException(Argument, "Could not read command line argument '" + getName() + "'.");
- }
- else
- {
- if (!value_.set(value))
- {
- value_.set(defaultValue_);
- ThrowException(Argument, "Could not read command line argument '" + getName() + "'.");
- }
- else
- this->bHasDefaultValue_ = false;
- }
- }
-
-
- /**
- @brief
- Destructor destroys all CommandLineArguments with it.
- */
- CommandLineParser::~CommandLineParser()
- {
- CommandLineParser::destroyAllArguments();
- }
-
- /**
- @brief
- Returns a unique instance (Meyers Singleton).
- */
- CommandLineParser& CommandLineParser::_getInstance()
- {
- static CommandLineParser instance;
- return instance;
- }
-
- /**
- @brief
- Destroys all command line arguments. This should be called at the end
- of main. Do not use before that.
- */
- void CommandLineParser::destroyAllArguments()
- {
- for (std::map<std::string, CommandLineArgument*>::const_iterator it = _getInstance().cmdLineArgs_.begin();
- it != _getInstance().cmdLineArgs_.end(); ++it)
- delete it->second;
- _getInstance().cmdLineArgs_.clear();
- }
-
- /** Parses the command line string for arguments and stores these.
- @note
- The reason that you have to provide the string to be parsed as
- space separated list is because of argc and argv. If you only have
- a whole string, simply use getAllStrings() of SubString.
- @param cmdLine
- Command line string WITHOUT the execution path.
- */
- void CommandLineParser::_parse(const std::string& cmdLine)
- {
- std::vector<std::string> arguments;
- SubString tokens(cmdLine, " ", " ", false, '\\', true, '"', true, '\0', '\0', false);
- for (unsigned i = 0; i < tokens.size(); ++i)
- arguments.push_back(tokens[i]);
-
- try
- {
- // why this? See bFirstTimeParse_ declaration.
- if (bFirstTimeParse_)
- {
- // first shove all the shortcuts in a map
- for (std::map<std::string, CommandLineArgument*>::const_iterator it = cmdLineArgs_.begin();
- it != cmdLineArgs_.end(); ++it)
- {
- OrxAssert(cmdLineArgsShortcut_.find(it->second->getShortcut()) == cmdLineArgsShortcut_.end(),
- "Cannot have two command line shortcut with the same name.");
- if (!it->second->getShortcut().empty())
- cmdLineArgsShortcut_[it->second->getShortcut()] = it->second;
- }
- bFirstTimeParse_ = false;
- }
-
- std::string name;
- std::string shortcut;
- std::string value;
- for (unsigned int i = 0; i < arguments.size(); ++i)
- {
- if (arguments[i].size() != 0)
- {
- // sure not ""
- if (arguments[i][0] == '-')
- {
- // start with "-"
- if (arguments[i].size() == 1)
- {
- // argument[i] is "-", probably a minus sign
- value += "- ";
- }
- else if (arguments[i][1] <= 57 && arguments[i][1] >= 48)
- {
- // negative number as a value
- value += arguments[i] + ' ';
- }
- else
- {
- // can be shortcut or full name argument
-
- // save old data first
- value = removeTrailingWhitespaces(value);
- if (!name.empty())
- {
- checkFullArgument(name, value);
- name.clear();
- assert(shortcut.empty());
- }
- else if (!shortcut.empty())
- {
- checkShortcut(shortcut, value);
- shortcut.clear();
- assert(name.empty());
- }
-
- if (arguments[i][1] == '-')
- {
- // full name argument with "--name"
- name = arguments[i].substr(2);
- }
- else
- {
- // shortcut with "-s"
- shortcut = arguments[i].substr(1);
- }
-
- // reset value string
- value.clear();
- }
- }
- else
- {
- // value string
-
- if (name.empty() && shortcut.empty())
- {
- ThrowException(Argument, "Expected \"-\" or \"-\" in command line arguments.\n");
- }
-
- // Concatenate strings as long as there's no new argument by "-" or "--"
- value += arguments[i] + ' ';
- }
- }
- }
-
- // parse last argument
- value = removeTrailingWhitespaces(value);
- if (!name.empty())
- {
- checkFullArgument(name, value);
- assert(shortcut.empty());
- }
- else if (!shortcut.empty())
- {
- checkShortcut(shortcut, value);
- assert(name.empty());
- }
- }
- catch (const ArgumentException& ex)
- {
- orxout(user_error) << "Could not parse command line: " << ex.what() << endl;
- orxout(user_error) << CommandLineParser::getUsageInformation() << endl;
- throw GeneralException("");
- }
- }
-
- /**
- @brief
- Parses an argument based on its full name.
- @param name
- Full name of the argument
- @param value
- String containing the value
- @param bParsingFile
- Parsing a file or the command line itself
- */
- void CommandLineParser::checkFullArgument(const std::string& name, const std::string& value)
- {
- std::map<std::string, CommandLineArgument*>::const_iterator it = cmdLineArgs_.find(name);
- if (it == cmdLineArgs_.end())
- ThrowException(Argument, "Command line argument '" + name + "' does not exist.");
-
- it->second->parse(value);
- }
-
- /**
- @brief
- Parses an argument based on its shortcut.
- @param shortcut
- Shortcut to the argument
- @param value
- String containing the value
- @param bParsingFile
- Parsing a file or the command line itself
- */
- void CommandLineParser::checkShortcut(const std::string& shortcut, const std::string& value)
- {
- std::map<std::string, CommandLineArgument*>::const_iterator it = cmdLineArgsShortcut_.find(shortcut);
- if (it == cmdLineArgsShortcut_.end())
- ThrowException(Argument, "Command line shortcut '" + shortcut + "' does not exist.");
-
- it->second->parse(value);
- }
-
- std::string CommandLineParser::getUsageInformation()
- {
- CommandLineParser& inst = _getInstance();
- std::ostringstream infoStr;
-
- // determine maximum name size
- size_t maxNameSize = 0;
- for (std::map<std::string, CommandLineArgument*>::const_iterator it = inst.cmdLineArgs_.begin();
- it != inst.cmdLineArgs_.end(); ++it)
- {
- maxNameSize = std::max(it->second->getName().size(), maxNameSize);
- }
-
- infoStr << endl;
- infoStr << "Usage: orxonox [options]" << endl;
- infoStr << "Available options:" << endl;
-
- for (std::map<std::string, CommandLineArgument*>::const_iterator it = inst.cmdLineArgs_.begin();
- it != inst.cmdLineArgs_.end(); ++it)
- {
- if (!it->second->getShortcut().empty())
- infoStr << " [-" << it->second->getShortcut() << "] ";
- else
- infoStr << " ";
- infoStr << "--" << it->second->getName() << ' ';
- if (it->second->getValue().isType<bool>())
- infoStr << " ";
- else
- infoStr << "ARG ";
- // fill with the necessary amount of blanks
- infoStr << std::string(maxNameSize - it->second->getName().size(), ' ');
- infoStr << ": " << it->second->getInformation();
- infoStr << endl;
- }
- return infoStr.str();
- }
-
- void CommandLineParser::generateDoc(std::ofstream& file)
- {
- file << "/** @page cmdargspage Command Line Arguments Reference" << endl;
- file << " @verbatim"; /*no endl*/
- file << getUsageInformation(); /*no endl*/
- file << " @endverbatim" << endl;
- file << "*/" << endl;
- }
-
- /**
- @brief
- Retrieves a CommandLineArgument.
- The method throws an exception if 'name' was not found or the value could not be converted.
- @note
- You should of course not call this method before the command line has been parsed.
- */
- const CommandLineArgument* CommandLineParser::getArgument(const std::string& name)
- {
- std::map<std::string, CommandLineArgument*>::const_iterator it = _getInstance().cmdLineArgs_.find(name);
- if (it == _getInstance().cmdLineArgs_.end())
- {
- ThrowException(Argument, "Could find command line argument '" + name + "'.");
- }
- else
- {
- return it->second;
- }
- }
-}
Deleted: code/branches/core7/src/libraries/core/config/CommandLineParser.h
===================================================================
--- code/branches/core7/src/libraries/core/config/CommandLineParser.h 2015-04-04 11:24:49 UTC (rev 10342)
+++ code/branches/core7/src/libraries/core/config/CommandLineParser.h 2015-04-04 11:34:39 UTC (rev 10343)
@@ -1,227 +0,0 @@
-/*
- * ORXONOX - the hottest 3D action shooter ever to exist
- * > www.orxonox.net <
- *
- *
- * License notice:
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- *
- * Author:
- * Reto Grieder
- * Co-authors:
- * ...
- *
- */
-
-/**
- @defgroup CmdArgs Commandline arguments
- @ingroup Config
- @brief For a reference of all commandline arguments see @ref cmdargspage
-*/
-
-/**
- @file
- @ingroup Config CmdArgs
- @brief Declaration of CommandLineParser and CommandLineArgument, definition of the SetCommandLineArgument() macros.
-*/
-
-#ifndef _CommandLine_H__
-#define _CommandLine_H__
-
-#include "core/CorePrereqs.h"
-
-#include <fstream>
-#include <map>
-#include "util/OrxAssert.h"
-#include "util/MultiType.h"
-
-#define SetCommandLineArgument(name, defaultValue) \
- orxonox::CommandLineArgument& CmdArgumentDummyBoolVar##name \
- = orxonox::CommandLineParser::addArgument(#name, defaultValue)
-#define SetCommandLineSwitch(name) \
- orxonox::CommandLineArgument& CmdArgumentDummyBoolVar##name \
- = orxonox::CommandLineParser::addArgument(#name, false)
-
-namespace orxonox
-{
- /**
- @brief
- Container class for a command line argument of any type supported by MultiType.
-
- Whenever you want to have an option specified by a command line switch,
- you need to first define it with SetCommandLineArgument(name, defaultValue).
- It is then added to a map and possibly changed when the command line is being parsed.
- If the option was not given, you can detect this by asking hasDefaultValue().
-
- There is a possibility to define a short cut so you can write "-p 20" instead of "--port 20".
- Note the difference between "-" and "--"!
- Also, there is no restriction to the number of strings you add after --name.
- So "--startVector {2, 4, 5}" is perfectly legal.
-
- Retrieving an argument is possible with the getCommandLineArgument function of the
- CommandLineParser class. It is a Singleton, but the public interface is static.
- */
- class _CoreExport CommandLineArgument
- {
- friend class CommandLineParser;
-
- public:
- //! Tells whether the value has been changed by the command line.
- bool hasDefaultValue() const { return bHasDefaultValue_; }
- //! Returns the name of the argument.
- const std::string& getName() const { return name_; }
-
- //! Returns the shortcut (example: "-p 22" for "--port 22") of the argument.
- //! Evaluates to "" if there is none.
- const std::string& getShortcut() const { return shortcut_; }
- //! Sets the shortcut for the argument
- CommandLineArgument& shortcut(const std::string& shortcut)
- { this->shortcut_ = shortcut; return *this; }
-
- //! Returns the usage information
- const std::string& getInformation() const { return this->usageInformation_; }
- //! Sets the option information when displaying orxonox usage.
- CommandLineArgument& information(const std::string& usage)
- { this->usageInformation_ = usage; return *this; }
-
- //! Returns the actual value of the argument. Can be equal to default value.
- const MultiType& getValue() const { return value_; }
- //! Returns the given default value as type T.
- const MultiType& getDefaultValue() const { return defaultValue_; }
-
- private:
- //! Constructor initialises both value_ and defaultValue_ with defaultValue.
- CommandLineArgument(const std::string& name, const MultiType& defaultValue)
- : bHasDefaultValue_(true)
- , name_(name)
- , value_(defaultValue)
- , defaultValue_(defaultValue)
- { }
-
- //! Undefined copy constructor
- CommandLineArgument(const CommandLineArgument& instance);
- ~CommandLineArgument() { }
-
- //! Parses the value string of a command line argument.
- void parse(const std::string& value);
-
- //! Tells whether the value has been changed by the command line.
- bool bHasDefaultValue_;
-
- private:
- std::string name_; //!< Name of the argument
- std::string shortcut_; //!< Shortcut of the argument. @see getShortcut().
- std::string usageInformation_; //!< Tells about the usage of this parameter
-
- MultiType value_; //!< The actual value
- MultiType defaultValue_; //!< Default value. Should not be changed.
- };
-
-
- /**
- @brief
- Global interface to command line options.
- Allows to add and retrieve command line arguments. Also does the parsing.
- @note
- Internally it is a Singleton, but the public interface is static.
- @see
- CommandLineArgument
- */
- class _CoreExport CommandLineParser
- {
- public:
-
- //! Parse redirection to internal member method.
- static void parse(const std::string& cmdLine)
- { _getInstance()._parse(cmdLine); }
-
- static std::string getUsageInformation();
-
- static const CommandLineArgument* getArgument(const std::string& name);
- //! Writes the argument value in the given parameter.
- template <class T>
- static void getValue(const std::string& name, T* value)
- { *value = (T)(getArgument(name)->getValue()); }
- static const MultiType& getValue(const std::string& name)
- { return getArgument(name)->getValue(); }
- template <class T>
- static CommandLineArgument& addArgument(const std::string& name, T defaultValue);
-
- static bool existsArgument(const std::string& name)
- {
- std::map<std::string, CommandLineArgument*>::const_iterator it = _getInstance().cmdLineArgs_.find(name);
- return !(it == _getInstance().cmdLineArgs_.end());
- }
-
- static void destroyAllArguments();
-
- static void generateDoc(std::ofstream& file);
-
- private:
- //! Constructor initialises bFirstTimeParse_ with true.
- CommandLineParser() : bFirstTimeParse_(true) { }
- //! Undefined copy constructor
- CommandLineParser(const CommandLineParser& instance);
- ~CommandLineParser();
-
- static CommandLineParser& _getInstance();
-
- void _parse(const std::string& cmdLine);
- void checkFullArgument(const std::string& name, const std::string& value);
- void checkShortcut(const std::string& shortcut, const std::string& value);
-
- /**
- Tells whether we parsed for the first time. The CommmandLineArguments are added before main().
- So when we call parse() the first time, we need to create a map with all shortcuts since these
- get added after addCommandLineArgument().
- */
- bool bFirstTimeParse_;
-
- //! Holds all pointers to the arguments and serves as a search map by name.
- std::map<std::string, CommandLineArgument*> cmdLineArgs_;
- //! Search map by shortcut for the arguments.
- std::map<std::string, CommandLineArgument*> cmdLineArgsShortcut_;
- };
-
- template <>
- inline void CommandLineParser::getValue<std::string>(const std::string& name, std::string* value)
- {
- *value = getArgument(name)->getValue().get<std::string>();
- }
-
- /**
- @brief
- Adds a new CommandLineArgument to the internal map.
- Note that only such arguments are actually valid.
- @param name
- Name of the argument. Shortcut can be added later.
- @param defaultValue
- Default value that is used when argument was not given.
- */
- template <class T>
- CommandLineArgument& CommandLineParser::addArgument(const std::string& name, T defaultValue)
- {
- OrxAssert(!_getInstance().existsArgument(name),
- "Cannot add a command line argument with name '" + name + "' twice.");
- OrxAssert(!MultiType(defaultValue).isType<bool>() || MultiType(defaultValue).get<bool>() != true,
- "Boolean command line arguments with positive default values are not supported." << endl
- << "Please use SetCommandLineSwitch and adjust your argument: " << name);
-
- return *(_getInstance().cmdLineArgs_[name] = new CommandLineArgument(name, defaultValue));
- }
-}
-
-#endif /* _CommandLine_H__ */
Modified: code/branches/core7/src/libraries/core/input/InputManager.cc
===================================================================
--- code/branches/core7/src/libraries/core/input/InputManager.cc 2015-04-04 11:24:49 UTC (rev 10342)
+++ code/branches/core7/src/libraries/core/input/InputManager.cc 2015-04-04 11:34:39 UTC (rev 10343)
@@ -47,7 +47,7 @@
#include "core/CoreIncludes.h"
#include "core/GraphicsManager.h"
#include "core/config/ConfigValueIncludes.h"
-#include "core/config/CommandLineParser.h"
+#include "core/commandline/CommandLineParser.h"
#include "core/command/ConsoleCommand.h"
#include "core/command/Functor.h"
Modified: code/branches/core7/src/libraries/network/Client.cc
===================================================================
--- code/branches/core7/src/libraries/network/Client.cc 2015-04-04 11:24:49 UTC (rev 10342)
+++ code/branches/core7/src/libraries/network/Client.cc 2015-04-04 11:34:39 UTC (rev 10343)
@@ -51,7 +51,7 @@
#include "FunctionCallManager.h"
#include "core/CoreIncludes.h"
#include "core/Game.h"
-#include "core/config/CommandLineParser.h"
+#include "core/commandline/CommandLineParser.h"
namespace orxonox
{
Modified: code/branches/core7/src/orxonox/LevelManager.cc
===================================================================
--- code/branches/core7/src/orxonox/LevelManager.cc 2015-04-04 11:24:49 UTC (rev 10342)
+++ code/branches/core7/src/orxonox/LevelManager.cc 2015-04-04 11:34:39 UTC (rev 10343)
@@ -36,7 +36,7 @@
#include <map>
#include "util/ScopedSingletonManager.h"
-#include "core/config/CommandLineParser.h"
+#include "core/commandline/CommandLineParser.h"
#include "core/config/ConfigValueIncludes.h"
#include "core/CoreIncludes.h"
#include "core/ClassTreeMask.h"
Modified: code/branches/core7/src/orxonox/Main.cc
===================================================================
--- code/branches/core7/src/orxonox/Main.cc 2015-04-04 11:24:49 UTC (rev 10342)
+++ code/branches/core7/src/orxonox/Main.cc 2015-04-04 11:34:39 UTC (rev 10343)
@@ -35,7 +35,7 @@
#include "Main.h"
-#include "core/config/CommandLineParser.h"
+#include "core/commandline/CommandLineParser.h"
#include "core/Game.h"
#include "core/LuaState.h"
Modified: code/branches/core7/src/orxonox/gamestates/GSClient.cc
===================================================================
--- code/branches/core7/src/orxonox/gamestates/GSClient.cc 2015-04-04 11:24:49 UTC (rev 10342)
+++ code/branches/core7/src/orxonox/gamestates/GSClient.cc 2015-04-04 11:34:39 UTC (rev 10343)
@@ -29,7 +29,7 @@
#include "GSClient.h"
#include "util/Exception.h"
-#include "core/config/CommandLineParser.h"
+#include "core/commandline/CommandLineParser.h"
#include "core/Game.h"
#include "core/GameMode.h"
#include "network/Client.h"
Modified: code/branches/core7/src/orxonox/gamestates/GSServer.cc
===================================================================
--- code/branches/core7/src/orxonox/gamestates/GSServer.cc 2015-04-04 11:24:49 UTC (rev 10342)
+++ code/branches/core7/src/orxonox/gamestates/GSServer.cc 2015-04-04 11:34:39 UTC (rev 10343)
@@ -29,7 +29,7 @@
#include "GSServer.h"
#include "util/Output.h"
-#include "core/config/CommandLineParser.h"
+#include "core/commandline/CommandLineParser.h"
#include "core/Game.h"
#include "core/GameMode.h"
#include "network/Server.h"
More information about the Orxonox-commit
mailing list