Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Oct 24, 2008, 12:43:13 AM (16 years ago)
Author:
rgrieder
Message:

Simplified CommandLineArgument by using the new MultiType.
That also enables to get a cmd argument even if you don't know the exact type. It simply gets converted.

File:
1 edited

Legend:

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

    r1755 r2003  
    3535    /**
    3636    @brief
     37        Parses a value string for a command line argument.
     38        It simply uses convertValue(Output, Input) to do that.
     39        Bools are treated specially. That is necessary
     40        so that you can have simple command line switches.
     41    */
     42    void CommandLineArgument::parse(const std::string& value)
     43    {
     44        if (value_.getType() == MT_bool)
     45        {
     46            // simulate command line switch
     47            bool temp;
     48            if (convertValue(&temp, value))
     49            {
     50                this->bHasDefaultValue_ = false;
     51                this->value_ = temp;
     52            }
     53            else if (value == "")
     54            {
     55                this->bHasDefaultValue_ = false;
     56                this->value_ = true;
     57            }
     58            else
     59            {
     60                ThrowException(Argument, "Could not read command line argument '" + getName() + "'.");
     61            }
     62        }
     63        else
     64        {
     65            if (!value_.setValue(value))
     66            {
     67                value_.setValue(defaultValue_);
     68                ThrowException(Argument, "Could not read command line argument '" + getName() + "'.");
     69            }
     70            else
     71                this->bHasDefaultValue_ = false;
     72        }
     73    }
     74
     75
     76    /**
     77    @brief
    3778        Destructor destroys all CommandLineArguments with it.
    3879    */
    3980    CommandLine::~CommandLine()
    4081    {
    41         for (std::map<std::string, BaseCommandLineArgument*>::const_iterator it = cmdLineArgs_.begin();
     82        for (std::map<std::string, CommandLineArgument*>::const_iterator it = cmdLineArgs_.begin();
    4283            it != cmdLineArgs_.end(); ++it)
    4384        {
     
    73114        {
    74115            // first shove all the shortcuts in a map
    75             for (std::map<std::string, BaseCommandLineArgument*>::const_iterator it = cmdLineArgs_.begin();
     116            for (std::map<std::string, CommandLineArgument*>::const_iterator it = cmdLineArgs_.begin();
    76117                it != cmdLineArgs_.end(); ++it)
    77118            {
     
    178219    void CommandLine::checkFullArgument(const std::string& name, const std::string& value)
    179220    {
    180         std::map<std::string, BaseCommandLineArgument*>::const_iterator it = cmdLineArgs_.find(name);
     221        std::map<std::string, CommandLineArgument*>::const_iterator it = cmdLineArgs_.find(name);
    181222        if (it == cmdLineArgs_.end())
    182223            ThrowException(Argument, "Command line argument '" + name + "' does not exist.");
     
    195236    void CommandLine::checkShortcut(const std::string& shortcut, const std::string& value)
    196237    {
    197         std::map<std::string, BaseCommandLineArgument*>::const_iterator it = cmdLineArgsShortcut_.find(shortcut);
     238        std::map<std::string, CommandLineArgument*>::const_iterator it = cmdLineArgsShortcut_.find(shortcut);
    198239        if (it == cmdLineArgsShortcut_.end())
    199240            ThrowException(Argument, "Command line shortcut '" + shortcut + "' does not exist.");
     
    206247        CommandLine* inst = &_getInstance();
    207248        std::string infoStr;
    208         for (std::map<std::string, BaseCommandLineArgument*>::const_iterator it = inst->cmdLineArgs_.begin();
     249        for (std::map<std::string, CommandLineArgument*>::const_iterator it = inst->cmdLineArgs_.begin();
    209250            it != inst->cmdLineArgs_.end(); ++it)
    210251        {
     
    214255    }
    215256
     257    /**
     258    @brief
     259        Retrieves a CommandLineArgument.
     260        The method throws an exception if 'name' was not found or the value could not be converted.
     261    @note
     262        You shold of course not call this method before the command line has been parsed.
     263    */
     264    const CommandLineArgument* CommandLine::getArgument(const std::string& name)
     265    {
     266        std::map<std::string, CommandLineArgument*>::const_iterator it = _getInstance().cmdLineArgs_.find(name);
     267        if (it == _getInstance().cmdLineArgs_.end())
     268        {
     269            ThrowException(Argument, "Could find command line argument '" + name + "'.");
     270        }
     271        else
     272        {
     273            return it->second;
     274        }
     275    }
    216276}
Note: See TracChangeset for help on using the changeset viewer.