tlx
CmdlineParser Class Referenceabstract

base class of all options and parameters More...

#include <cmdline_parser.hpp>

Inheritance diagram for CmdlineParser:

Public Member Functions

 Argument (char key, const std::string &longkey, const std::string &keytype, const std::string &desc, bool required)
 contructor filling most attributes More...
 
virtual ~Argument ()=default
 empty virtual destructor More...
 
virtual const char * type_name () const =0
 return formatted type name to user More...
 
virtual bool process (int &argc, const char *const *&argv)=0
 process one item from command line for this argument More...
 
virtual void print_value (std::ostream &os) const =0
 format value to ostream More...
 
std::string param_text () const
 return 'longkey [keytype]' More...
 
std::string option_text () const
 return '-s, –longkey [keytype]' More...
 
 ArgumentBool (char key, const std::string &longkey, const std::string &keytype, const std::string &desc, bool required, bool &dest)
 contructor filling most attributes More...
 
const char * type_name () const final
 
bool process (int &argc, const char *const *&argv) final
 "process" argument: just set to true, no argument is used. More...
 
void print_value (std::ostream &os) const final
 
 ArgumentInt (char key, const std::string &longkey, const std::string &keytype, const std::string &desc, bool required, int &dest)
 contructor filling most attributes More...
 
const char * type_name () const final
 
bool process (int &argc, const char *const *&argv) final
 parse signed integer using sscanf. More...
 
void print_value (std::ostream &os) const final
 
 ArgumentUnsigned (char key, const std::string &longkey, const std::string &keytype, const std::string &desc, bool required, unsigned int &dest)
 contructor filling most attributes More...
 
const char * type_name () const final
 
bool process (int &argc, const char *const *&argv) final
 parse unsigned integer using sscanf. More...
 
void print_value (std::ostream &os) const final
 
 ArgumentSizeT (char key, const std::string &longkey, const std::string &keytype, const std::string &desc, bool required, size_t &dest)
 contructor filling most attributes More...
 
const char * type_name () const final
 
bool process (int &argc, const char *const *&argv) final
 parse size_t using sscanf. More...
 
void print_value (std::ostream &os) const final
 
 ArgumentFloat (char key, const std::string &longkey, const std::string &keytype, const std::string &desc, bool required, float &dest)
 contructor filling most attributes More...
 
const char * type_name () const final
 
bool process (int &argc, const char *const *&argv) final
 parse unsigned integer using sscanf. More...
 
void print_value (std::ostream &os) const final
 
 ArgumentDouble (char key, const std::string &longkey, const std::string &keytype, const std::string &desc, bool required, double &dest)
 contructor filling most attributes More...
 
const char * type_name () const final
 
bool process (int &argc, const char *const *&argv) final
 parse unsigned integer using sscanf. More...
 
void print_value (std::ostream &os) const final
 
 ArgumentBytes32 (char key, const std::string &longkey, const std::string &keytype, const std::string &desc, bool required, uint32_t &dest)
 contructor filling most attributes More...
 
const char * type_name () const final
 
bool process (int &argc, const char *const *&argv) final
 parse byte size using SI/IEC parser. More...
 
void print_value (std::ostream &os) const final
 
 ArgumentBytes64 (char key, const std::string &longkey, const std::string &keytype, const std::string &desc, bool required, uint64_t &dest)
 contructor filling most attributes More...
 
const char * type_name () const final
 
bool process (int &argc, const char *const *&argv) final
 parse byte size using SI/IEC parser. More...
 
void print_value (std::ostream &os) const final
 
 ArgumentString (char key, const std::string &longkey, const std::string &keytype, const std::string &desc, bool required, std::string &dest)
 contructor filling most attributes More...
 
const char * type_name () const final
 
bool process (int &argc, const char *const *&argv) final
 "process" string argument just by storing it. More...
 
void print_value (std::ostream &os) const final
 
 ArgumentStringlist (char key, const std::string &longkey, const std::string &keytype, const std::string &desc, bool required, std::vector< std::string > &dest)
 contructor filling most attributes More...
 
const char * type_name () const final
 
bool process (int &argc, const char *const *&argv) final
 "process" string argument just by storing it in vector. More...
 
void print_value (std::ostream &os) const final
 
 CmdlineParser ()
 Constructor. More...
 
 ~CmdlineParser ()
 Delete all added arguments. More...
 
void set_description (const std::string &description)
 Set description of program, text will be wrapped. More...
 
void set_author (const std::string &author)
 Set author of program, will be wrapped. More...
 
void set_verbose_process (bool verbose_process)
 Set verbose processing of command line arguments. More...
 
void print_usage (std::ostream &os)
 output nicely formatted usage information including description of all parameters and options. More...
 
void print_usage ()
 output to std::cout nicely formatted usage information including description of all parameters and options. More...
 
CmdlineParsersort ()
 sort options by key (but not the positional parameters) More...
 
bool process (int argc, const char *const *argv, std::ostream &os)
 parse command line options as specified by the options and parameters added. More...
 
bool process (int argc, const char *const *argv)
 parse command line options as specified by the options and parameters added. More...
 
void print_result (std::ostream &os)
 print nicely formatted result of processing More...
 
void print_result ()
 print nicely formatted result of processing to std::cout More...
 
Add Option with short -k, --longkey, and description.
void add_bool (char key, const std::string &longkey, bool &dest, const std::string &desc)
 add boolean option flag -key, –longkey with description and store to dest More...
 
void add_flag (char key, const std::string &longkey, bool &dest, const std::string &desc)
 add boolean option flag -key, –longkey with description and store to dest. More...
 
void add_int (char key, const std::string &longkey, int &dest, const std::string &desc)
 add signed integer option -key, –longkey with description and store to dest More...
 
void add_unsigned (char key, const std::string &longkey, unsigned int &dest, const std::string &desc)
 add unsigned integer option -key, –longkey with description and store to dest More...
 
void add_uint (char key, const std::string &longkey, unsigned int &dest, const std::string &desc)
 add unsigned integer option -key, –longkey with description and store to dest. More...
 
void add_size_t (char key, const std::string &longkey, size_t &dest, const std::string &desc)
 add size_t option -key, –longkey with description and store to dest More...
 
void add_float (char key, const std::string &longkey, float &dest, const std::string &desc)
 add float option -key, –longkey with description and store to dest More...
 
void add_double (char key, const std::string &longkey, double &dest, const std::string &desc)
 add double option -key, –longkey with description and store to dest More...
 
void add_bytes (char key, const std::string &longkey, uint32_t &dest, const std::string &desc)
 add SI/IEC suffixes byte size option -key, –longkey and store to 32-bit dest More...
 
void add_bytes (char key, const std::string &longkey, uint64_t &dest, const std::string &desc)
 add SI/IEC suffixes byte size option -key, –longkey and store to 64-bit dest More...
 
void add_string (char key, const std::string &longkey, std::string &dest, const std::string &desc)
 add string option -key, –longkey and store to dest More...
 
void add_stringlist (char key, const std::string &longkey, std::vector< std::string > &dest, const std::string &desc)
 add string list option -key, –longkey and store to dest More...
 
Add Option with --longkey and description.
void add_bool (const std::string &longkey, bool &dest, const std::string &desc)
 add boolean option flag –longkey with description and store to dest More...
 
void add_flag (const std::string &longkey, bool &dest, const std::string &desc)
 add boolean option flag –longkey with description and store to dest. More...
 
void add_int (const std::string &longkey, int &dest, const std::string &desc)
 add signed integer option –longkey with description and store to dest More...
 
void add_unsigned (const std::string &longkey, unsigned int &dest, const std::string &desc)
 add unsigned integer option –longkey with description and store to dest More...
 
void add_uint (const std::string &longkey, unsigned int &dest, const std::string &desc)
 add unsigned integer option –longkey with description and store to dest. More...
 
void add_size_t (const std::string &longkey, size_t &dest, const std::string &desc)
 add size_t option –longkey with description and store to dest More...
 
void add_float (const std::string &longkey, float &dest, const std::string &desc)
 add float option –longkey with description and store to dest More...
 
void add_double (const std::string &longkey, double &dest, const std::string &desc)
 add double option –longkey with description and store to dest More...
 
void add_bytes (const std::string &longkey, uint32_t &dest, const std::string &desc)
 add SI/IEC suffixes byte size option –longkey and store to 32-bit dest More...
 
void add_bytes (const std::string &longkey, uint64_t &dest, const std::string &desc)
 add SI/IEC suffixes byte size option –longkey and store to 64-bit dest More...
 
void add_string (const std::string &longkey, std::string &dest, const std::string &desc)
 add string option –longkey and store to dest More...
 
void add_stringlist (const std::string &longkey, std::vector< std::string > &dest, const std::string &desc)
 add string list option –longkey and store to dest More...
 
Add Option with short -k, --longkey, [keytype], and description.
void add_bool (char key, const std::string &longkey, const std::string &keytype, bool &dest, const std::string &desc)
 add boolean option flag -key, –longkey [keytype] with description and store to dest More...
 
void add_flag (char key, const std::string &longkey, const std::string &keytype, bool &dest, const std::string &desc)
 add boolean option flag -key, –longkey [keytype] with description and store to dest. More...
 
void add_int (char key, const std::string &longkey, const std::string &keytype, int &dest, const std::string &desc)
 add signed integer option -key, –longkey [keytype] with description and store to dest More...
 
void add_unsigned (char key, const std::string &longkey, const std::string &keytype, unsigned int &dest, const std::string &desc)
 add unsigned integer option -key, –longkey [keytype] with description and store to dest More...
 
void add_uint (char key, const std::string &longkey, const std::string &keytype, unsigned int &dest, const std::string &desc)
 add unsigned integer option -key, –longkey [keytype] with description and store to dest. More...
 
void add_size_t (char key, const std::string &longkey, const std::string &keytype, size_t &dest, const std::string &desc)
 add size_t option -key, –longkey [keytype] with description and store to dest More...
 
void add_float (char key, const std::string &longkey, const std::string &keytype, float &dest, const std::string &desc)
 add float option -key, –longkey [keytype] with description and store to dest More...
 
void add_double (char key, const std::string &longkey, const std::string &keytype, double &dest, const std::string &desc)
 add double option -key, –longkey [keytype] with description and store to dest More...
 
void add_bytes (char key, const std::string &longkey, const std::string &keytype, uint32_t &dest, const std::string &desc)
 add SI/IEC suffixes byte size option -key, –longkey [keytype] and store to 64-bit dest More...
 
void add_bytes (char key, const std::string &longkey, const std::string &keytype, uint64_t &dest, const std::string &desc)
 add SI/IEC suffixes byte size option -key, –longkey [keytype] and store to 64-bit dest More...
 
void add_string (char key, const std::string &longkey, const std::string &keytype, std::string &dest, const std::string &desc)
 add string option -key, –longkey [keytype] and store to dest More...
 
void add_stringlist (char key, const std::string &longkey, const std::string &keytype, std::vector< std::string > &dest, const std::string &desc)
 add string list option -key, –longkey [keytype] and store to dest More...
 
Add Required Parameter [name] with description.
void add_param_int (const std::string &name, int &dest, const std::string &desc)
 add signed integer parameter [name] with description and store to dest More...
 
void add_param_unsigned (const std::string &name, unsigned int &dest, const std::string &desc)
 add unsigned integer parameter [name] with description and store to dest More...
 
void add_param_uint (const std::string &name, unsigned int &dest, const std::string &desc)
 add unsigned integer parameter [name] with description and store to dest. More...
 
void add_param_size_t (const std::string &name, size_t &dest, const std::string &desc)
 add size_t parameter [name] with description and store to dest More...
 
void add_param_float (const std::string &name, float &dest, const std::string &desc)
 add float parameter [name] with description and store to dest More...
 
void add_param_double (const std::string &name, double &dest, const std::string &desc)
 add double parameter [name] with description and store to dest More...
 
void add_param_bytes (const std::string &name, uint32_t &dest, const std::string &desc)
 add SI/IEC suffixes byte size parameter [name] with description and store to dest More...
 
void add_param_bytes (const std::string &name, uint64_t &dest, const std::string &desc)
 add SI/IEC suffixes byte size parameter [name] with description and store to dest More...
 
void add_param_string (const std::string &name, std::string &dest, const std::string &desc)
 add string parameter [name] with description and store to dest More...
 
void add_param_stringlist (const std::string &name, std::vector< std::string > &dest, const std::string &desc)
 add string list parameter [name] with description and store to dest. More...
 
Add Optional Parameter [name] with description.
void add_opt_param_int (const std::string &name, int &dest, const std::string &desc)
 add optional signed integer parameter [name] with description and store to dest More...
 
void add_opt_param_unsigned (const std::string &name, unsigned int &dest, const std::string &desc)
 add optional unsigned integer parameter [name] with description and store to dest More...
 
void add_opt_param_uint (const std::string &name, unsigned int &dest, const std::string &desc)
 add optional unsigned integer parameter [name] with description and store to dest. More...
 
void add_opt_param_size_t (const std::string &name, size_t &dest, const std::string &desc)
 add optional size_t parameter [name] with description and store to dest More...
 
void add_opt_param_float (const std::string &name, float &dest, const std::string &desc)
 add optional float parameter [name] with description and store to dest More...
 
void add_opt_param_double (const std::string &name, double &dest, const std::string &desc)
 add optional double parameter [name] with description and store to dest More...
 
void add_opt_param_bytes (const std::string &name, uint32_t &dest, const std::string &desc)
 add optional SI/IEC suffixes byte size parameter [name] with description and store to dest More...
 
void add_opt_param_bytes (const std::string &name, uint64_t &dest, const std::string &desc)
 add optional SI/IEC suffixes byte size parameter [name] with description and store to dest More...
 
void add_opt_param_string (const std::string &name, std::string &dest, const std::string &desc)
 add optional string parameter [name] with description and store to dest More...
 
void add_opt_param_stringlist (const std::string &name, std::vector< std::string > &dest, const std::string &desc)
 add optional string parameter [name] with description and store to dest More...
 

Static Public Member Functions

static void output_wrap (std::ostream &os, const std::string &text, size_t wraplen, size_t indent_first=0, size_t indent_rest=0, size_t current=0, size_t indent_newline=0)
 Wrap a long string at spaces into lines. More...
 

Public Attributes

char key_
 single letter short option, or 0 is none More...
 
std::string longkey_
 long option key or name for parameters More...
 
std::string keytype_
 option type description, e.g. "<#>" to indicate numbers More...
 
std::string desc_
 longer description, which will be wrapped More...
 
bool required_
 required, process() fails if the option/parameter is not found. More...
 
bool found_
 found during processing of command line More...
 
bool repeated_
 repeated argument, i.e. std::vector<std::string> More...
 

Protected Attributes

bool & dest_
 reference to boolean to set to true More...
 
int & dest_
 
unsigned int & dest_
 
size_t & dest_
 
float & dest_
 
double & dest_
 
uint32_t & dest_
 
uint64_t & dest_
 
std::string & dest_
 
std::vector< std::string > & dest_
 

Private Types

using ArgumentList = std::vector< Argument * >
 option and parameter list type More...
 

Private Member Functions

void calc_option_max (const Argument *arg)
 update maximum formatting width for new option More...
 
void calc_param_max (const Argument *arg)
 update maximum formatting width for new parameter More...
 
void print_option_error (int argc, const char *const *argv, const Argument *arg, std::ostream &os)
 print error about option. More...
 
void print_param_error (int argc, const char *const *argv, const Argument *arg, std::ostream &os)
 print error about parameter. More...
 

Private Attributes

ArgumentList option_list_
 list of options available More...
 
ArgumentList param_list_
 list of parameters, both required and optional More...
 
size_t option_max_width_
 formatting width for options, '-s, –switch <#>' More...
 
size_t param_max_width_
 formatting width for parameters, 'param <#>' More...
 
const char * program_name_
 argv[0] for usage. More...
 
bool verbose_process_
 verbose processing of arguments More...
 
std::string description_
 user set description of program, will be wrapped More...
 
std::string author_
 user set author of program, will be wrapped More...
 
unsigned int line_wrap_
 set line wrap length More...
 

Static Private Attributes

static constexpr int max_type_name_
 maximum length of a type_name() result More...
 

Detailed Description

base class of all options and parameters

Command line parser which automatically fills variables and prints nice usage messages.

specialization of argument for multiple string options or parameters

specialization of argument for string options or parameters

specialization of argument for SI/IEC suffixes byte size options or parameters

specialization of argument for double options or parameters

specialization of argument for float options or parameters

specialization of argument for size_t options or parameters

specialization of argument for unsigned integer options or parameters

specialization of argument for integer options or parameters

specialization of argument for boolean flags (can only be set to true).

This is a straightforward command line parser in C++, which will recognize short options -s, long options –long and parameters, both required and optional. It will automatically parse integers and byte sizes with SI/IEC suffixes (e.g. 1 GiB). It also works with lists of strings, e.g. multiple filenames.

#include <iostream>
#include <string>
int main(int argc, char* argv[]) {
// add description and author
cp.set_description("This may some day be a useful program, which solves "
"many serious problems of the real world and achives "
"global peace.");
cp.set_author("Timo Bingmann <tb@panthema.net>");
// add an unsigned integer option --rounds <N>
unsigned rounds = 0;
cp.add_unsigned('r', "rounds", "N", rounds,
"Run N rounds of the experiment.");
// add a byte size argument which the user can enter like '1gi'
uint64_t a_size = 0;
cp.add_bytes('s', "size", a_size,
"Number of bytes to process.");
// add a required parameter
std::string a_filename;
cp.add_param_string("filename", a_filename,
"A filename to process");
// process command line
if (!cp.process(argc, argv))
return -1; // some error occurred and help was always written to user.
std::cout << "Command line parsed okay." << std::endl;
// output for debugging
// do something useful
return 0;
}

When running the program above without arguments, it will print:

$ ./tlx_cmdline_parser_example
Missing required argument for parameter 'filename'

Usage: ./tlx_cmdline_parser_example [options] <filename>

This may some day be a useful program, which solves many serious problems of
the real world and achives global peace.

Author: Timo Bingmann <tb@panthema.net>

Parameters:
  filename  A filename to process
Options:
  -r, --rounds N  Run N rounds of the experiment.
  -s, --size      Number of bytes to process.

Nice output, notice the line wrapping of the description and formatting of parameters and arguments. These too are wrapped if the description is too long.

We now try to give the program some arguments:

$ ./tlx_cmdline_parser_example -s 2GiB -r 42 /dev/null
Option -s, --size set to 2147483648.
Option -r, --rounds N set to 42.
Parameter filename set to "/dev/null".
Command line parsed okay.
Parameters:
  filename        (string)            "/dev/null"
Options:
  -r, --rounds N  (unsigned integer)  42
  -s, --size      (bytes)             2147483648

The output shows pretty much what happens. The command line parser is by default in a verbose mode outputting all arguments and values parsed. The debug summary shows to have values the corresponding variables were set.

One feature worth naming is that the parser also supports lists of strings, i.e. std::vector<std::string> via CmdlineParser::add_param_stringlist() and similar.

Definition at line 32 of file cmdline_parser.cpp.

Member Typedef Documentation

using ArgumentList = std::vector<Argument*>
private

option and parameter list type

Definition at line 95 of file cmdline_parser.hpp.

Constructor & Destructor Documentation

virtual ~Argument ( )
virtualdefault

empty virtual destructor

Constructor.

Definition at line 490 of file cmdline_parser.cpp.

Delete all added arguments.

Definition at line 492 of file cmdline_parser.cpp.

Member Function Documentation

void add_bool ( char  key,
const std::string &  longkey,
bool &  dest,
const std::string &  desc 
)

add boolean option flag -key, –longkey with description and store to dest

Definition at line 612 of file cmdline_parser.cpp.

void add_bool ( const std::string &  longkey,
bool &  dest,
const std::string &  desc 
)

add boolean option flag –longkey with description and store to dest

Definition at line 675 of file cmdline_parser.cpp.

void add_bool ( char  key,
const std::string &  longkey,
const std::string &  keytype,
bool &  dest,
const std::string &  desc 
)

add boolean option flag -key, –longkey [keytype] with description and store to dest

Definition at line 516 of file cmdline_parser.cpp.

void add_bytes ( char  key,
const std::string &  longkey,
uint32_t &  dest,
const std::string &  desc 
)

add SI/IEC suffixes byte size option -key, –longkey and store to 32-bit dest

Definition at line 652 of file cmdline_parser.cpp.

void add_bytes ( char  key,
const std::string &  longkey,
uint64_t &  dest,
const std::string &  desc 
)

add SI/IEC suffixes byte size option -key, –longkey and store to 64-bit dest

Definition at line 657 of file cmdline_parser.cpp.

void add_bytes ( const std::string &  longkey,
uint32_t &  dest,
const std::string &  desc 
)

add SI/IEC suffixes byte size option –longkey and store to 32-bit dest

Definition at line 715 of file cmdline_parser.cpp.

void add_bytes ( const std::string &  longkey,
uint64_t &  dest,
const std::string &  desc 
)

add SI/IEC suffixes byte size option –longkey and store to 64-bit dest

Definition at line 720 of file cmdline_parser.cpp.

void add_bytes ( char  key,
const std::string &  longkey,
const std::string &  keytype,
uint32_t &  dest,
const std::string &  desc 
)

add SI/IEC suffixes byte size option -key, –longkey [keytype] and store to 64-bit dest

Definition at line 576 of file cmdline_parser.cpp.

void add_bytes ( char  key,
const std::string &  longkey,
const std::string &  keytype,
uint64_t &  dest,
const std::string &  desc 
)

add SI/IEC suffixes byte size option -key, –longkey [keytype] and store to 64-bit dest

Definition at line 584 of file cmdline_parser.cpp.

void add_double ( char  key,
const std::string &  longkey,
double &  dest,
const std::string &  desc 
)

add double option -key, –longkey with description and store to dest

Definition at line 647 of file cmdline_parser.cpp.

void add_double ( const std::string &  longkey,
double &  dest,
const std::string &  desc 
)

add double option –longkey with description and store to dest

Definition at line 710 of file cmdline_parser.cpp.

void add_double ( char  key,
const std::string &  longkey,
const std::string &  keytype,
double &  dest,
const std::string &  desc 
)

add double option -key, –longkey [keytype] with description and store to dest

Definition at line 568 of file cmdline_parser.cpp.

void add_flag ( char  key,
const std::string &  longkey,
bool &  dest,
const std::string &  desc 
)

add boolean option flag -key, –longkey with description and store to dest.

identical to add_bool()

Definition at line 617 of file cmdline_parser.cpp.

void add_flag ( const std::string &  longkey,
bool &  dest,
const std::string &  desc 
)

add boolean option flag –longkey with description and store to dest.

identical to add_bool()

Definition at line 680 of file cmdline_parser.cpp.

void add_flag ( char  key,
const std::string &  longkey,
const std::string &  keytype,
bool &  dest,
const std::string &  desc 
)

add boolean option flag -key, –longkey [keytype] with description and store to dest.

identical to add_bool()

Definition at line 524 of file cmdline_parser.cpp.

void add_float ( char  key,
const std::string &  longkey,
float &  dest,
const std::string &  desc 
)

add float option -key, –longkey with description and store to dest

Definition at line 642 of file cmdline_parser.cpp.

void add_float ( const std::string &  longkey,
float &  dest,
const std::string &  desc 
)

add float option –longkey with description and store to dest

Definition at line 705 of file cmdline_parser.cpp.

void add_float ( char  key,
const std::string &  longkey,
const std::string &  keytype,
float &  dest,
const std::string &  desc 
)

add float option -key, –longkey [keytype] with description and store to dest

Definition at line 560 of file cmdline_parser.cpp.

void add_int ( char  key,
const std::string &  longkey,
int &  dest,
const std::string &  desc 
)

add signed integer option -key, –longkey with description and store to dest

Definition at line 622 of file cmdline_parser.cpp.

void add_int ( const std::string &  longkey,
int &  dest,
const std::string &  desc 
)

add signed integer option –longkey with description and store to dest

Definition at line 685 of file cmdline_parser.cpp.

void add_int ( char  key,
const std::string &  longkey,
const std::string &  keytype,
int &  dest,
const std::string &  desc 
)

add signed integer option -key, –longkey [keytype] with description and store to dest

Definition at line 530 of file cmdline_parser.cpp.

void add_opt_param_bytes ( const std::string &  name,
uint32_t &  dest,
const std::string &  desc 
)

add optional SI/IEC suffixes byte size parameter [name] with description and store to dest

Definition at line 841 of file cmdline_parser.cpp.

void add_opt_param_bytes ( const std::string &  name,
uint64_t &  dest,
const std::string &  desc 
)

add optional SI/IEC suffixes byte size parameter [name] with description and store to dest

Definition at line 848 of file cmdline_parser.cpp.

void add_opt_param_double ( const std::string &  name,
double &  dest,
const std::string &  desc 
)

add optional double parameter [name] with description and store to dest

Definition at line 834 of file cmdline_parser.cpp.

void add_opt_param_float ( const std::string &  name,
float &  dest,
const std::string &  desc 
)

add optional float parameter [name] with description and store to dest

Definition at line 828 of file cmdline_parser.cpp.

void add_opt_param_int ( const std::string &  name,
int &  dest,
const std::string &  desc 
)

add optional signed integer parameter [name] with description and store to dest

Definition at line 804 of file cmdline_parser.cpp.

void add_opt_param_size_t ( const std::string &  name,
size_t &  dest,
const std::string &  desc 
)

add optional size_t parameter [name] with description and store to dest

Definition at line 822 of file cmdline_parser.cpp.

void add_opt_param_string ( const std::string &  name,
std::string &  dest,
const std::string &  desc 
)

add optional string parameter [name] with description and store to dest

Definition at line 855 of file cmdline_parser.cpp.

void add_opt_param_stringlist ( const std::string &  name,
std::vector< std::string > &  dest,
const std::string &  desc 
)

add optional string parameter [name] with description and store to dest

Warning
this parameter must be last, as it will gobble all non-option arguments!

Definition at line 862 of file cmdline_parser.cpp.

void add_opt_param_uint ( const std::string &  name,
unsigned int &  dest,
const std::string &  desc 
)

add optional unsigned integer parameter [name] with description and store to dest.

identical to add_unsigned()

Definition at line 817 of file cmdline_parser.cpp.

void add_opt_param_unsigned ( const std::string &  name,
unsigned int &  dest,
const std::string &  desc 
)

add optional unsigned integer parameter [name] with description and store to dest

Definition at line 810 of file cmdline_parser.cpp.

void add_param_bytes ( const std::string &  name,
uint32_t &  dest,
const std::string &  desc 
)

add SI/IEC suffixes byte size parameter [name] with description and store to dest

Definition at line 774 of file cmdline_parser.cpp.

void add_param_bytes ( const std::string &  name,
uint64_t &  dest,
const std::string &  desc 
)

add SI/IEC suffixes byte size parameter [name] with description and store to dest

Definition at line 781 of file cmdline_parser.cpp.

void add_param_double ( const std::string &  name,
double &  dest,
const std::string &  desc 
)

add double parameter [name] with description and store to dest

Definition at line 768 of file cmdline_parser.cpp.

void add_param_float ( const std::string &  name,
float &  dest,
const std::string &  desc 
)

add float parameter [name] with description and store to dest

Definition at line 762 of file cmdline_parser.cpp.

void add_param_int ( const std::string &  name,
int &  dest,
const std::string &  desc 
)

add signed integer parameter [name] with description and store to dest

Definition at line 738 of file cmdline_parser.cpp.

void add_param_size_t ( const std::string &  name,
size_t &  dest,
const std::string &  desc 
)

add size_t parameter [name] with description and store to dest

Definition at line 756 of file cmdline_parser.cpp.

void add_param_string ( const std::string &  name,
std::string &  dest,
const std::string &  desc 
)

add string parameter [name] with description and store to dest

Definition at line 788 of file cmdline_parser.cpp.

void add_param_stringlist ( const std::string &  name,
std::vector< std::string > &  dest,
const std::string &  desc 
)

add string list parameter [name] with description and store to dest.

Warning
this parameter must be last, as it will gobble all non-option arguments!

Definition at line 794 of file cmdline_parser.cpp.

void add_param_uint ( const std::string &  name,
unsigned int &  dest,
const std::string &  desc 
)

add unsigned integer parameter [name] with description and store to dest.

identical to add_unsigned()

Definition at line 751 of file cmdline_parser.cpp.

void add_param_unsigned ( const std::string &  name,
unsigned int &  dest,
const std::string &  desc 
)

add unsigned integer parameter [name] with description and store to dest

Definition at line 744 of file cmdline_parser.cpp.

void add_size_t ( char  key,
const std::string &  longkey,
size_t &  dest,
const std::string &  desc 
)

add size_t option -key, –longkey with description and store to dest

Definition at line 637 of file cmdline_parser.cpp.

void add_size_t ( const std::string &  longkey,
size_t &  dest,
const std::string &  desc 
)

add size_t option –longkey with description and store to dest

Definition at line 700 of file cmdline_parser.cpp.

void add_size_t ( char  key,
const std::string &  longkey,
const std::string &  keytype,
size_t &  dest,
const std::string &  desc 
)

add size_t option -key, –longkey [keytype] with description and store to dest

Definition at line 552 of file cmdline_parser.cpp.

void add_string ( char  key,
const std::string &  longkey,
std::string &  dest,
const std::string &  desc 
)

add string option -key, –longkey and store to dest

Definition at line 662 of file cmdline_parser.cpp.

void add_string ( const std::string &  longkey,
std::string &  dest,
const std::string &  desc 
)

add string option –longkey and store to dest

Definition at line 725 of file cmdline_parser.cpp.

void add_string ( char  key,
const std::string &  longkey,
const std::string &  keytype,
std::string &  dest,
const std::string &  desc 
)

add string option -key, –longkey [keytype] and store to dest

Definition at line 592 of file cmdline_parser.cpp.

void add_stringlist ( char  key,
const std::string &  longkey,
std::vector< std::string > &  dest,
const std::string &  desc 
)

add string list option -key, –longkey and store to dest

Definition at line 667 of file cmdline_parser.cpp.

void add_stringlist ( const std::string &  longkey,
std::vector< std::string > &  dest,
const std::string &  desc 
)

add string list option –longkey and store to dest

Definition at line 730 of file cmdline_parser.cpp.

void add_stringlist ( char  key,
const std::string &  longkey,
const std::string &  keytype,
std::vector< std::string > &  dest,
const std::string &  desc 
)

add string list option -key, –longkey [keytype] and store to dest

Definition at line 600 of file cmdline_parser.cpp.

void add_uint ( char  key,
const std::string &  longkey,
unsigned int &  dest,
const std::string &  desc 
)

add unsigned integer option -key, –longkey with description and store to dest.

identical to add_unsigned()

Definition at line 632 of file cmdline_parser.cpp.

void add_uint ( const std::string &  longkey,
unsigned int &  dest,
const std::string &  desc 
)

add unsigned integer option –longkey with description and store to dest.

identical to add_unsigned()

Definition at line 695 of file cmdline_parser.cpp.

void add_uint ( char  key,
const std::string &  longkey,
const std::string &  keytype,
unsigned int &  dest,
const std::string &  desc 
)

add unsigned integer option -key, –longkey [keytype] with description and store to dest.

identical to add_unsigned()

Definition at line 546 of file cmdline_parser.cpp.

void add_unsigned ( char  key,
const std::string &  longkey,
unsigned int &  dest,
const std::string &  desc 
)

add unsigned integer option -key, –longkey with description and store to dest

Definition at line 627 of file cmdline_parser.cpp.

void add_unsigned ( const std::string &  longkey,
unsigned int &  dest,
const std::string &  desc 
)

add unsigned integer option –longkey with description and store to dest

Definition at line 690 of file cmdline_parser.cpp.

void add_unsigned ( char  key,
const std::string &  longkey,
const std::string &  keytype,
unsigned int &  dest,
const std::string &  desc 
)

add unsigned integer option -key, –longkey [keytype] with description and store to dest

Definition at line 538 of file cmdline_parser.cpp.

Argument ( char  key,
const std::string &  longkey,
const std::string &  keytype,
const std::string &  desc,
bool  required 
)
inline

contructor filling most attributes

Definition at line 52 of file cmdline_parser.cpp.

ArgumentBool ( char  key,
const std::string &  longkey,
const std::string &  keytype,
const std::string &  desc,
bool  required,
bool &  dest 
)
inline

contructor filling most attributes

Definition at line 105 of file cmdline_parser.cpp.

ArgumentBytes32 ( char  key,
const std::string &  longkey,
const std::string &  keytype,
const std::string &  desc,
bool  required,
uint32_t &  dest 
)
inline

contructor filling most attributes

Definition at line 309 of file cmdline_parser.cpp.

ArgumentBytes64 ( char  key,
const std::string &  longkey,
const std::string &  keytype,
const std::string &  desc,
bool  required,
uint64_t &  dest 
)
inline

contructor filling most attributes

Definition at line 344 of file cmdline_parser.cpp.

ArgumentDouble ( char  key,
const std::string &  longkey,
const std::string &  keytype,
const std::string &  desc,
bool  required,
double &  dest 
)
inline

contructor filling most attributes

Definition at line 274 of file cmdline_parser.cpp.

ArgumentFloat ( char  key,
const std::string &  longkey,
const std::string &  keytype,
const std::string &  desc,
bool  required,
float &  dest 
)
inline

contructor filling most attributes

Definition at line 241 of file cmdline_parser.cpp.

ArgumentInt ( char  key,
const std::string &  longkey,
const std::string &  keytype,
const std::string &  desc,
bool  required,
int &  dest 
)
inline

contructor filling most attributes

Definition at line 133 of file cmdline_parser.cpp.

ArgumentSizeT ( char  key,
const std::string &  longkey,
const std::string &  keytype,
const std::string &  desc,
bool  required,
size_t &  dest 
)
inline

contructor filling most attributes

Definition at line 205 of file cmdline_parser.cpp.

ArgumentString ( char  key,
const std::string &  longkey,
const std::string &  keytype,
const std::string &  desc,
bool  required,
std::string &  dest 
)
inline

contructor filling most attributes

Definition at line 376 of file cmdline_parser.cpp.

ArgumentStringlist ( char  key,
const std::string &  longkey,
const std::string &  keytype,
const std::string &  desc,
bool  required,
std::vector< std::string > &  dest 
)
inline

contructor filling most attributes

Definition at line 406 of file cmdline_parser.cpp.

ArgumentUnsigned ( char  key,
const std::string &  longkey,
const std::string &  keytype,
const std::string &  desc,
bool  required,
unsigned int &  dest 
)
inline

contructor filling most attributes

Definition at line 169 of file cmdline_parser.cpp.

void calc_option_max ( const Argument arg)
private

update maximum formatting width for new option

Definition at line 437 of file cmdline_parser.cpp.

void calc_param_max ( const Argument arg)
private

update maximum formatting width for new parameter

Definition at line 442 of file cmdline_parser.cpp.

std::string option_text ( ) const
inline

return '-s, –longkey [keytype]'

Definition at line 79 of file cmdline_parser.cpp.

void output_wrap ( std::ostream &  os,
const std::string &  text,
size_t  wraplen,
size_t  indent_first = 0,
size_t  indent_rest = 0,
size_t  current = 0,
size_t  indent_newline = 0 
)
static

Wrap a long string at spaces into lines.

Prefix is added unconditionally to each line. Lines are wrapped after wraplen characters if possible.

Definition at line 449 of file cmdline_parser.cpp.

std::string param_text ( ) const
inline

return 'longkey [keytype]'

Definition at line 70 of file cmdline_parser.cpp.

void print_option_error ( int  argc,
const char *const *  argv,
const Argument arg,
std::ostream &  os 
)
private

print error about option.

Definition at line 942 of file cmdline_parser.cpp.

void print_param_error ( int  argc,
const char *const *  argv,
const Argument arg,
std::ostream &  os 
)
private

print error about parameter.

Definition at line 955 of file cmdline_parser.cpp.

void print_result ( std::ostream &  os)

print nicely formatted result of processing

Definition at line 1114 of file cmdline_parser.cpp.

void print_result ( )

print nicely formatted result of processing to std::cout

Definition at line 1160 of file cmdline_parser.cpp.

void print_usage ( std::ostream &  os)

output nicely formatted usage information including description of all parameters and options.

Definition at line 880 of file cmdline_parser.cpp.

void print_usage ( )

output to std::cout nicely formatted usage information including description of all parameters and options.

Definition at line 938 of file cmdline_parser.cpp.

virtual void print_value ( std::ostream &  os) const
pure virtual

format value to ostream

void print_value ( std::ostream &  os) const
inlinefinal

Definition at line 119 of file cmdline_parser.cpp.

void print_value ( std::ostream &  os) const
inlinefinal

Definition at line 157 of file cmdline_parser.cpp.

void print_value ( std::ostream &  os) const
inlinefinal

Definition at line 193 of file cmdline_parser.cpp.

void print_value ( std::ostream &  os) const
inlinefinal

Definition at line 229 of file cmdline_parser.cpp.

void print_value ( std::ostream &  os) const
inlinefinal

Definition at line 263 of file cmdline_parser.cpp.

void print_value ( std::ostream &  os) const
inlinefinal

Definition at line 296 of file cmdline_parser.cpp.

void print_value ( std::ostream &  os) const
inlinefinal

Definition at line 332 of file cmdline_parser.cpp.

void print_value ( std::ostream &  os) const
inlinefinal

Definition at line 364 of file cmdline_parser.cpp.

void print_value ( std::ostream &  os) const
inlinefinal

Definition at line 392 of file cmdline_parser.cpp.

void print_value ( std::ostream &  os) const
inlinefinal

Definition at line 424 of file cmdline_parser.cpp.

virtual bool process ( int &  argc,
const char *const *&  argv 
)
pure virtual

process one item from command line for this argument

bool process ( int &  argc,
const char *const *&  argv 
)
inlinefinal

"process" argument: just set to true, no argument is used.

Definition at line 113 of file cmdline_parser.cpp.

bool process ( int &  argc,
const char *const *&  argv 
)
inlinefinal

parse signed integer using sscanf.

Definition at line 141 of file cmdline_parser.cpp.

bool process ( int &  argc,
const char *const *&  argv 
)
inlinefinal

parse unsigned integer using sscanf.

Definition at line 177 of file cmdline_parser.cpp.

bool process ( int &  argc,
const char *const *&  argv 
)
inlinefinal

parse size_t using sscanf.

Definition at line 213 of file cmdline_parser.cpp.

bool process ( int &  argc,
const char *const *&  argv 
)
inlinefinal

parse unsigned integer using sscanf.

Definition at line 249 of file cmdline_parser.cpp.

bool process ( int &  argc,
const char *const *&  argv 
)
inlinefinal

parse unsigned integer using sscanf.

Definition at line 282 of file cmdline_parser.cpp.

bool process ( int &  argc,
const char *const *&  argv 
)
inlinefinal

parse byte size using SI/IEC parser.

Definition at line 317 of file cmdline_parser.cpp.

bool process ( int &  argc,
const char *const *&  argv 
)
inlinefinal

parse byte size using SI/IEC parser.

Definition at line 352 of file cmdline_parser.cpp.

bool process ( int &  argc,
const char *const *&  argv 
)
inlinefinal

"process" string argument just by storing it.

Definition at line 384 of file cmdline_parser.cpp.

bool process ( int &  argc,
const char *const *&  argv 
)
inlinefinal

"process" string argument just by storing it in vector.

Definition at line 416 of file cmdline_parser.cpp.

bool process ( int  argc,
const char *const *  argv,
std::ostream &  os 
)

parse command line options as specified by the options and parameters added.

Returns
true if command line is okay and all required parameters are present.

Definition at line 968 of file cmdline_parser.cpp.

bool process ( int  argc,
const char *const *  argv 
)

parse command line options as specified by the options and parameters added.

Returns
true if command line is okay and all required parameters are present.

Definition at line 1110 of file cmdline_parser.cpp.

void set_author ( const std::string &  author)

Set author of program, will be wrapped.

Definition at line 506 of file cmdline_parser.cpp.

void set_description ( const std::string &  description)

Set description of program, text will be wrapped.

Definition at line 502 of file cmdline_parser.cpp.

void set_verbose_process ( bool  verbose_process)

Set verbose processing of command line arguments.

Definition at line 510 of file cmdline_parser.cpp.

CmdlineParser & sort ( )

sort options by key (but not the positional parameters)

Definition at line 872 of file cmdline_parser.cpp.

virtual const char* type_name ( ) const
pure virtual

return formatted type name to user

const char* type_name ( ) const
inlinefinal

Definition at line 110 of file cmdline_parser.cpp.

const char* type_name ( ) const
inlinefinal

Definition at line 138 of file cmdline_parser.cpp.

const char* type_name ( ) const
inlinefinal

Definition at line 174 of file cmdline_parser.cpp.

const char* type_name ( ) const
inlinefinal

Definition at line 210 of file cmdline_parser.cpp.

const char* type_name ( ) const
inlinefinal

Definition at line 246 of file cmdline_parser.cpp.

const char* type_name ( ) const
inlinefinal

Definition at line 279 of file cmdline_parser.cpp.

const char* type_name ( ) const
inlinefinal

Definition at line 314 of file cmdline_parser.cpp.

const char* type_name ( ) const
inlinefinal

Definition at line 349 of file cmdline_parser.cpp.

const char* type_name ( ) const
inlinefinal

Definition at line 381 of file cmdline_parser.cpp.

const char* type_name ( ) const
inlinefinal

Definition at line 413 of file cmdline_parser.cpp.

Member Data Documentation

std::string author_
private

user set author of program, will be wrapped

Definition at line 116 of file cmdline_parser.hpp.

std::string desc_

longer description, which will be wrapped

Definition at line 42 of file cmdline_parser.cpp.

std::string description_
private

user set description of program, will be wrapped

Definition at line 114 of file cmdline_parser.hpp.

bool& dest_
protected

reference to boolean to set to true

Definition at line 101 of file cmdline_parser.cpp.

int& dest_
protected

Definition at line 129 of file cmdline_parser.cpp.

unsigned int& dest_
protected

Definition at line 165 of file cmdline_parser.cpp.

size_t& dest_
protected

Definition at line 201 of file cmdline_parser.cpp.

float& dest_
protected

Definition at line 237 of file cmdline_parser.cpp.

double& dest_
protected

Definition at line 270 of file cmdline_parser.cpp.

uint32_t& dest_
protected

Definition at line 305 of file cmdline_parser.cpp.

uint64_t& dest_
protected

Definition at line 340 of file cmdline_parser.cpp.

std::string& dest_
protected

Definition at line 372 of file cmdline_parser.cpp.

std::vector<std::string>& dest_
protected

Definition at line 402 of file cmdline_parser.cpp.

bool found_

found during processing of command line

Definition at line 46 of file cmdline_parser.cpp.

char key_

single letter short option, or 0 is none

Definition at line 36 of file cmdline_parser.cpp.

std::string keytype_

option type description, e.g. "<#>" to indicate numbers

Definition at line 40 of file cmdline_parser.cpp.

unsigned int line_wrap_
private

set line wrap length

Definition at line 119 of file cmdline_parser.hpp.

std::string longkey_

long option key or name for parameters

Definition at line 38 of file cmdline_parser.cpp.

constexpr int max_type_name_
staticprivate

maximum length of a type_name() result

Definition at line 122 of file cmdline_parser.hpp.

ArgumentList option_list_
private

list of options available

Definition at line 98 of file cmdline_parser.hpp.

size_t option_max_width_
private

formatting width for options, '-s, –switch <#>'

Definition at line 103 of file cmdline_parser.hpp.

ArgumentList param_list_
private

list of parameters, both required and optional

Definition at line 100 of file cmdline_parser.hpp.

size_t param_max_width_
private

formatting width for parameters, 'param <#>'

Definition at line 105 of file cmdline_parser.hpp.

const char* program_name_
private

argv[0] for usage.

Definition at line 108 of file cmdline_parser.hpp.

bool repeated_

repeated argument, i.e. std::vector<std::string>

Definition at line 48 of file cmdline_parser.cpp.

bool required_

required, process() fails if the option/parameter is not found.

Definition at line 44 of file cmdline_parser.cpp.

bool verbose_process_
private

verbose processing of arguments

Definition at line 111 of file cmdline_parser.hpp.


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