GRSISort "v4.0.0.5"
An extension of the ROOT analysis Framework
Loading...
Searching...
No Matches
ArgParser Class Reference

Detailed Description

This class is used to parse the command line arguments.

Example usage:

ArgParser parser;
int myOption = 0;
parser.option("some-option s", &myOption, true).description("my cool option").default_value(42);
parser.parse(argc, argv, true);
void parse(int argc, char **argv, bool firstPass)
Definition ArgParser.h:333
ArgParseConfigT< T > & option(const std::string flag, T *output_location, bool firstPass)
Definition ArgParser.h:412

This example reads the number after the flag "--some-option" or "-s" into the integer variable myOption. If no flag is provided, the default value of 42 is used.

The 3rd argument in ArgParser::option and ArgParser::parse that are set to true here are the "firstPass" arguments which simply means we want to read these options right away instead of reading something from an input file and only then parse the command line arguments.

Definition at line 317 of file ArgParser.h.

Public Member Functions

 ArgParser ()=default
 
 ArgParser (ArgParser &&) noexcept=default
 
 ArgParser (const ArgParser &)=default
 
 ~ArgParser ()
 
template<typename T >
ArgParseConfigT< std::vector< T > > & default_option (std::vector< T > *output_location, bool firstPass)
 
ArgParseroperator= (ArgParser &&) noexcept=default
 
ArgParseroperator= (const ArgParser &)=default
 
template<typename T >
ArgParseConfigT< T > & option (const std::string flag, T *output_location, bool firstPass)
 
void parse (int argc, char **argv, bool firstPass)
 
void parse_file (std::string &filename)
 
void print (std::ostream &out) const
 

Private Member Functions

ArgParseItemget_item (const std::string &flag)
 
void handle_default_option (int, char **argv, int &iarg, bool firstPass)
 
void handle_long_flag (int argc, char **argv, int &iarg, bool firstPass)
 
void handle_short_flag (int argc, char **argv, int &iarg, bool firstPass)
 

Static Private Member Functions

static std::vector< std::string > argument_list (int argc, char **argv, int &iarg, int max_args)
 

Private Attributes

std::vector< ArgParseItem * > values
 
+ Collaboration diagram for ArgParser:

Constructor & Destructor Documentation

◆ ArgParser() [1/3]

ArgParser::ArgParser ( )
default

◆ ArgParser() [2/3]

ArgParser::ArgParser ( const ArgParser & )
default

◆ ArgParser() [3/3]

ArgParser::ArgParser ( ArgParser && )
defaultnoexcept

◆ ~ArgParser()

ArgParser::~ArgParser ( )
inline

Definition at line 326 of file ArgParser.h.

References values.

Member Function Documentation

◆ argument_list()

static std::vector< std::string > ArgParser::argument_list ( int argc,
char ** argv,
int & iarg,
int max_args )
inlinestaticprivate

Reads arguments into a list until finding one that begins with '-'.

Definition at line 505 of file ArgParser.h.

Referenced by handle_long_flag(), and handle_short_flag().

◆ default_option()

template<typename T >
ArgParseConfigT< std::vector< T > > & ArgParser::default_option ( std::vector< T > * output_location,
bool firstPass )
inline

Definition at line 420 of file ArgParser.h.

References option().

Referenced by TGRSIOptions::Load().

◆ get_item()

ArgParseItem & ArgParser::get_item ( const std::string & flag)
inlineprivate

Definition at line 523 of file ArgParser.h.

References values.

Referenced by handle_default_option(), handle_long_flag(), handle_short_flag(), and parse_file().

◆ handle_default_option()

void ArgParser::handle_default_option ( int ,
char ** argv,
int & iarg,
bool firstPass )
inlineprivate

Definition at line 495 of file ArgParser.h.

References get_item(), and ArgParseItem::parse().

Referenced by parse().

◆ handle_long_flag()

void ArgParser::handle_long_flag ( int argc,
char ** argv,
int & iarg,
bool firstPass )
inlineprivate

Definition at line 446 of file ArgParser.h.

References argument_list(), get_item(), ArgParseItem::num_arguments(), and ArgParseItem::parse().

Referenced by parse().

◆ handle_short_flag()

void ArgParser::handle_short_flag ( int argc,
char ** argv,
int & iarg,
bool firstPass )
inlineprivate

Definition at line 470 of file ArgParser.h.

References argument_list(), get_item(), ArgParseItem::num_arguments(), and ArgParseItem::parse().

Referenced by parse().

◆ operator=() [1/2]

ArgParser & ArgParser::operator= ( ArgParser && )
defaultnoexcept

◆ operator=() [2/2]

ArgParser & ArgParser::operator= ( const ArgParser & )
default

◆ option()

template<typename T >
ArgParseConfigT< T > & ArgParser::option ( const std::string flag,
T * output_location,
bool firstPass )
inline

Definition at line 412 of file ArgParser.h.

References values.

Referenced by default_option(), TGRSIOptions::Load(), and main().

◆ parse()

void ArgParser::parse ( int argc,
char ** argv,
bool firstPass )
inline

This version takes argc and argv, parses them, and sets only those that have the matching firstPass flag set. This allows us to parse command line arguments in two stages, one to get the normal options and file names (from which the run info and analysis options are read), and a second stage, where only the run info and analysis option flags are parsed.

Definition at line 333 of file ArgParser.h.

References handle_default_option(), handle_long_flag(), handle_short_flag(), and values.

Referenced by TGRSIOptions::Load(), and main().

◆ parse_file()

void ArgParser::parse_file ( std::string & filename)
inline

Definition at line 365 of file ArgParser.h.

References get_item(), and ArgParseItem::parse().

Referenced by TGRSIOptions::Load().

◆ print()

void ArgParser::print ( std::ostream & out) const
inline

Definition at line 425 of file ArgParser.h.

References ArgParseItem::printable(), and values.

Referenced by operator<<().

Member Data Documentation

◆ values

std::vector<ArgParseItem*> ArgParser::values
private

Definition at line 540 of file ArgParser.h.

Referenced by ~ArgParser(), get_item(), option(), parse(), and print().