mutable
A Database System for Research and Fast Prototyping
Loading...
Searching...
No Matches
Data Structures | Public Member Functions | Private Types | Private Attributes
m::ArgParser Class Reference

A parser for command line arguments. More...

#include <ArgParser.hpp>

Collaboration diagram for m::ArgParser:
[legend]

Data Structures

struct  Option
 
struct  OptionImpl
 

Public Member Functions

 ArgParser ()
 
 ~ArgParser ()
 
template<typename T , is_invocable< T > Callback>
void add (const char *group_name, const char *short_name, const char *long_name, const char *description, Callback &&callback)
 Adds a new group option to the ArgParser.
 
template<typename T , is_invocable< T > Callback>
void add (const char *short_name, const char *long_name, const char *description, Callback &&callback)
 Adds a new option to the ArgParser.
 
void print_args (std::ostream &out) const
 Prints a list of all options to out.
 
void print_args () const
 Prints a list of all options to std::cout.
 
void parse_args (int argc, const char **argv)
 Parses the arguments from argv.
 
void operator() (int argc, const char **argv)
 Parses the arguments from argv.
 
const std::vector< const char * > & args () const
 Returns all positional arguments.
 

Private Types

using options_t = std::vector< std::unique_ptr< const Option > >
 

‍options type


 

Private Attributes

StringPool pool_
 

‍pool of internalized strings


 
options_t general_options_
 

‍general options


 
std::unordered_map< PooledString, options_tgrouped_options_
 

‍group options


 
std::vector< const char * > args_
 

‍positional arguments


 
std::unordered_map< PooledString, std::reference_wrapper< const Option > > key_map_
 

‍maps the option name to the option object


 
std::size_t short_len_ = 0
 

‍the deducted maximum length of all short options


 
std::size_t long_len_ = 0
 

‍the deducted maximum length of all long options


 

Detailed Description

A parser for command line arguments.

Automates the parsing of command line arguments such as short options -s, long options --long, and positional arguments. Can print a nicely formatted help message with a synopsis and explanations of all available options.

Definition at line 19 of file ArgParser.hpp.

Member Typedef Documentation

◆ options_t

using m::ArgParser::options_t = std::vector<std::unique_ptr<const Option> >
private

‍options type

Definition at line 57 of file ArgParser.hpp.

Constructor & Destructor Documentation

◆ ArgParser()

m::ArgParser::ArgParser ( )
inline

Definition at line 72 of file ArgParser.hpp.

◆ ~ArgParser()

m::ArgParser::~ArgParser ( )
inline

Definition at line 73 of file ArgParser.hpp.

Member Function Documentation

◆ add() [1/2]

template<typename T , is_invocable< T > Callback>
void m::ArgParser::add ( const char *  group_name,
const char *  short_name,
const char *  long_name,
const char *  description,
Callback &&  callback 
)
inline

Adds a new group option to the ArgParser.

Parameters
group_namename of the group; can be nullptr, in which case the option is added to general options
short_namename of the short option, e.g. "-s"
long_namename of the long option, e.g. "--long"
descriptiona textual description of the option
callbacka callback function that is invoked if the option is given

Definition at line 84 of file ArgParser.hpp.

References m::Pooled< T, Pool, CanBeNone >::assert_not_none(), and M_insist.

Referenced by __attribute__(), M_LCOV_EXCL_STOP::__attribute__(), and LIST_CE().

◆ add() [2/2]

template<typename T , is_invocable< T > Callback>
void m::ArgParser::add ( const char *  short_name,
const char *  long_name,
const char *  description,
Callback &&  callback 
)
inline

Adds a new option to the ArgParser.

Parameters
short_namename of the short option, e.g. "-s"
long_namename of the long option, e.g. "--long"
descriptiona textual description of the option
callbacka callback function that is invoked if the option is given

Definition at line 119 of file ArgParser.hpp.

◆ args()

const std::vector< const char * > & m::ArgParser::args ( ) const
inline

Returns all positional arguments.

Definition at line 143 of file ArgParser.hpp.

Referenced by main().

◆ operator()()

void m::ArgParser::operator() ( int  argc,
const char **  argv 
)
inline

Parses the arguments from argv.

Parameters
argcnumber of arguments
argvarray of c-strings; last element must be nullptr

Definition at line 140 of file ArgParser.hpp.

◆ parse_args()

M_LCOV_EXCL_STOP void ArgParser::parse_args ( int  argc,
const char **  argv 
)

Parses the arguments from argv.

Parameters
argcnumber of arguments
argvarray of c-strings; last element must be nullptr

Definition at line 186 of file ArgParser.cpp.

References args_, key_map_, pool_, m::streq(), and m::strneq().

Referenced by main().

◆ print_args() [1/2]

void m::ArgParser::print_args ( ) const
inline

Prints a list of all options to std::cout.

Definition at line 126 of file ArgParser.hpp.

References print_args().

Referenced by print_args().

◆ print_args() [2/2]

M_LCOV_EXCL_START void ArgParser::print_args ( std::ostream &  out) const

Prints a list of all options to out.

Definition at line 158 of file ArgParser.cpp.

References general_options_, grouped_options_, long_len_, and short_len_.

Referenced by m::operator<<().

Field Documentation

◆ args_

std::vector<const char*> m::ArgParser::args_
private

‍positional arguments

Definition at line 63 of file ArgParser.hpp.

Referenced by parse_args().

◆ general_options_

options_t m::ArgParser::general_options_
private

‍general options

Definition at line 59 of file ArgParser.hpp.

Referenced by print_args().

◆ grouped_options_

std::unordered_map<PooledString, options_t> m::ArgParser::grouped_options_
private

‍group options

Definition at line 61 of file ArgParser.hpp.

Referenced by print_args().

◆ key_map_

std::unordered_map<PooledString, std::reference_wrapper<const Option> > m::ArgParser::key_map_
private

‍maps the option name to the option object

Definition at line 65 of file ArgParser.hpp.

Referenced by parse_args().

◆ long_len_

std::size_t m::ArgParser::long_len_ = 0
private

‍the deducted maximum length of all long options

Definition at line 69 of file ArgParser.hpp.

Referenced by print_args().

◆ pool_

StringPool m::ArgParser::pool_
private

‍pool of internalized strings

Definition at line 55 of file ArgParser.hpp.

Referenced by parse_args().

◆ short_len_

std::size_t m::ArgParser::short_len_ = 0
private

‍the deducted maximum length of all short options

Definition at line 67 of file ArgParser.hpp.

Referenced by print_args().


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