Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

File:
1 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}
Note: See TracChangeset for help on using the changeset viewer.