[Orxonox-commit 715] r3246 - branches/core4/src/core

rgrieder at orxonox.net rgrieder at orxonox.net
Mon Jun 29 13:43:41 CEST 2009


Author: rgrieder
Date: 2009-06-29 13:43:41 +0200 (Mon, 29 Jun 2009)
New Revision: 3246

Modified:
   branches/core4/src/core/CommandLine.cc
   branches/core4/src/core/CommandLine.h
   branches/core4/src/core/Core.cc
Log:
Added new feature to CommandLine: arguments that cannot be specified in a file.
--optionsFile and --writingPathSuffix are now such arguments (because of obvious dependency problems).

Modified: branches/core4/src/core/CommandLine.cc
===================================================================
--- branches/core4/src/core/CommandLine.cc	2009-06-29 11:32:40 UTC (rev 3245)
+++ branches/core4/src/core/CommandLine.cc	2009-06-29 11:43:41 UTC (rev 3246)
@@ -39,7 +39,7 @@
 
 namespace orxonox
 {
-    SetCommandLineArgument(optionsFile, "start.ini").shortcut("o");
+    SetCommandLineOnlyArgument(optionsFile, "start.ini").shortcut("o");
 
     /**
     @brief
@@ -48,8 +48,10 @@
         Bools are treated specially. That is necessary
         so that you can have simple command line switches.
     */
-    void CommandLineArgument::parse(const std::string& value)
+    void CommandLineArgument::parse(const std::string& value, bool bParsingFile)
     {
+        if (bParsingFile && this->bCommandLineOnly_)
+            ThrowException(Argument, "Command line argument '" + getName() + "' is not allowed in files.");
         if (value_.getType() == MT_bool)
         {
             // simulate command line switch
@@ -65,9 +67,7 @@
                 this->value_ = true;
             }
             else
-            {
                 ThrowException(Argument, "Could not read command line argument '" + getName() + "'.");
-            }
         }
         else
         {
@@ -125,105 +125,114 @@
     @param arguments
         Vector of space separated strings.
     */
-    void CommandLine::_parse(const std::vector<std::string>& arguments)
+    void CommandLine::_parse(const std::vector<std::string>& arguments, bool bParsingFile)
     {
-        // why this? See bFirstTimeParse_ declaration.
-        if (bFirstTimeParse_)
+        try
         {
-            // first shove all the shortcuts in a map
-            for (std::map<std::string, CommandLineArgument*>::const_iterator it = cmdLineArgs_.begin();
-                it != cmdLineArgs_.end(); ++it)
+            // why this? See bFirstTimeParse_ declaration.
+            if (bFirstTimeParse_)
             {
-                OrxAssert(cmdLineArgsShortcut_.find(it->second->getShortcut()) == cmdLineArgsShortcut_.end(),
-                    "Cannot have two command line shortcut with the same name.");
-                if (it->second->getShortcut() != "")
-                    cmdLineArgsShortcut_[it->second->getShortcut()] = it->second;
+                // 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() != "")
+                        cmdLineArgsShortcut_[it->second->getShortcut()] = it->second;
+                }
+                bFirstTimeParse_ = false;
             }
-            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)
+            std::string name;
+            std::string shortcut;
+            std::string value;
+            for (unsigned int i = 0; i < arguments.size(); ++i)
             {
-                // sure not ""
-                if (arguments[i][0] == '-')
+                if (arguments[i].size() != 0)
                 {
-                    // start with "-"
-                    if (arguments[i].size() == 1)
+                    // sure not ""
+                    if (arguments[i][0] == '-')
                     {
-                        // 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 != "")
+                        // start with "-"
+                        if (arguments[i].size() == 1)
                         {
-                            checkFullArgument(name, value);
-                            name = "";
-                            assert(shortcut == "");
+                            // argument[i] is "-", probably a minus sign
+                            value += "- ";
                         }
-                        else if (shortcut != "")
+                        else if (arguments[i][1] <= 57 && arguments[i][1] >= 48)
                         {
-                            checkShortcut(shortcut, value);
-                            shortcut = "";
-                            assert(name == "");
+                            // negative number as a value
+                            value += arguments[i] + " ";
                         }
+                        else
+                        {
+                            // can be shortcut or full name argument
 
-                        if (arguments[i][1] == '-')
-                        {
-                            // full name argument with "--name"
-                            name = arguments[i].substr(2);
+                            // save old data first
+                            value = removeTrailingWhitespaces(value);
+                            if (name != "")
+                            {
+                                checkFullArgument(name, value, bParsingFile);
+                                name = "";
+                                assert(shortcut == "");
+                            }
+                            else if (shortcut != "")
+                            {
+                                checkShortcut(shortcut, value, bParsingFile);
+                                shortcut = "";
+                                assert(name == "");
+                            }
+
+                            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 = "";
                         }
-                        else
+                    }
+                    else
+                    {
+                        // value string
+
+                        if (name == "" && shortcut == "")
                         {
-                            // shortcut with "-s"
-                            shortcut = arguments[i].substr(1);
+                            ThrowException(Argument, "Expected \"-\" or \"-\" in command line arguments.\n");
                         }
 
-                        // reset value string
-                        value = "";
+                        // Concatenate strings as long as there's no new argument by "-" or "--"
+                        value += arguments[i] + ' ';
                     }
                 }
-                else
-                {
-                    // value string
+            }
 
-                    if (name == "" && shortcut == "")
-                    {
-                        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 != "")
+            {
+                checkFullArgument(name, value, bParsingFile);
+                assert(shortcut == "");
             }
+            else if (shortcut != "")
+            {
+                checkShortcut(shortcut, value, bParsingFile);
+                assert(name == "");
+            }
         }
-
-        // parse last argument
-        value = removeTrailingWhitespaces(value);
-        if (name != "")
+        catch (const ArgumentException& ex)
         {
-            checkFullArgument(name, value);
-            assert(shortcut == "");
+            COUT(0) << "Could not parse command line (including additional files): " << ex.what() << std::endl;
+            COUT(0) << "Usage:" << std::endl << "orxonox " << CommandLine::getUsageInformation() << std::endl;
+            throw GeneralException("");
         }
-        else if (shortcut != "")
-        {
-            checkShortcut(shortcut, value);
-            assert(name == "");
-        }
     }
 
     /**
@@ -234,13 +243,13 @@
     @param value
         String containing the value
     */
-    void CommandLine::checkFullArgument(const std::string& name, const std::string& value)
+    void CommandLine::checkFullArgument(const std::string& name, const std::string& value, bool bParsingFile)
     {
         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);
+        it->second->parse(value, bParsingFile);
     }
 
     /**
@@ -251,13 +260,13 @@
     @param value
         String containing the value
     */
-    void CommandLine::checkShortcut(const std::string& shortcut, const std::string& value)
+    void CommandLine::checkShortcut(const std::string& shortcut, const std::string& value, bool bParsingFile)
     {
         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);
+        it->second->parse(value, bParsingFile);
     }
 
     std::string CommandLine::getUsageInformation()
@@ -294,16 +303,22 @@
 
     /**
     @brief
-        Parses both command line and start.ini for CommandLineArguments.
+        Parses only the command line for CommandLineArguments.
     */
-    void CommandLine::_parseAll(int argc, char** argv)
+    void CommandLine::_parseCommandLine(int argc, char** argv)
     {
-        // parse command line first
         std::vector<std::string> args;
         for (int i = 1; i < argc; ++i)
             args.push_back(argv[i]);
-        this->_parse(args);
+        this->_parse(args, false);
+    }
 
+    /**
+    @brief
+        Parses start.ini (or the file specified with --optionsFile) for CommandLineArguments.
+    */
+    void CommandLine::_parseFile()
+    {
         std::string filename = CommandLine::getValue("optionsFile").getString();
         boost::filesystem::path filepath(Core::getConfigPath() / filename);
 
@@ -311,7 +326,7 @@
         // They will not overwrite the arguments given directly
         std::ifstream file;
         file.open(filepath.string().c_str());
-        args.clear();
+        std::vector<std::string> args;
         if (file)
         {
             while (!file.eof())
@@ -331,14 +346,6 @@
             file.close();
         }
 
-        try
-        {
-            _parse(args);
-        }
-        catch (orxonox::ArgumentException& ex)
-        {
-            COUT(1) << "An Exception occured while parsing " << filename << std::endl;
-            throw(ex);
-        }
+        _parse(args, true);
     }
 }

Modified: branches/core4/src/core/CommandLine.h
===================================================================
--- branches/core4/src/core/CommandLine.h	2009-06-29 11:32:40 UTC (rev 3245)
+++ branches/core4/src/core/CommandLine.h	2009-06-29 11:43:41 UTC (rev 3246)
@@ -37,10 +37,16 @@
 
 #define SetCommandLineArgument(name, defaultValue) \
     orxonox::CommandLineArgument& CmdArgumentDummyBoolVar##name \
-    = orxonox::CommandLine::addArgument(#name, defaultValue)
+    = orxonox::CommandLine::addArgument(#name, defaultValue, false)
+#define SetCommandLineOnlyArgument(name, defaultValue) \
+    orxonox::CommandLineArgument& CmdArgumentDummyBoolVar##name \
+    = orxonox::CommandLine::addArgument(#name, defaultValue, true)
 #define SetCommandLineSwitch(name) \
     orxonox::CommandLineArgument& CmdArgumentDummyBoolVar##name \
-    = orxonox::CommandLine::addArgument(#name, false)
+    = orxonox::CommandLine::addArgument(#name, false, false)
+#define SetCommandLineOnlySwitch(name) \
+    orxonox::CommandLineArgument& CmdArgumentDummyBoolVar##name \
+    = orxonox::CommandLine::addArgument(#name, false, true)
 
 
 namespace orxonox
@@ -92,11 +98,12 @@
 
     private:
         //! Constructor initialises both value_ and defaultValue_ with defaultValue.
-        CommandLineArgument(const std::string& name, const MultiType& defaultValue)
+        CommandLineArgument(const std::string& name, const MultiType& defaultValue, bool bCommandLineOnly)
             : bHasDefaultValue_(true)
             , name_(name)
             , value_(defaultValue)
             , defaultValue_(defaultValue)
+            , bCommandLineOnly_(bCommandLineOnly)
         { }
 
         //! Undefined copy constructor
@@ -104,7 +111,7 @@
         ~CommandLineArgument() { }
 
         //! Parses the value string of a command line argument.
-        void parse(const std::string& value);
+        void parse(const std::string& value, bool bParsingFile);
 
         //! Tells whether the value has been changed by the command line.
         bool bHasDefaultValue_;
@@ -114,8 +121,9 @@
         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.
+        MultiType   value_;            //!< The actual value
+        MultiType   defaultValue_;     //!< Default value. Should not be changed.
+        bool        bCommandLineOnly_; //!< Whether you cannot specify the value in a text file
     };
 
 
@@ -133,7 +141,8 @@
     public:
 
         //! Parse redirection to internal member method.
-        static void parseAll(int argc, char** argv) { _getInstance()._parseAll(argc, argv); }
+        static void parseCommandLine(int argc, char** argv) { _getInstance()._parseCommandLine(argc, argv); }
+        static void parseFile() { _getInstance()._parseFile(); }
 
         static std::string getUsageInformation();
 
@@ -145,7 +154,7 @@
         static MultiType getValue(const std::string& name)
         { return getArgument(name)->getValue(); }
         template <class T>
-        static CommandLineArgument& addArgument(const std::string& name, T defaultValue);
+        static CommandLineArgument& addArgument(const std::string& name, T defaultValue, bool bCommandLineOnly);
 
         static bool existsArgument(const std::string& name)
         {
@@ -164,10 +173,11 @@
 
         static CommandLine& _getInstance();
 
-        void _parseAll(int argc, char** argv);
-        void _parse(const std::vector<std::string>& arguments);
-        void checkFullArgument(const std::string& name, const std::string& value);
-        void checkShortcut(const std::string& shortcut, const std::string& value);
+        void _parseCommandLine(int argc, char** argv);
+        void _parseFile();
+        void _parse(const std::vector<std::string>& arguments, bool bParsingFile);
+        void checkFullArgument(const std::string& name, const std::string& value, bool bParsingFile);
+        void checkShortcut(const std::string& shortcut, const std::string& value, bool bParsingFile);
 
         /**
             Tells whether we parsed for the first time. The CommmandLineArguments are added before main().
@@ -198,12 +208,12 @@
         Default value that is used when argument was not given.
     */
     template <class T>
-    CommandLineArgument& CommandLine::addArgument(const std::string& name, T defaultValue)
+    CommandLineArgument& CommandLine::addArgument(const std::string& name, T defaultValue, bool bCommandLineOnly)
     {
         OrxAssert(!_getInstance().existsArgument(name),
             "Cannot add a command line argument with name '" + name + "' twice.");
 
-        return *(_getInstance().cmdLineArgs_[name] = new CommandLineArgument(name, defaultValue));
+        return *(_getInstance().cmdLineArgs_[name] = new CommandLineArgument(name, defaultValue, bCommandLineOnly));
     }
 }
 

Modified: branches/core4/src/core/Core.cc
===================================================================
--- branches/core4/src/core/Core.cc	2009-06-29 11:32:40 UTC (rev 3245)
+++ branches/core4/src/core/Core.cc	2009-06-29 11:43:41 UTC (rev 3246)
@@ -87,7 +87,7 @@
     Core* Core::singletonRef_s  = 0;
 
     SetCommandLineArgument(mediaPath, "").information("PATH");
-    SetCommandLineArgument(writingPathSuffix, "").information("DIR");
+    SetCommandLineOnlyArgument(writingPathSuffix, "").information("DIR");
     SetCommandLineArgument(settingsFile, "orxonox.ini");
     SetCommandLineArgument(limitToCPU, 0).information("0: off | #cpu");
 
@@ -102,15 +102,7 @@
     void Core::initialise(int argc, char** argv)
     {
         // Parse command line arguments fist
-        try
-        {
-            CommandLine::parseAll(argc, argv);
-        }
-        catch (ArgumentException& ex)
-        {
-            COUT(1) << ex.what() << std::endl;
-            COUT(0) << "Usage:" << std::endl << "orxonox " << CommandLine::getUsageInformation() << std::endl;
-        }
+        CommandLine::parseCommandLine(argc, argv);
 
         // limit the main thread to the first core so that QueryPerformanceCounter doesn't jump
         // do this after ogre has initialised. Somehow Ogre changes the settings again (not through
@@ -137,6 +129,8 @@
         // Set the correct log path. Before this call, /tmp (Unix) or %TEMP% was used
         OutputHandler::getOutStream().setLogPath(Core::getLogPathString());
 
+        CommandLine::parseFile();
+
         // Manage ini files and set the default settings file (usually orxonox.ini)
         this->configFileManager_ = new ConfigFileManager();
         this->configFileManager_->setFilename(ConfigFileType::Settings,




More information about the Orxonox-commit mailing list