Chaste  Release::2018.1
CommandLineArguments Class Reference

#include <CommandLineArguments.hpp>

+ Inheritance diagram for CommandLineArguments:
+ Collaboration diagram for CommandLineArguments:

Public Member Functions

bool OptionExists (const std::string &rOption)
 
int GetNumberOfArgumentsForOption (const std::string &rOption, bool throwIfNone=false)
 
char * GetValueCorrespondingToOption (const std::string &rOption, int valueNumber=1)
 
double GetDoubleCorrespondingToOption (const std::string &rOption, int valueNumber=1)
 
int GetIntCorrespondingToOption (const std::string &rOption, int valueNumber=1)
 
unsigned GetUnsignedCorrespondingToOption (const std::string &rOption, int valueNumber=1)
 
std::string GetStringCorrespondingToOption (const std::string &rOption, int valueNumber=1)
 
std::vector< std::string > GetStringsCorrespondingToOption (const std::string &rOption)
 
std::vector< doubleGetDoublesCorrespondingToOption (const std::string &rOption)
 
std::vector< int > GetIntsCorrespondingToOption (const std::string &rOption)
 
std::vector< unsignedGetUnsignedsCorrespondingToOption (const std::string &rOption)
 
bool GetBoolCorrespondingToOption (const std::string &rOption)
 

Static Public Member Functions

static CommandLineArgumentsInstance ()
 

Public Attributes

int * p_argc
 
char *** p_argv
 

Private Member Functions

 CommandLineArguments ()
 
int GetIndexForArgument (std::string rOption)
 
void TestOptionFormat (const std::string &rOption)
 

Static Private Attributes

static CommandLineArgumentsmpInstance = nullptr
 

Detailed Description

A convenient holder for the command line arguments, with helper methods for checking whether an option has been given or getting the value(s) corresponding to a given option.

Options are considered to be any argument starting with a '-' followed by a non-numeric character, in order to distinguish between options and negative numbers. Each option may have zero or more values following it on the command line. Examples:

  • -option1 on
  • –my_param value
  • –verbose
  • -numbers 1 8 -4.5
  • –models model1 model2

The cxxtest harness will fill in the member variables when a test is started. They can then be read by PETSc when it is initialised, and by other code as required.

Definition at line 62 of file CommandLineArguments.hpp.

Constructor & Destructor Documentation

CommandLineArguments::CommandLineArguments ( )
private

Default constructor. Should never be called directly, call CommandLineArguments::Instance() instead.

Definition at line 42 of file CommandLineArguments.cpp.

References mpInstance.

Referenced by Instance().

Member Function Documentation

bool CommandLineArguments::GetBoolCorrespondingToOption ( const std::string &  rOption)

Read bools from the command line. This accepts 0 or 1, and also true/True/TRUE and false/False/FALSE.

Parameters
rOptionThe option name as a string.
Returns
Whether the option was set to true or false.

Definition at line 209 of file CommandLineArguments.cpp.

References EXCEPTION, and GetStringCorrespondingToOption().

double CommandLineArguments::GetDoubleCorrespondingToOption ( const std::string &  rOption,
int  valueNumber = 1 
)
Returns
the double for a given option.

This uses GetValueCorrespondingToOption and converts the char* to a double.

Parameters
rOptionthe option name as a string.
valueNumberthe number of the argument following the option definition (defaults to 1, for 1st argument).

Definition at line 91 of file CommandLineArguments.cpp.

References GetValueCorrespondingToOption().

Referenced by GetDoublesCorrespondingToOption().

std::vector< double > CommandLineArguments::GetDoublesCorrespondingToOption ( const std::string &  rOption)
Returns
a collection of doubles for a given option.

This uses GetDoubleCorrespondingToOption repeatedly.

Parameters
rOptionthe option name as a string.

Definition at line 176 of file CommandLineArguments.cpp.

References GetDoubleCorrespondingToOption(), and GetNumberOfArgumentsForOption().

int CommandLineArguments::GetIndexForArgument ( std::string  rOption)
private
Returns
the index for the given argument. Returns -1 if the argument is not found.
Parameters
rOptionthe argument name as a string.
Returns
the position of the argument in the list (indexed from 1)

Definition at line 114 of file CommandLineArguments.cpp.

References EXCEPTION, p_argc, p_argv, and TestOptionFormat().

Referenced by GetNumberOfArgumentsForOption(), GetValueCorrespondingToOption(), and OptionExists().

int CommandLineArguments::GetIntCorrespondingToOption ( const std::string &  rOption,
int  valueNumber = 1 
)
Returns
the int for a given option.

This uses GetValueCorrespondingToOption and converts the char* to an int.

Parameters
rOptionthe option name as a string.
valueNumberthe number of the argument following the option definition (defaults to 1, for 1st argument).

Definition at line 97 of file CommandLineArguments.cpp.

References GetValueCorrespondingToOption().

Referenced by GetIntsCorrespondingToOption().

std::vector< int > CommandLineArguments::GetIntsCorrespondingToOption ( const std::string &  rOption)
Returns
a collection of ints for a given option.

This uses GetIntCorrespondingToOption repeatedly.

Parameters
rOptionthe option name as a string.

Definition at line 198 of file CommandLineArguments.cpp.

References GetIntCorrespondingToOption(), and GetNumberOfArgumentsForOption().

int CommandLineArguments::GetNumberOfArgumentsForOption ( const std::string &  rOption,
bool  throwIfNone = false 
)
Returns
the number of arguments for a given option. Throws an Exception if the option does not have any following arguments, or the option does not exist. So you can use OptionExists() to avoid Exceptions in your code when using the other public methods.
Parameters
rOptionthe option name as a string.
throwIfNoneWhether to give an exception if there are no arguments for this rOption (defaults to false).
Returns
the number of arguments following this option.

Definition at line 128 of file CommandLineArguments.cpp.

References EXCEPTION, GetIndexForArgument(), p_argc, and p_argv.

Referenced by GetDoublesCorrespondingToOption(), GetIntsCorrespondingToOption(), GetStringsCorrespondingToOption(), GetUnsignedsCorrespondingToOption(), GetValueCorrespondingToOption(), and Citations::Print().

std::string CommandLineArguments::GetStringCorrespondingToOption ( const std::string &  rOption,
int  valueNumber = 1 
)
Returns
the string for a given option.

This uses GetValueCorrespondingToOption and converts the char* to a std::string.

Parameters
rOptionthe option name as a string.
valueNumberthe number of the argument following the option definition (defaults to 1, for 1st argument).

Definition at line 158 of file CommandLineArguments.cpp.

References GetValueCorrespondingToOption().

Referenced by GetBoolCorrespondingToOption(), GetStringsCorrespondingToOption(), and Citations::Print().

std::vector< std::string > CommandLineArguments::GetStringsCorrespondingToOption ( const std::string &  rOption)
Returns
a collection of strings for a given option (useful for inputting a list of files for example).

This uses GetStringCorrespondingToOption repeatedly.

Parameters
rOptionthe option name as a string.

Definition at line 165 of file CommandLineArguments.cpp.

References GetNumberOfArgumentsForOption(), and GetStringCorrespondingToOption().

unsigned CommandLineArguments::GetUnsignedCorrespondingToOption ( const std::string &  rOption,
int  valueNumber = 1 
)
Returns
the unsigned for a given option.

This uses GetValueCorrespondingToOption and converts the char* to an unsigned. Throws an exception if the option converts to a negative integer.

Parameters
rOptionthe option name as a string.
valueNumberthe number of the argument following the option definition (defaults to 1, for 1st argument).

Definition at line 103 of file CommandLineArguments.cpp.

References EXCEPTION, and GetValueCorrespondingToOption().

Referenced by GetUnsignedsCorrespondingToOption().

std::vector< unsigned > CommandLineArguments::GetUnsignedsCorrespondingToOption ( const std::string &  rOption)
Returns
a collection of unsigneds for a given option.

This uses GetUnsignedCorrespondingToOption repeatedly.

Parameters
rOptionthe option name as a string.

Definition at line 187 of file CommandLineArguments.cpp.

References GetNumberOfArgumentsForOption(), and GetUnsignedCorrespondingToOption().

char * CommandLineArguments::GetValueCorrespondingToOption ( const std::string &  rOption,
int  valueNumber = 1 
)
Returns
the value for a given option, i.e. the argument after the option name in the list of command line arguments. For example, if the following arguments were given ./heart/build/debug/TestMyClassRunner -timestep 0.04 Then calling CommandLineArguments::Instance()->GetValueCorrespondingToOption("-timestep"); will return 0.04 (as a char*). Use atoi or atof to convert the char* to an int or a double(float) respectively, or call one of our convenience methods for common types.
Parameters
rOptionthe option name as a string.
valueNumberthe number of the argument following the option definition (defaults to 1, for 1st argument).

Definition at line 74 of file CommandLineArguments.cpp.

References EXCEPT_IF_NOT, EXCEPTION, GetIndexForArgument(), GetNumberOfArgumentsForOption(), p_argv, and TestOptionFormat().

Referenced by GetDoubleCorrespondingToOption(), GetIntCorrespondingToOption(), GetStringCorrespondingToOption(), and GetUnsignedCorrespondingToOption().

void CommandLineArguments::TestOptionFormat ( const std::string &  rOption)
private

Throw an exception if the option is not of the required form: '-' followed by a non-numeric character.

Parameters
rOptionthe option name to check the format of.

Definition at line 234 of file CommandLineArguments.cpp.

References EXCEPTION.

Referenced by GetIndexForArgument(), GetValueCorrespondingToOption(), and OptionExists().

Member Data Documentation

CommandLineArguments * CommandLineArguments::mpInstance = nullptr
staticprivate

The single instance of the class.

Definition at line 70 of file CommandLineArguments.hpp.

Referenced by CommandLineArguments(), and Instance().


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