OptionsIO Class Reference

#include <OptionsIO.h>


Detailed Description

Helping methods for parsing of command line arguments and reading a configuration file. Any errors are reported by throwing a ProcessError exception which contains a description about the failure.

Definition at line 52 of file OptionsIO.h.


Static Public Member Functions

static void getOptions (bool loadConfig, int argv, char **argc) throw (ProcessError)
 Parses the command line arguments and loads the configuration optionally.
static void loadConfiguration () throw (ProcessError)
 Loads and parses the configuration.

Member Function Documentation

void OptionsIO::getOptions ( bool  loadConfig,
int  argv,
char **  argc 
) throw (ProcessError) [static]

Parses the command line arguments and loads the configuration optionally.

Command line arguments are parsed, first, throwing a ProcessError if something fails. If loadConfig is false, the method returns after this. Otherwise, options are reset to being writeable and the configuration is loaded using "loadConfiguration". After this, the options are reset again and the command line arguments are reparsed.

This workflow allows to read the name of a configuration file from command line arguments, first, then to load values from this configuration file and reset them by other values from the command line.

Parameters:
[in] loadConfig Whether the configuration shall be loaded
[in] argv number of arguments given at the command line
[in] argc arguments given at the command line

Definition at line 56 of file OptionsIO.cpp.

References OptionsCont::getOptions(), OptionsParser::parse(), and OptionsCont::resetWritable().

Referenced by GUILoadThread::initOptions(), and main().

00056                                                                                 {
00057     // preparse the options
00058     //  (maybe another configuration file was chosen)
00059     if (!OptionsParser::parse(argc, argv)) {
00060         throw ProcessError("Could not parse commandline options.");
00061     }
00062     // check whether to use the command line parameters only
00063     if (!loadConfig) {
00064         return;
00065     }
00066     // read the configuration when everything's ok
00067     OptionsCont::getOptions().resetWritable();
00068     loadConfiguration();
00069     // reparse the options
00070     //  (overwrite the settings from the configuration file)
00071     OptionsCont::getOptions().resetWritable();
00072     OptionsParser::parse(argc, argv);
00073 }

void OptionsIO::loadConfiguration (  )  throw (ProcessError) [static]

Loads and parses the configuration.

The name of the configuration file is extracted from the global OptionsCont ("configuration-file" is used as the name of the option to get the name of the configuration).

Definition at line 77 of file OptionsIO.cpp.

References MsgHandler::beginProcessMsg(), MsgHandler::endProcessMsg(), OptionsLoader::errorOccured(), FileHelpers::exists(), OptionsCont::exists(), MsgHandler::getMessageInstance(), OptionsCont::getOptions(), OptionsCont::getString(), OptionsCont::isSet(), and OptionsCont::relocateFiles().

00077                                                  {
00078     OptionsCont &oc = OptionsCont::getOptions();
00079     if (!oc.exists("configuration-file") || !oc.isSet("configuration-file")) {
00080         return;
00081     }
00082     std::string path = oc.getString("configuration-file");
00083     if (!FileHelpers::exists(path)) {
00084         throw ProcessError("Could not find configuration '" + oc.getString("configuration-file") + "'.");
00085     }
00086     MsgHandler::getMessageInstance()->beginProcessMsg("Loading configuration...");
00087     // build parser
00088     SAXParser parser;
00089     parser.setValidationScheme(SAXParser::Val_Auto);
00090     parser.setDoNamespaces(false);
00091     parser.setDoSchema(false);
00092     // start the parsing
00093     OptionsLoader handler;
00094     try {
00095         parser.setDocumentHandler(&handler);
00096         parser.setErrorHandler(&handler);
00097         parser.parse(path.c_str());
00098         if (handler.errorOccured()) {
00099             throw ProcessError("Could not load configuration '" + path + "'.");
00100         }
00101     } catch (const XMLException &e) {
00102         throw ProcessError("Could not load configuration '" + path + "':\n " + TplConvert<XMLCh>::_2str(e.getMessage()));
00103     }
00104     oc.relocateFiles(path);
00105     MsgHandler::getMessageInstance()->endProcessMsg("done.");
00106 }


The documentation for this class was generated from the following files:

Generated on Wed May 5 00:06:57 2010 for Sumo - Simulation of Urban MObility by  doxygen 1.5.6