Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 8520


Ignore:
Timestamp:
May 20, 2011, 5:51:21 AM (13 years ago)
Author:
rgrieder
Message:

Removed useless and possibly confusion feature: specifying a file with additional command line arguments.
If we ever have too many command line arguments, then something is wrong anyway. And configuring a dedicated server should be done with config values (but a different file).
In general, always prefer config values to CommandLineArguments.

Location:
code/branches/unity_build/src/libraries/core
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • code/branches/unity_build/src/libraries/core/CommandLineParser.cc

    r7401 r8520  
    4141namespace orxonox
    4242{
    43     SetCommandLineOnlyArgument(optionsFile, "start.ini").shortcut("o");
    44 
    4543    /**
    4644    @brief
     
    5048        so that you can have simple command line switches.
    5149    */
    52     void CommandLineArgument::parse(const std::string& value, bool bParsingFile)
    53     {
    54         if (bParsingFile && this->bCommandLineOnly_)
    55             ThrowException(Argument, "Command line argument '" + getName() + "' is not allowed in files.");
     50    void CommandLineArgument::parse(const std::string& value)
     51    {
    5652        if (value_.getType() == MT_Type::Bool)
    5753        {
     
    116112    }
    117113
    118     /**
    119     @brief
    120         Reads the command line parses the values of each argument.
    121         It is then stored in the corresponding CommandLineArgument.
     114    /** Parses the command line string for arguments and stores these.
    122115    @note
    123116        The reason that you have to provide the string to be parsed as
    124         space separted list is because of argc and argv. If you only have
     117        space separated list is because of argc and argv. If you only have
    125118        a whole string, simply use getAllStrings() of SubString.
    126     @param arguments
    127         Vector of space separated strings.
    128     @param bParsingFile
    129         Parsing a file or the command line itself
    130     */
    131     void CommandLineParser::_parse(const std::vector<std::string>& arguments, bool bParsingFile)
    132     {
     119    @param cmdLine
     120        Command line string WITHOUT the execution path.
     121    */
     122    void CommandLineParser::_parse(const std::string& cmdLine)
     123    {
     124        std::vector<std::string> arguments;
     125        SubString tokens(cmdLine, " ", " ", false, '\\', true, '"', true, '\0', '\0', false);
     126        for (unsigned i = 0; i < tokens.size(); ++i)
     127            arguments.push_back(tokens[i]);
     128
    133129        try
    134130        {
     
    177173                            if (!name.empty())
    178174                            {
    179                                 checkFullArgument(name, value, bParsingFile);
     175                                checkFullArgument(name, value);
    180176                                name.clear();
    181177                                assert(shortcut.empty());
     
    183179                            else if (!shortcut.empty())
    184180                            {
    185                                 checkShortcut(shortcut, value, bParsingFile);
     181                                checkShortcut(shortcut, value);
    186182                                shortcut.clear();
    187183                                assert(name.empty());
     
    222218            if (!name.empty())
    223219            {
    224                 checkFullArgument(name, value, bParsingFile);
     220                checkFullArgument(name, value);
    225221                assert(shortcut.empty());
    226222            }
    227223            else if (!shortcut.empty())
    228224            {
    229                 checkShortcut(shortcut, value, bParsingFile);
     225                checkShortcut(shortcut, value);
    230226                assert(name.empty());
    231227            }
     
    233229        catch (const ArgumentException& ex)
    234230        {
    235             COUT(0) << "Could not parse command line (including additional files): " << ex.what() << std::endl;
     231            COUT(0) << "Could not parse command line: " << ex.what() << std::endl;
    236232            COUT(0) << CommandLineParser::getUsageInformation() << std::endl;
    237233            throw GeneralException("");
     
    249245        Parsing a file or the command line itself
    250246    */
    251     void CommandLineParser::checkFullArgument(const std::string& name, const std::string& value, bool bParsingFile)
     247    void CommandLineParser::checkFullArgument(const std::string& name, const std::string& value)
    252248    {
    253249        std::map<std::string, CommandLineArgument*>::const_iterator it = cmdLineArgs_.find(name);
     
    255251            ThrowException(Argument, "Command line argument '" + name + "' does not exist.");
    256252
    257         it->second->parse(value, bParsingFile);
     253        it->second->parse(value);
    258254    }
    259255
     
    268264        Parsing a file or the command line itself
    269265    */
    270     void CommandLineParser::checkShortcut(const std::string& shortcut, const std::string& value, bool bParsingFile)
     266    void CommandLineParser::checkShortcut(const std::string& shortcut, const std::string& value)
    271267    {
    272268        std::map<std::string, CommandLineArgument*>::const_iterator it = cmdLineArgsShortcut_.find(shortcut);
     
    274270            ThrowException(Argument, "Command line shortcut '" + shortcut + "' does not exist.");
    275271
    276         it->second->parse(value, bParsingFile);
     272        it->second->parse(value);
    277273    }
    278274
     
    342338        }
    343339    }
    344 
    345     /**
    346     @brief
    347         Parses only the command line for CommandLineArguments.
    348     */
    349     void CommandLineParser::_parseCommandLine(const std::string& cmdLine)
    350     {
    351         std::vector<std::string> args;
    352         SubString tokens(cmdLine, " ", " ", false, '\\', true, '"', true, '\0', '\0', false);
    353         for (unsigned i = 0; i < tokens.size(); ++i)
    354             args.push_back(tokens[i]);
    355         this->_parse(args, false);
    356     }
    357 
    358     /**
    359     @brief
    360         Parses start.ini (or the file specified with --optionsFile) for CommandLineArguments.
    361     */
    362     void CommandLineParser::_parseFile()
    363     {
    364         const std::string& filename = CommandLineParser::getValue("optionsFile").getString();
    365 
    366         // look for additional arguments in given file or start.ini as default
    367         // They will not overwrite the arguments given directly
    368         std::ifstream file;
    369         file.open((PathConfig::getConfigPathString() + filename).c_str());
    370         std::vector<std::string> args;
    371         if (file)
    372         {
    373             while (!file.eof())
    374             {
    375                 std::string line;
    376                 std::getline(file, line);
    377                 line = removeTrailingWhitespaces(line);
    378                 //if (!(line[0] == '#' || line[0] == '%'))
    379                 //{
    380                 SubString tokens(line, " ", " ", false, '\\', true, '"', true, '\0', '\0', false, '#');
    381                 for (unsigned i = 0; i < tokens.size(); ++i)
    382                     if (tokens[i][0] != '#')
    383                         args.push_back(tokens[i]);
    384                 //args.insert(args.end(), tokens.getAllStrings().begin(), tokens.getAllStrings().end());
    385                 //}
    386             }
    387             file.close();
    388         }
    389 
    390         _parse(args, true);
    391     }
    392340}
  • code/branches/unity_build/src/libraries/core/CommandLineParser.h

    r7401 r8520  
    5151#define SetCommandLineArgument(name, defaultValue) \
    5252    orxonox::CommandLineArgument& CmdArgumentDummyBoolVar##name \
    53     = orxonox::CommandLineParser::addArgument(#name, defaultValue, false)
    54 #define SetCommandLineOnlyArgument(name, defaultValue) \
    55     orxonox::CommandLineArgument& CmdArgumentDummyBoolVar##name \
    56     = orxonox::CommandLineParser::addArgument(#name, defaultValue, true)
     53    = orxonox::CommandLineParser::addArgument(#name, defaultValue)
    5754#define SetCommandLineSwitch(name) \
    5855    orxonox::CommandLineArgument& CmdArgumentDummyBoolVar##name \
    59     = orxonox::CommandLineParser::addArgument(#name, false, false)
    60 #define SetCommandLineOnlySwitch(name) \
    61     orxonox::CommandLineArgument& CmdArgumentDummyBoolVar##name \
    62     = orxonox::CommandLineParser::addArgument(#name, false, true)
    63 
     56    = orxonox::CommandLineParser::addArgument(#name, false)
    6457
    6558namespace orxonox
     
    112105    private:
    113106        //! Constructor initialises both value_ and defaultValue_ with defaultValue.
    114         CommandLineArgument(const std::string& name, const MultiType& defaultValue, bool bCommandLineOnly)
     107        CommandLineArgument(const std::string& name, const MultiType& defaultValue)
    115108            : bHasDefaultValue_(true)
    116109            , name_(name)
    117110            , value_(defaultValue)
    118111            , defaultValue_(defaultValue)
    119             , bCommandLineOnly_(bCommandLineOnly)
    120112        { }
    121113
     
    125117
    126118        //! Parses the value string of a command line argument.
    127         void parse(const std::string& value, bool bParsingFile);
     119        void parse(const std::string& value);
    128120
    129121        //! Tells whether the value has been changed by the command line.
     
    137129        MultiType   value_;            //!< The actual value
    138130        MultiType   defaultValue_;     //!< Default value. Should not be changed.
    139         bool        bCommandLineOnly_; //!< Whether you cannot specify the value in a text file
    140131    };
    141132
     
    155146
    156147        //! Parse redirection to internal member method.
    157         static void parseCommandLine(const std::string& cmdLine) { _getInstance()._parseCommandLine(cmdLine); }
    158         static void parseFile() { _getInstance()._parseFile(); }
     148        static void parse(const std::string& cmdLine)
     149        { _getInstance()._parse(cmdLine); }
    159150
    160151        static std::string getUsageInformation();
     
    168159        { return getArgument(name)->getValue(); }
    169160        template <class T>
    170         static CommandLineArgument& addArgument(const std::string& name, T defaultValue, bool bCommandLineOnly);
     161        static CommandLineArgument& addArgument(const std::string& name, T defaultValue);
    171162
    172163        static bool existsArgument(const std::string& name)
     
    189180        static CommandLineParser& _getInstance();
    190181
    191         void _parseCommandLine(const std::string& cmdLine);
    192         void _parseFile();
    193         void _parse(const std::vector<std::string>& arguments, bool bParsingFile);
    194         void checkFullArgument(const std::string& name, const std::string& value, bool bParsingFile);
    195         void checkShortcut(const std::string& shortcut, const std::string& value, bool bParsingFile);
     182        void _parse(const std::string& cmdLine);
     183        void checkFullArgument(const std::string& name, const std::string& value);
     184        void checkShortcut(const std::string& shortcut, const std::string& value);
    196185
    197186        /**
     
    222211    @param defaultValue
    223212        Default value that is used when argument was not given.
    224     @param bCommandLineOnly
    225         Parsing a file or the command line itself
    226213    */
    227214    template <class T>
    228     CommandLineArgument& CommandLineParser::addArgument(const std::string& name, T defaultValue, bool bCommandLineOnly)
     215    CommandLineArgument& CommandLineParser::addArgument(const std::string& name, T defaultValue)
    229216    {
    230217        OrxAssert(!_getInstance().existsArgument(name),
     
    234221            << "Please use SetCommandLineSwitch and adjust your argument: " << name);
    235222
    236         return *(_getInstance().cmdLineArgs_[name] = new CommandLineArgument(name, defaultValue, bCommandLineOnly));
     223        return *(_getInstance().cmdLineArgs_[name] = new CommandLineArgument(name, defaultValue));
    237224    }
    238225}
  • code/branches/unity_build/src/libraries/core/Core.cc

    r8519 r8520  
    131131
    132132        // Parse command line arguments AFTER the modules have been loaded (static code!)
    133         CommandLineParser::parseCommandLine(cmdLine);
     133        CommandLineParser::parse(cmdLine);
    134134
    135135        // Set configurable paths like log, config and media
     
    143143        // Set the correct log path. Before this call, /tmp (Unix) or %TEMP% (Windows) was used
    144144        OutputHandler::getInstance().setLogPath(PathConfig::getLogPathString());
    145 
    146         // Parse additional options file now that we know its path
    147         CommandLineParser::parseFile();
    148145
    149146#ifdef ORXONOX_PLATFORM_WINDOWS
  • code/branches/unity_build/src/libraries/core/PathConfig.cc

    r8366 r8520  
    7474
    7575    SetCommandLineArgument(externalDataPath, "").information("Path to the external data files");
    76     SetCommandLineOnlyArgument(writingPathSuffix, "").information("Additional subfolder for config and log files");
     76    SetCommandLineArgument(writingPathSuffix, "").information("Additional subfolder for config and log files");
    7777
    7878    PathConfig::PathConfig()
Note: See TracChangeset for help on using the changeset viewer.