SatAndLight  2.2.2-hubble
Simulation toolkit for space telescopes
SnlProg Class Reference

Parse user options from the command line. More...

#include <Options.h>

Collaboration diagram for SnlProg:

Public Member Functions

double GetValueD (const E_OPTIONS_ID aId)
 Returns option value (double). More...
 
int GetValueI (const E_OPTIONS_ID aId)
 Returns option value (integer). More...
 
string GetValueS (const E_OPTIONS_ID aId)
 Returns option value (string). More...
 
unsigned int GetValueUI (const E_OPTIONS_ID aId)
 Returns option value (unsigned 32b integer). More...
 
unsigned long int GetValueULI (const E_OPTIONS_ID aId)
 Returns option value (unsigned 64b integer). More...
 
vector< double > GetValueVector (const E_OPTIONS_ID aId)
 Returns the option value as a vector of double values. More...
 
vector< string > GetValueVectorS (const E_OPTIONS_ID aId)
 Returns the option value as a vector of sub-strings. More...
 
void PrintUsage (void)
 Prints program usage. More...
 
int Scan (int argc, char *argv[])
 Parse the command line arguments. More...
 
void SelectOption (const E_OPTIONS_ID aId, const string aDefaultValue, const bool aRequired=true, const string aExtraDescription="")
 Selects an option. More...
 
Constructors and destructors
 SnlProg (const string aName, const string aDescription="", const string aDocPage="")
 Constructor of the SnlProg class. More...
 
virtual ~SnlProg (void)
 Destructor of the SnlProg class. More...
 

Private Member Functions

E_ERROR_CODE CheckOpt (const E_OPTIONS_ID aId)
 Checks the sanity of user input values. More...
 
void SaveValue (const string aKeyWord, const string aValue)
 Saves option value. More...
 

Private Attributes

string desc
 Program description. More...
 
string doc
 Link to documentation page. More...
 
string name
 Program name. More...
 
SnlOptopt [OPTIONS_ID_N]
 List of options. More...
 
bool required [OPTIONS_ID_N]
 List of required options. More...
 
bool selected [OPTIONS_ID_N]
 List of selected options. More...
 
bool used [OPTIONS_ID_N]
 List of used options. More...
 

Detailed Description

Parse user options from the command line.

The SnlProg class is designed to manage user options given to a SNL program in the command line. All SNL applications can be given options with the following format:

snl-program keyword1=value1 keyword2=value2 ...

The SnlProg class is used to parse the command line arguments and extract user parameters. It is based on a pre-defined list of possible options defined by an ID (_E_OPTIONS_ID). Each option is given:

Moreover options can be given attributes:

  • the option can be required by the application or not
  • the option description can be tuned with extra text.

This class internally manages the usage message of a program (PrintUsage()). It also checks the sanity of input parameters (CheckOpt()).

The use of this class is simple. First the user must specify the list of options and attributes by calling SelectOption(). Then call Scan() to scan the list of user options.

Author
Florent Robinet

Constructor & Destructor Documentation

◆ SnlProg()

SnlProg::SnlProg ( const string  aName,
const string  aDescription = "",
const string  aDocPage = "" 
)

Constructor of the SnlProg class.

The SNL options (_E_OPTIONS_ID) are initialized. By default none of the options listed in ::_E_OPTIONS_ID are selected. All of them are set to an "optional" mode. Use SelectOption() to select and configure an option.

Parameters
[in]aNameName of the program.
[in]aDescriptionProgram description.
[in]aDocPageLink to a documentation page.

◆ ~SnlProg()

SnlProg::~SnlProg ( void  )
virtual

Destructor of the SnlProg class.

Member Function Documentation

◆ CheckOpt()

E_ERROR_CODE SnlProg::CheckOpt ( const E_OPTIONS_ID  aId)
private

Checks the sanity of user input values.

Several sanity checks are implemented depending on the option ID:

◆ GetValueD()

double SnlProg::GetValueD ( const E_OPTIONS_ID  aId)
inline

Returns option value (double).

Parameters
[in]aIdOption ID.
Warning
Make sure the type associated to aId is OPTIONS_TYPE_DOUBLE.
See also
OPTIONS_TYPE

◆ GetValueI()

int SnlProg::GetValueI ( const E_OPTIONS_ID  aId)
inline

Returns option value (integer).

Parameters
[in]aIdOption ID.
Warning
Make sure the type associated to aId is OPTIONS_TYPE_INT.
See also
OPTIONS_TYPE

◆ GetValueS()

string SnlProg::GetValueS ( const E_OPTIONS_ID  aId)
inline

Returns option value (string).

Parameters
[in]aIdOption ID.
Warning
Make sure the type associated to aId is OPTIONS_TYPE_STRING.
See also
OPTIONS_TYPE

◆ GetValueUI()

unsigned int SnlProg::GetValueUI ( const E_OPTIONS_ID  aId)
inline

Returns option value (unsigned 32b integer).

Parameters
[in]aIdOption ID.
Warning
Make sure the type associated to aId is OPTIONS_TYPE_U_INT.
See also
OPTIONS_TYPE

◆ GetValueULI()

unsigned long int SnlProg::GetValueULI ( const E_OPTIONS_ID  aId)
inline

Returns option value (unsigned 64b integer).

Parameters
[in]aIdOption ID.
Warning
Make sure the type associated to aId is OPTIONS_TYPE_UL_INT.
See also
OPTIONS_TYPE

◆ GetValueVector()

vector< double > SnlProg::GetValueVector ( const E_OPTIONS_ID  aId)

Returns the option value as a vector of double values.

Sometimes it can be convenient to use an option as a sequence of numbers. This function returns this sequence of numbers in a vector.

This only works if the option type is OPTIONS_TYPE_STRING. The character ';' is then used to seprate the numbers in the option value. For example if the option is given as:

keyword=2.3;5;3e45

This function returns 2.3, 5 and 3e45 in a vector of doubles.

Parameters
[in]aIdOption ID.
Warning
Make sure the type associated to aId is OPTIONS_TYPE_STRING.
See also
OPTIONS_TYPE

◆ GetValueVectorS()

vector<string> SnlProg::GetValueVectorS ( const E_OPTIONS_ID  aId)
inline

Returns the option value as a vector of sub-strings.

Sometimes it can be convenient to use an option as a sequence of words. This function returns this sequence of words in a vector.

This only works if the character ';' is used to seprate the words in the option value. For example if the option is given as:

keyword=val1;val2;val3

This function returns "val1", "val2" and "val3" in a vector of strings.

Parameters
[in]aIdOption ID.
Warning
Make sure the type associated to aId is OPTIONS_TYPE_STRING.
See also
OPTIONS_TYPE

◆ PrintUsage()

void SnlProg::PrintUsage ( void  )

Prints program usage.

◆ SaveValue()

void SnlProg::SaveValue ( const string  aKeyWord,
const string  aValue 
)
private

Saves option value.

The option keyword is checked against the dictionary OPTIONS_KEY.

Parameters
[in]aKeyWordOption keyword.
[in]aValueOption value.

◆ Scan()

int SnlProg::Scan ( int  argc,
char *  argv[] 
)

Parse the command line arguments.

The program command line is scanned:

  • If the command line has no arguments, the help message is printed and SNL_ERROR_HELP is returned.
  • If the first command argument is "version", the program version number is printed and SNL_ERROR_VERSION is returned.
  • The list of argments is scanned. Each argument must match exactly "keyword=value" otherwise the argument is ignored.

In a second step, the user inputs are checked:

  • Required options must be provided. If not this function returns SNL_ERROR_MISSING_OPTION.
  • Specific checks are performed depending on the option ID. If the check fails, a specific error code is returned: see CheckOpt().
Parameters
[in]argcNumber of arguments in the program.
[in]argvList of arguments in the program.
Returns
The error code.
See also
_E_ERROR_CODE

◆ SelectOption()

void SnlProg::SelectOption ( const E_OPTIONS_ID  aId,
const string  aDefaultValue,
const bool  aRequired = true,
const string  aExtraDescription = "" 
)

Selects an option.

The option, identified by its ID, is activated and given attributes.

Parameters
[in]aIdOption ID.
[in]aDefaultValueOption default value.
[in]aRequiredOption required flag: true = the option is required. false = the option is optional.
[in]aExtraDescriptionOption extra description.
Warning
The validity/sanity of the default value is not checked! The user must make sure the value makes sense. This is especially true for optional options.
See also
OPTIONS_DESC

Member Data Documentation

◆ desc

string SnlProg::desc
private

Program description.

◆ doc

string SnlProg::doc
private

Link to documentation page.

◆ name

string SnlProg::name
private

Program name.

◆ opt

SnlOpt* SnlProg::opt[OPTIONS_ID_N]
private

List of options.

◆ required

bool SnlProg::required[OPTIONS_ID_N]
private

List of required options.

◆ selected

bool SnlProg::selected[OPTIONS_ID_N]
private

List of selected options.

◆ used

bool SnlProg::used[OPTIONS_ID_N]
private

List of used options.


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