PetaVision  Alpha
PV::ConfigParser Class Reference

#include <ConfigParser.hpp>

Public Member Functions

 ConfigParser (std::istream &configStream, bool allowUnrecognizedArguments)
 
bool getAllowUnrecognizedArguments () const
 
bool const & getBooleanArgument (std::string const &name) const
 
Configuration const & getConfig () const
 
int const & getIntegerArgument (std::string const &name) const
 
Configuration::IntOptional getIntOptionalArgument (std::string const &name) const
 
std::string getStringArgument (std::string const &name) const
 
unsigned int const & getUnsignedIntArgument (std::string const &name) const
 
std::string printConfig () const
 

Static Public Member Functions

static std::string createString (bool requireReturnFlag, std::string const &outputPath, std::string const &paramsFile, std::string const &logFile, std::string const &gpuDevices, unsigned int randomSeed, std::string const &workingDir, bool restartFlag, std::string const &checkpointReadDir, bool useDefaultNumThreads, int numThreads, int numRows, int numColumns, int batchWidth, bool dryRunFlag)
 

Private Member Functions

void handleUnrecognized (std::string const &argument, std::string const &value, int linenumber)
 
void initialize (std::istream &inputStream, bool allowUnrecognizedArguments)
 
std::string stripLeadingTrailingWhitespace (std::string const &inString)
 

Private Attributes

bool mAllowUnrecognizedArguments = false
 
Configuration mConfig
 

Detailed Description

A class to parse an input stream for configuration options. The main data member is a Configuration object. ConfigParser has get-methods which are passed to the Configuration object, but it does not have set-methods because the object, once instantiated should always reflect the configuration of the input stream. The intended use case is to allow the Arguments class to modify some configuration settings but be able, at any time, to reset to the state from the original stream (using the Arguments::resetState(void) method).

Definition at line 21 of file ConfigParser.hpp.

Constructor & Destructor Documentation

PV::ConfigParser::ConfigParser ( std::istream &  configStream,
bool  allowUnrecognizedArguments 
)

The public constructor for ConfigParser. configStream is an input stream with the contents of the configuration. See the initialize method on how the stream contents are parsed. If allowUnrecognizedArguments is true, any unrecognized configuration arguments are skipped. If it is false, unrecognized arguments cause an error.

Definition at line 19 of file ConfigParser.cpp.

Member Function Documentation

bool PV::ConfigParser::getAllowUnrecognizedArguments ( ) const
inline

Returns the value of allowUnrecognizedArguments used in instantiation.

Definition at line 36 of file ConfigParser.hpp.

bool const& PV::ConfigParser::getBooleanArgument ( std::string const &  name) const
inline

If the configuration recognizes the string in 'name' as a boolean argument, this method returns the value of that argument. If the name is not a boolean argument, throws an invalid_argument exception.

Definition at line 44 of file ConfigParser.hpp.

Configuration const& PV::ConfigParser::getConfig ( ) const
inline

Returns a constant reference to the underlying Configuration object.

Definition at line 115 of file ConfigParser.hpp.

int const& PV::ConfigParser::getIntegerArgument ( std::string const &  name) const
inline

If the configuration recognizes the string in 'name' as an integer argument, this method returns the value of that argument. If the name is not an integer argument, throws an invalid_argument exception.

Definition at line 54 of file ConfigParser.hpp.

Configuration::IntOptional PV::ConfigParser::getIntOptionalArgument ( std::string const &  name) const
inline

If the configuration recognizes the string in 'name' as an integer argument with a default option, this method returns the value of that argument. If the name is not an integer-with-default argument, throws an invalid_argument exception.

Definition at line 84 of file ConfigParser.hpp.

std::string PV::ConfigParser::getStringArgument ( std::string const &  name) const
inline

If the configuration recognizes the string in 'name' as a string argument, this method returns the value of that argument. If the name is not a string argument, throws an invalid_argument exception.

Definition at line 74 of file ConfigParser.hpp.

unsigned int const& PV::ConfigParser::getUnsignedIntArgument ( std::string const &  name) const
inline

If the configuration recognizes the string in 'name' as an unsigned integer argument, this method returns the value of that argument. If the name is not an unsigned integer argument, throws an invalid_argument exception.

Definition at line 64 of file ConfigParser.hpp.

void PV::ConfigParser::handleUnrecognized ( std::string const &  argument,
std::string const &  value,
int  linenumber 
)
private

If allowUnrecognizedArguments is true, this routine does nothing. If it is false, it exits with an error message containing the arguments.

Definition at line 70 of file ConfigParser.cpp.

void PV::ConfigParser::initialize ( std::istream &  inputStream,
bool  allowUnrecognizedArguments 
)
private

Called by the ConfigParser constructor. Each line is read until the end of the inputStream, and processed as follows: Blank lines and lines consisting solely of whitespace are skipped. Lines whose first nonwhitespace character is '#' (comments) are skipped. The remaining lines are scanned for a colon ':'. It is an error for a nonblank, noncomment line to have no colons. The part of the line before the first colon, with leading and trailing whitespace deleted, is the argument name. The part after the first colon, with leading and trailing whitespace deleted, is the argument value. Recognized parameters and the method used for each are: RequireReturn (parseBoolean) OutputPath (parseString) ParamsFile (parseString) LogFile (parseString) GPUDevices (parseString) RandomSeed (parseUnsignedInt) WorkingDirectory (parseString) Restart (parseBoolean) CheckpointReadDirectory (parseString) NumThreads (parseIntOptional) NumRows (parseInteger) NumColumns (parseInteger) BatchWidth (parseInteger) DryRun (parseBoolean) Any other argument names are ignored if the allowUnrecognizedArguments flag is true, and cause an error if the flag is false.

initialize() expects that if the parsing method it calls encounter a bad argument value, it will throw an exception. initialize() catches any such exception and exits with an error.

Definition at line 26 of file ConfigParser.cpp.

std::string PV::ConfigParser::printConfig ( ) const
inline

Returns a string consisting of all the configuration settings. If this string is converted to an input stream, it can be used to instantiate another ConfigParser object.

Definition at line 93 of file ConfigParser.hpp.

std::string PV::ConfigParser::stripLeadingTrailingWhitespace ( std::string const &  inString)
private

This method returns a string whose contents are the input string, with any whitespace at the beginning or end of the string removed. Whitespace in the middle of the string remains untouched.

Definition at line 80 of file ConfigParser.cpp.


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