zz::cfg::ArgParser Class Reference

The ArgParser class. For parsing command line arguments. More...

#include <zupply.hpp>

Public Member Functions

 ArgParser ()
 ArgParser default constructor.
 
void add_info (std::string info)
 Add info to parser. More...
 
ArgOptionadd_opt (char shortKey, std::string longKey)
 Add option. Use -1 for shortkey if you don't want to specify short key. Use "" for longkey if you don't want to specify long key. More...
 
ArgOptionadd_opt (char shortKey)
 Add option, short version only. More...
 
ArgOptionadd_opt (std::string longKey)
 Add option, long version only. More...
 
template<typename T >
ArgOptionadd_opt_value (char shortKey, std::string longKey, T &dst, T defaultValue, std::string help="", std::string type="", int min=1, int max=1)
 Template function for an option take a value. This will store the value from argument to dst, otherwise dst = defaultValue. More...
 
ArgOptionadd_opt_flag (char shortKey, std::string longKey, std::string help="", bool *dst=nullptr)
 Add an toggle option. When this option found, dst = true, otherwise dst = false. More...
 
void add_opt_help (char shortKey, std::string longKey, std::string help="print this help and exit")
 Add a special option used to display help information for argument parser. When triggered, program will print help info and exit. More...
 
void add_opt_version (char shortKey, std::string longKey, std::string version, std::string help="print version and exit")
 Add a special option used to display version information for argument parser. When triggered, program will print version info and exit. More...
 
std::string version () const
 Return version info. More...
 
void parse (int argc, char **argv, bool ignoreUnknown=false)
 Start parsing arguments. More...
 
std::size_t count_error ()
 Get error count generated during parsing. More...
 
int count (char shortKey)
 Count the occurance of the option by short key. More...
 
int count (std::string longKey)
 Count the occurance of the option by long key. More...
 
std::string get_error ()
 Get all errors generated during parsing. More...
 
std::string get_help ()
 Get help information of entire parser. More...
 
Value operator[] (const std::string &longKey)
 Overloaded operator [] to retrieve the value by long key. More...
 
Value operator[] (const char shortKey)
 Overloaded operator [] to retrieve the value by short key. More...
 
std::vector< Valuearguments () const
 Return all input arguments that does not belong to any option. More...
 

Detailed Description

The ArgParser class. For parsing command line arguments.

Member Function Documentation

void zz::cfg::ArgParser::add_info ( std::string  info)

Add info to parser.

Parameters
infoInfo to add
ArgOption & zz::cfg::ArgParser::add_opt ( char  shortKey,
std::string  longKey 
)

Add option. Use -1 for shortkey if you don't want to specify short key. Use "" for longkey if you don't want to specify long key.

Parameters
shortKeyA char
longKeyA std::string
Returns
Reference to the added option
ArgOption & zz::cfg::ArgParser::add_opt ( char  shortKey)

Add option, short version only.

Parameters
shortKeyA char
Returns
Reference to the added option
ArgOption & zz::cfg::ArgParser::add_opt ( std::string  longKey)

Add option, long version only.

Parameters
longKeyA std::string
Returns
Reference to the added option
ArgOption & zz::cfg::ArgParser::add_opt_flag ( char  shortKey,
std::string  longKey,
std::string  help = "",
bool *  dst = nullptr 
)

Add an toggle option. When this option found, dst = true, otherwise dst = false.

Parameters
shortKeyA char
longKeyA std::string
helpHelp description.
dstPointer to a bool variable to be set accroding to this option.
Returns
Reference to the added option
void zz::cfg::ArgParser::add_opt_help ( char  shortKey,
std::string  longKey,
std::string  help = "print this help and exit" 
)

Add a special option used to display help information for argument parser. When triggered, program will print help info and exit.

Parameters
shortKeyA char
longKeyA std::string
helpOption description
template<typename T >
ArgOption & zz::cfg::ArgParser::add_opt_value ( char  shortKey,
std::string  longKey,
T &  dst,
defaultValue,
std::string  help = "",
std::string  type = "",
int  min = 1,
int  max = 1 
)
inline

Template function for an option take a value. This will store the value from argument to dst, otherwise dst = defaultValue.

Parameters
shortKeyA char
longKeyA std::string
dstReference to the variable to be set
defaultValueDefault value if this option not found
helpHelp description to this option
typeHelp describe the type of this option
minMinimum count of argument it takes
maxMaximum count of argument it takes, use -1 to allow unlimited arguments
Returns
Reference to the added option
void zz::cfg::ArgParser::add_opt_version ( char  shortKey,
std::string  longKey,
std::string  version,
std::string  help = "print version and exit" 
)

Add a special option used to display version information for argument parser. When triggered, program will print version info and exit.

Parameters
shortKeyA char
longKeyA std::string
helpOption description
std::vector< Value > zz::cfg::ArgParser::arguments ( ) const

Return all input arguments that does not belong to any option.

Returns
Arguments in vector
int zz::cfg::ArgParser::count ( char  shortKey)

Count the occurance of the option by short key.

Parameters
shortKey
Returns
Occurance count
int zz::cfg::ArgParser::count ( std::string  longKey)

Count the occurance of the option by long key.

Parameters
longKey
Returns
Occurance count
std::size_t zz::cfg::ArgParser::count_error ( )
inline

Get error count generated during parsing.

Returns
Number of errors generated.
std::string zz::cfg::ArgParser::get_error ( )

Get all errors generated during parsing.

Returns
Errors in string
std::string zz::cfg::ArgParser::get_help ( )

Get help information of entire parser.

Returns
All help information
Value zz::cfg::ArgParser::operator[] ( const std::string &  longKey)

Overloaded operator [] to retrieve the value by long key.

Parameters
longKey
Returns
Value type where the argument was stored
Value zz::cfg::ArgParser::operator[] ( const char  shortKey)

Overloaded operator [] to retrieve the value by short key.

Parameters
shortKey
Returns
Value type where the argument was stored
void zz::cfg::ArgParser::parse ( int  argc,
char **  argv,
bool  ignoreUnknown = false 
)

Start parsing arguments.

Parameters
argc
argv
ignoreUnknownWhether or not ignore unknown option keys
std::string zz::cfg::ArgParser::version ( ) const
inline

Return version info.

Returns
Version info in string

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