PYTHIA  8.313
Classes | Public Types | Public Member Functions | List of all members
InputParser Class Reference

#include <InputParser.h>

Public Types

enum  Status { Valid = -1, Invalid = EXIT_FAILURE, Help = EXIT_SUCCESS }
 

Public Member Functions

 InputParser (string usage="", vector< string > examples={}, string extra="", ostream *stream=&cout, string optName="h", set< string > aliases={"-help","H"})
 
bool add (const string &optName, const string &defString, const string &helpText="", set< string > aliases={})
 
bool require (const string &optName, const string &helpText="", set< string > aliases={})
 Add required command line option to the parser. Do this before parsing.
 
Status init (int &argc, char **argv)
 
bool parse (int &argc, char **argv)
 
bool has (const string &optName) const
 
template<typename T >
get (const string &optName)
 
template<typename T >
vector< T > getVector (const string &optName)
 Templated method to get all the values of an option. More...
 
const string help () const
 

Detailed Description

Convenience class for parsing C++ command-line arguments on the form ./mainXX -a A -b B etc.

Usage: (1) Create an InputParser object in your main program. (2) Specify the command-line options that should be available, including default values, using add. (3) Call the parse(int& argc, char** argv) method, with the two aruments forwarded from argc and argv from your main. (4) Extract given command-line values of type T, using get<T>.

Member Enumeration Documentation

enum Status

Initialize the parser. This includes parsing, printing help if requested, and checking the required options.

Constructor & Destructor Documentation

InputParser ( string  usage = "",
vector< string >  examples = {},
string  extra = "",
ostream *  stream = &cout,
string  optName = "h",
set< string >  aliases = {"-help","H"} 
)
inline

Constructor. usage: Text to print when describing usage with help. examples: Vector of examples to print with help. extra: Extra text to print with help. stream: Optional pointer to stream to print messages to. optName: Option name for the help flag. aliases: Aliases for the help flag.

Member Function Documentation

bool add ( const string &  optName,
const string &  defString,
const string &  helpText = "",
set< string >  aliases = {} 
)
inline

Add an optional command line option to the parser. Do this before parsing. Parameters: optName: The name of the option. defString: The default value for the option. helpText: The help text describing the option (optional). aliases: A set of aliases for the option (optional).

Check for name conflicts with existing options or aliases.

Create an OptionInfo object and add it to the options map.

Add aliases to the alias map.

T get ( const string &  optName)
inline

Templated method to get the last value of an option. Returns the last value of the option converted to the specified type.

Retrieve the OptionInfo object for the given option name.

Return default-constructed T if string is empty.

Convert the string value to the specified type.

Error message and default constructed T() is conversion failed.

vector<T> getVector ( const string &  optName)
inline

Templated method to get all the values of an option.

Retrieve the OptionInfo object for the given option name.

Return empty vector if no values.

Convert the string value to the specified type.

Error message and default constructed T() is conversion failed.

bool has ( const string &  optName) const
inline

Check if an option is defined. Returns true if the option is defined, false otherwise.

const string help ( ) const
inline

Method to generate the help text for all options. Returns a formatted string containing the help text.

Status init ( int &  argc,
char **  argv 
)
inline

Parse the arguments.

Print the help if requested.

Check the required options are set.

bool parse ( int &  argc,
char **  argv 
)
inline

Method to parse command line arguments. Returns true if parsing was successful, false otherwise. Print error messages to stream. The hflag option specifies the help option.

Check if the argument is an option (starts with '-').

Check for aliases and get the actual option name.

Check if the option is defined.

If the next argument is not an option, set the value for this option.

Treat as boolean flag and flip its value.


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