Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 3246


Ignore:
Timestamp:
Jun 29, 2009, 1:43:41 PM (15 years ago)
Author:
rgrieder
Message:

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).

Location:
code/branches/core4/src/core
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • code/branches/core4/src/core/CommandLine.cc

    r3196 r3246  
    4040namespace orxonox
    4141{
    42     SetCommandLineArgument(optionsFile, "start.ini").shortcut("o");
     42    SetCommandLineOnlyArgument(optionsFile, "start.ini").shortcut("o");
    4343
    4444    /**
     
    4949        so that you can have simple command line switches.
    5050    */
    51     void CommandLineArgument::parse(const std::string& value)
    52     {
     51    void CommandLineArgument::parse(const std::string& value, bool bParsingFile)
     52    {
     53        if (bParsingFile && this->bCommandLineOnly_)
     54            ThrowException(Argument, "Command line argument '" + getName() + "' is not allowed in files.");
    5355        if (value_.getType() == MT_bool)
    5456        {
     
    6668            }
    6769            else
    68             {
    6970                ThrowException(Argument, "Could not read command line argument '" + getName() + "'.");
    70             }
    7171        }
    7272        else
     
    126126        Vector of space separated strings.
    127127    */
    128     void CommandLine::_parse(const std::vector<std::string>& arguments)
    129     {
    130         // why this? See bFirstTimeParse_ declaration.
    131         if (bFirstTimeParse_)
    132         {
    133             // first shove all the shortcuts in a map
    134             for (std::map<std::string, CommandLineArgument*>::const_iterator it = cmdLineArgs_.begin();
    135                 it != cmdLineArgs_.end(); ++it)
    136             {
    137                 OrxAssert(cmdLineArgsShortcut_.find(it->second->getShortcut()) == cmdLineArgsShortcut_.end(),
    138                     "Cannot have two command line shortcut with the same name.");
    139                 if (it->second->getShortcut() != "")
    140                     cmdLineArgsShortcut_[it->second->getShortcut()] = it->second;
    141             }
    142             bFirstTimeParse_ = false;
    143         }
    144 
    145         std::string name;
    146         std::string shortcut;
    147         std::string value;
    148         for (unsigned int i = 0; i < arguments.size(); ++i)
    149         {
    150             if (arguments[i].size() != 0)
    151             {
    152                 // sure not ""
    153                 if (arguments[i][0] == '-')
     128    void CommandLine::_parse(const std::vector<std::string>& arguments, bool bParsingFile)
     129    {
     130        try
     131        {
     132            // why this? See bFirstTimeParse_ declaration.
     133            if (bFirstTimeParse_)
     134            {
     135                // first shove all the shortcuts in a map
     136                for (std::map<std::string, CommandLineArgument*>::const_iterator it = cmdLineArgs_.begin();
     137                    it != cmdLineArgs_.end(); ++it)
    154138                {
    155                     // start with "-"
    156                     if (arguments[i].size() == 1)
     139                    OrxAssert(cmdLineArgsShortcut_.find(it->second->getShortcut()) == cmdLineArgsShortcut_.end(),
     140                        "Cannot have two command line shortcut with the same name.");
     141                    if (it->second->getShortcut() != "")
     142                        cmdLineArgsShortcut_[it->second->getShortcut()] = it->second;
     143                }
     144                bFirstTimeParse_ = false;
     145            }
     146
     147            std::string name;
     148            std::string shortcut;
     149            std::string value;
     150            for (unsigned int i = 0; i < arguments.size(); ++i)
     151            {
     152                if (arguments[i].size() != 0)
     153                {
     154                    // sure not ""
     155                    if (arguments[i][0] == '-')
    157156                    {
    158                         // argument[i] is "-", probably a minus sign
    159                         value += "- ";
    160                     }
    161                     else if (arguments[i][1] <= 57 && arguments[i][1] >= 48)
    162                     {
    163                         // negative number as a value
    164                         value += arguments[i] + " ";
     157                        // start with "-"
     158                        if (arguments[i].size() == 1)
     159                        {
     160                            // argument[i] is "-", probably a minus sign
     161                            value += "- ";
     162                        }
     163                        else if (arguments[i][1] <= 57 && arguments[i][1] >= 48)
     164                        {
     165                            // negative number as a value
     166                            value += arguments[i] + " ";
     167                        }
     168                        else
     169                        {
     170                            // can be shortcut or full name argument
     171
     172                            // save old data first
     173                            value = removeTrailingWhitespaces(value);
     174                            if (name != "")
     175                            {
     176                                checkFullArgument(name, value, bParsingFile);
     177                                name = "";
     178                                assert(shortcut == "");
     179                            }
     180                            else if (shortcut != "")
     181                            {
     182                                checkShortcut(shortcut, value, bParsingFile);
     183                                shortcut = "";
     184                                assert(name == "");
     185                            }
     186
     187                            if (arguments[i][1] == '-')
     188                            {
     189                                // full name argument with "--name"
     190                                name = arguments[i].substr(2);
     191                            }
     192                            else
     193                            {
     194                                // shortcut with "-s"
     195                                shortcut = arguments[i].substr(1);
     196                            }
     197
     198                            // reset value string
     199                            value = "";
     200                        }
    165201                    }
    166202                    else
    167203                    {
    168                         // can be shortcut or full name argument
    169 
    170                         // save old data first
    171                         value = removeTrailingWhitespaces(value);
    172                         if (name != "")
     204                        // value string
     205
     206                        if (name == "" && shortcut == "")
    173207                        {
    174                             checkFullArgument(name, value);
    175                             name = "";
    176                             assert(shortcut == "");
     208                            ThrowException(Argument, "Expected \"-\" or \"-\" in command line arguments.\n");
    177209                        }
    178                         else if (shortcut != "")
    179                         {
    180                             checkShortcut(shortcut, value);
    181                             shortcut = "";
    182                             assert(name == "");
    183                         }
    184 
    185                         if (arguments[i][1] == '-')
    186                         {
    187                             // full name argument with "--name"
    188                             name = arguments[i].substr(2);
    189                         }
    190                         else
    191                         {
    192                             // shortcut with "-s"
    193                             shortcut = arguments[i].substr(1);
    194                         }
    195 
    196                         // reset value string
    197                         value = "";
     210
     211                        // Concatenate strings as long as there's no new argument by "-" or "--"
     212                        value += arguments[i] + ' ';
    198213                    }
    199214                }
    200                 else
    201                 {
    202                     // value string
    203 
    204                     if (name == "" && shortcut == "")
    205                     {
    206                         ThrowException(Argument, "Expected \"-\" or \"-\" in command line arguments.\n");
    207                     }
    208 
    209                     // Concatenate strings as long as there's no new argument by "-" or "--"
    210                     value += arguments[i] + ' ';
    211                 }
    212             }
    213         }
    214 
    215         // parse last argument
    216         value = removeTrailingWhitespaces(value);
    217         if (name != "")
    218         {
    219             checkFullArgument(name, value);
    220             assert(shortcut == "");
    221         }
    222         else if (shortcut != "")
    223         {
    224             checkShortcut(shortcut, value);
    225             assert(name == "");
     215            }
     216
     217            // parse last argument
     218            value = removeTrailingWhitespaces(value);
     219            if (name != "")
     220            {
     221                checkFullArgument(name, value, bParsingFile);
     222                assert(shortcut == "");
     223            }
     224            else if (shortcut != "")
     225            {
     226                checkShortcut(shortcut, value, bParsingFile);
     227                assert(name == "");
     228            }
     229        }
     230        catch (const ArgumentException& ex)
     231        {
     232            COUT(0) << "Could not parse command line (including additional files): " << ex.what() << std::endl;
     233            COUT(0) << "Usage:" << std::endl << "orxonox " << CommandLine::getUsageInformation() << std::endl;
     234            throw GeneralException("");
    226235        }
    227236    }
     
    235244        String containing the value
    236245    */
    237     void CommandLine::checkFullArgument(const std::string& name, const std::string& value)
     246    void CommandLine::checkFullArgument(const std::string& name, const std::string& value, bool bParsingFile)
    238247    {
    239248        std::map<std::string, CommandLineArgument*>::const_iterator it = cmdLineArgs_.find(name);
     
    241250            ThrowException(Argument, "Command line argument '" + name + "' does not exist.");
    242251
    243         it->second->parse(value);
     252        it->second->parse(value, bParsingFile);
    244253    }
    245254
     
    252261        String containing the value
    253262    */
    254     void CommandLine::checkShortcut(const std::string& shortcut, const std::string& value)
     263    void CommandLine::checkShortcut(const std::string& shortcut, const std::string& value, bool bParsingFile)
    255264    {
    256265        std::map<std::string, CommandLineArgument*>::const_iterator it = cmdLineArgsShortcut_.find(shortcut);
     
    258267            ThrowException(Argument, "Command line shortcut '" + shortcut + "' does not exist.");
    259268
    260         it->second->parse(value);
     269        it->second->parse(value, bParsingFile);
    261270    }
    262271
     
    295304    /**
    296305    @brief
    297         Parses both command line and start.ini for CommandLineArguments.
    298     */
    299     void CommandLine::_parseAll(int argc, char** argv)
    300     {
    301         // parse command line first
     306        Parses only the command line for CommandLineArguments.
     307    */
     308    void CommandLine::_parseCommandLine(int argc, char** argv)
     309    {
    302310        std::vector<std::string> args;
    303311        for (int i = 1; i < argc; ++i)
    304312            args.push_back(argv[i]);
    305         this->_parse(args);
    306 
     313        this->_parse(args, false);
     314    }
     315
     316    /**
     317    @brief
     318        Parses start.ini (or the file specified with --optionsFile) for CommandLineArguments.
     319    */
     320    void CommandLine::_parseFile()
     321    {
    307322        std::string filename = CommandLine::getValue("optionsFile").getString();
    308323        boost::filesystem::path filepath(Core::getConfigPath() / filename);
     
    312327        std::ifstream file;
    313328        file.open(filepath.string().c_str());
    314         args.clear();
     329        std::vector<std::string> args;
    315330        if (file)
    316331        {
     
    332347        }
    333348
    334         try
    335         {
    336             _parse(args);
    337         }
    338         catch (orxonox::ArgumentException& ex)
    339         {
    340             COUT(1) << "An Exception occured while parsing " << filename << std::endl;
    341             throw(ex);
    342         }
     349        _parse(args, true);
    343350    }
    344351}
  • code/branches/core4/src/core/CommandLine.h

    r3196 r3246  
    3838#define SetCommandLineArgument(name, defaultValue) \
    3939    orxonox::CommandLineArgument& CmdArgumentDummyBoolVar##name \
    40     = orxonox::CommandLine::addArgument(#name, defaultValue)
     40    = orxonox::CommandLine::addArgument(#name, defaultValue, false)
     41#define SetCommandLineOnlyArgument(name, defaultValue) \
     42    orxonox::CommandLineArgument& CmdArgumentDummyBoolVar##name \
     43    = orxonox::CommandLine::addArgument(#name, defaultValue, true)
    4144#define SetCommandLineSwitch(name) \
    4245    orxonox::CommandLineArgument& CmdArgumentDummyBoolVar##name \
    43     = orxonox::CommandLine::addArgument(#name, false)
     46    = orxonox::CommandLine::addArgument(#name, false, false)
     47#define SetCommandLineOnlySwitch(name) \
     48    orxonox::CommandLineArgument& CmdArgumentDummyBoolVar##name \
     49    = orxonox::CommandLine::addArgument(#name, false, true)
    4450
    4551
     
    9399    private:
    94100        //! Constructor initialises both value_ and defaultValue_ with defaultValue.
    95         CommandLineArgument(const std::string& name, const MultiType& defaultValue)
     101        CommandLineArgument(const std::string& name, const MultiType& defaultValue, bool bCommandLineOnly)
    96102            : bHasDefaultValue_(true)
    97103            , name_(name)
    98104            , value_(defaultValue)
    99105            , defaultValue_(defaultValue)
     106            , bCommandLineOnly_(bCommandLineOnly)
    100107        { }
    101108
     
    105112
    106113        //! Parses the value string of a command line argument.
    107         void parse(const std::string& value);
     114        void parse(const std::string& value, bool bParsingFile);
    108115
    109116        //! Tells whether the value has been changed by the command line.
     
    115122        std::string usageInformation_; //!< Tells about the usage of this parameter
    116123
    117         MultiType value_;            //!< The actual value
    118         MultiType defaultValue_;     //!< Default value. Should not be changed.
     124        MultiType   value_;            //!< The actual value
     125        MultiType   defaultValue_;     //!< Default value. Should not be changed.
     126        bool        bCommandLineOnly_; //!< Whether you cannot specify the value in a text file
    119127    };
    120128
     
    134142
    135143        //! Parse redirection to internal member method.
    136         static void parseAll(int argc, char** argv) { _getInstance()._parseAll(argc, argv); }
     144        static void parseCommandLine(int argc, char** argv) { _getInstance()._parseCommandLine(argc, argv); }
     145        static void parseFile() { _getInstance()._parseFile(); }
    137146
    138147        static std::string getUsageInformation();
     
    146155        { return getArgument(name)->getValue(); }
    147156        template <class T>
    148         static CommandLineArgument& addArgument(const std::string& name, T defaultValue);
     157        static CommandLineArgument& addArgument(const std::string& name, T defaultValue, bool bCommandLineOnly);
    149158
    150159        static bool existsArgument(const std::string& name)
     
    165174        static CommandLine& _getInstance();
    166175
    167         void _parseAll(int argc, char** argv);
    168         void _parse(const std::vector<std::string>& arguments);
    169         void checkFullArgument(const std::string& name, const std::string& value);
    170         void checkShortcut(const std::string& shortcut, const std::string& value);
     176        void _parseCommandLine(int argc, char** argv);
     177        void _parseFile();
     178        void _parse(const std::vector<std::string>& arguments, bool bParsingFile);
     179        void checkFullArgument(const std::string& name, const std::string& value, bool bParsingFile);
     180        void checkShortcut(const std::string& shortcut, const std::string& value, bool bParsingFile);
    171181
    172182        /**
     
    199209    */
    200210    template <class T>
    201     CommandLineArgument& CommandLine::addArgument(const std::string& name, T defaultValue)
     211    CommandLineArgument& CommandLine::addArgument(const std::string& name, T defaultValue, bool bCommandLineOnly)
    202212    {
    203213        OrxAssert(!_getInstance().existsArgument(name),
    204214            "Cannot add a command line argument with name '" + name + "' twice.");
    205215
    206         return *(_getInstance().cmdLineArgs_[name] = new CommandLineArgument(name, defaultValue));
     216        return *(_getInstance().cmdLineArgs_[name] = new CommandLineArgument(name, defaultValue, bCommandLineOnly));
    207217    }
    208218}
  • code/branches/core4/src/core/Core.cc

    r3214 r3246  
    8888
    8989    SetCommandLineArgument(mediaPath, "").information("PATH");
    90     SetCommandLineArgument(writingPathSuffix, "").information("DIR");
     90    SetCommandLineOnlyArgument(writingPathSuffix, "").information("DIR");
    9191    SetCommandLineArgument(settingsFile, "orxonox.ini");
    9292    SetCommandLineArgument(limitToCPU, 0).information("0: off | #cpu");
     
    103103    {
    104104        // Parse command line arguments fist
    105         try
    106         {
    107             CommandLine::parseAll(argc, argv);
    108         }
    109         catch (ArgumentException& ex)
    110         {
    111             COUT(1) << ex.what() << std::endl;
    112             COUT(0) << "Usage:" << std::endl << "orxonox " << CommandLine::getUsageInformation() << std::endl;
    113         }
     105        CommandLine::parseCommandLine(argc, argv);
    114106
    115107        // limit the main thread to the first core so that QueryPerformanceCounter doesn't jump
     
    137129        // Set the correct log path. Before this call, /tmp (Unix) or %TEMP% was used
    138130        OutputHandler::getOutStream().setLogPath(Core::getLogPathString());
     131
     132        CommandLine::parseFile();
    139133
    140134        // Manage ini files and set the default settings file (usually orxonox.ini)
Note: See TracChangeset for help on using the changeset viewer.