Steppable 0.0.1
A CAS project written from scratch in C++
Loading...
Searching...
No Matches
steppable::__internals::utils::ProgramArgs Class Reference

This class is used by the program to parse command-line arguments. The class uses regular expressions to match and parse the command-line arguments. It supports switches, positional arguments, and keyword arguments. More...

#include <argParse.hpp>

Public Member Functions

 ProgramArgs (int _argc, const char **argv)
 The constructor of the ProgramArgs class, which copies the arguments passed to the program to the class.
 
void parseArgs ()
 This function is used to parse the command-line arguments and store them in the appropriate class members.
 
void addSwitch (const std::string &name, bool defaultValue, const std::string &description="")
 This function is used to add a switch to the class.
 
void addPosArg (char name, const std::string &description="", bool requiresNumber=true)
 This function is used to add a positional argument to the class.
 
void addKeywordArg (const std::string &name, int defaultValue, const std::string &description="")
 This function is used to add a keyword argument to the class.
 
std::string getPosArg (size_t index) const
 This function is used to get the value of a positional argument.
 
int getKeywordArgument (const std::string &name)
 This function is used to get the value of a keyword argument.
 
bool getSwitch (const std::string &name)
 This function is used to get the value of a switch.
 
void printUsage (const std::string &reason="") const
 This function is used to print the possible command-line arguments. Usually called when the user specifies the wrong arguments.
 

Private Attributes

std::unordered_map< std::string, bool > switches
 This map is used to store the information of all switches specified. Keys are switch names, and values are whether the switch is enabled.
 
std::map< std::string, std::string > switchDescriptions
 This map is used to store the descriptions of all switches specified.
 
std::vector< std::string > posArgs
 This vector is used to store the values of all positional arguments specified.
 
std::map< char, std::string > posArgDescriptions
 This map is used to store the descriptions of all positional arguments specified.
 
std::vector< bool > posArgIsNumber
 This map stores whether the positional arguments are required to be numbers.
 
std::unordered_map< std::string, int > keywordArgs
 This map is used to store the values of all keyword arguments specified. Keys are keyword argument names and values are the values of the keyword arguments.
 
std::map< std::string, std::string > keywordArgDescriptions
 This map is used to store the descriptions of all keyword arguments specified.
 
int argc
 This stores the number of arguments passed to the program.
 
std::vector< std::string > argv
 This stores the arguments passed to the program.
 
std::string programName
 This stores the name of the program.
 

Detailed Description

This class is used by the program to parse command-line arguments. The class uses regular expressions to match and parse the command-line arguments. It supports switches, positional arguments, and keyword arguments.

Note
A note on the names used in this class:
- Switches are arguments that start with a '+' or '-' sign. They are optional and can be in any order.
'+' switches are used to enable a feature, while '-' switches are used to disable a feature.
- Positional arguments are arguments that are not switches or keyword arguments. The names does not matter, and they are
required and ordered.
- Keyword arguments are arguments that start with a '-' sign and are followed by a name and a value. The format
is "-name:value". They are optional and can be in any order.

Constructor & Destructor Documentation

◆ ProgramArgs()

steppable::__internals::utils::ProgramArgs::ProgramArgs ( int _argc,
const char ** argv )

The constructor of the ProgramArgs class, which copies the arguments passed to the program to the class.

Parameters
[in]_argcThe number of arguments passed to the program.
[in]argvThe arguments passed to the program.

Member Function Documentation

◆ addKeywordArg()

void steppable::__internals::utils::ProgramArgs::addKeywordArg ( const std::string & name,
int defaultValue,
const std::string & description = "" )

This function is used to add a keyword argument to the class.

Parameters
[in]nameThe name of the keyword argument.
[in]defaultValueThe default value of the keyword argument. The value is stored as an integer.
[in]descriptionThe description of the keyword argument.

◆ addPosArg()

void steppable::__internals::utils::ProgramArgs::addPosArg ( char name,
const std::string & description = "",
bool requiresNumber = true )

This function is used to add a positional argument to the class.

Parameters
[in]nameThe name of the positional argument. Only one character is allowed.
[in]descriptionThe description of the positional argument.
[in]requiresNumberWhether the positional argument requires to be a number.
Note
The name of the positional argument is used for error messages only. It does not affect the parsing of
the command-line arguments.

◆ addSwitch()

void steppable::__internals::utils::ProgramArgs::addSwitch ( const std::string & name,
bool defaultValue,
const std::string & description = "" )

This function is used to add a switch to the class.

Parameters
[in]nameThe name of the switch.
[in]defaultValueThe default value of the switch. True = enabled, False = disabled.
[in]descriptionThe description of the switch.

◆ getKeywordArgument()

int steppable::__internals::utils::ProgramArgs::getKeywordArgument ( const std::string & name)

This function is used to get the value of a keyword argument.

Parameters
[in]nameThe name of the keyword argument.
Returns
The value of the keyword argument.
Note
If the keyword argument is not specified, the function will print an error message and exit the program.
Here is the call graph for this function:

◆ getPosArg()

std::string steppable::__internals::utils::ProgramArgs::getPosArg ( size_t index) const
nodiscard

This function is used to get the value of a positional argument.

Parameters
[in]indexThe index of the positional argument.
Returns
The value of the positional argument.
Note
If the positional argument is not specified, the function will print an error message and exit the program.
Here is the call graph for this function:

◆ getSwitch()

bool steppable::__internals::utils::ProgramArgs::getSwitch ( const std::string & name)

This function is used to get the value of a switch.

Parameters
[in]nameThe name of the switch.
Returns
The value of the switch.
Note
If the switch is not specified, the function will print an error message and exit the program.
Here is the call graph for this function:

◆ parseArgs()

void steppable::__internals::utils::ProgramArgs::parseArgs ( )

This function is used to parse the command-line arguments and store them in the appropriate class members.

Note
You may want to call this function after adding all the arguments to the class. Otherwise, the class will not recognize the arguments specified after the call.
Here is the call graph for this function:

◆ printUsage()

void steppable::__internals::utils::ProgramArgs::printUsage ( const std::string & reason = "") const

This function is used to print the possible command-line arguments. Usually called when the user specifies the wrong arguments.

Parameters
[in]reasonThe reason for printing the usage. If specified, the reason will be printed after the usage.
Note
The function will print the usage of the program and exit the program.
Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ argc

int steppable::__internals::utils::ProgramArgs::argc
private

This stores the number of arguments passed to the program.

◆ argv

std::vector<std::string> steppable::__internals::utils::ProgramArgs::argv
private

This stores the arguments passed to the program.

◆ keywordArgDescriptions

std::map<std::string, std::string> steppable::__internals::utils::ProgramArgs::keywordArgDescriptions
private

This map is used to store the descriptions of all keyword arguments specified.

◆ keywordArgs

std::unordered_map<std::string, int> steppable::__internals::utils::ProgramArgs::keywordArgs
private

This map is used to store the values of all keyword arguments specified. Keys are keyword argument names and values are the values of the keyword arguments.

◆ posArgDescriptions

std::map<char, std::string> steppable::__internals::utils::ProgramArgs::posArgDescriptions
private

This map is used to store the descriptions of all positional arguments specified.

◆ posArgIsNumber

std::vector<bool> steppable::__internals::utils::ProgramArgs::posArgIsNumber
private

This map stores whether the positional arguments are required to be numbers.

◆ posArgs

std::vector<std::string> steppable::__internals::utils::ProgramArgs::posArgs
private

This vector is used to store the values of all positional arguments specified.

◆ programName

std::string steppable::__internals::utils::ProgramArgs::programName
private

This stores the name of the program.

◆ switchDescriptions

std::map<std::string, std::string> steppable::__internals::utils::ProgramArgs::switchDescriptions
private

This map is used to store the descriptions of all switches specified.

◆ switches

std::unordered_map<std::string, bool> steppable::__internals::utils::ProgramArgs::switches
private

This map is used to store the information of all switches specified. Keys are switch names, and values are whether the switch is enabled.


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