tlx
Loading...
Searching...
No Matches
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
 
virtual ~Argument ()=default
 empty virtual destructor
 
virtual const char * type_name () const =0
 return formatted type name to user
 
virtual bool process (int &argc, const char *const *&argv)=0
 process one item from command line for this argument
 
virtual void print_value (std::ostream &os) const =0
 format value to ostream
 
std::string param_text () const
 return 'longkey [keytype]'
 
std::string option_text () const
 return '-s, –longkey [keytype]'
 
 ArgumentBool (char key, const std::string &longkey, const std::string &keytype, const std::string &desc, bool required, bool &dest)
 contructor filling most attributes
 
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.
 
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
 
const char * type_name () const final
 
bool process (int &argc, const char *const *&argv) final
 parse signed integer using sscanf.
 
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
 
const char * type_name () const final
 
bool process (int &argc, const char *const *&argv) final
 parse unsigned integer using sscanf.
 
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
 
const char * type_name () const final
 
bool process (int &argc, const char *const *&argv) final
 parse size_t using sscanf.
 
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
 
const char * type_name () const final
 
bool process (int &argc, const char *const *&argv) final
 parse unsigned integer using sscanf.
 
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
 
const char * type_name () const final
 
bool process (int &argc, const char *const *&argv) final
 parse unsigned integer using sscanf.
 
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, std::uint32_t &dest)
 contructor filling most attributes
 
const char * type_name () const final
 
bool process (int &argc, const char *const *&argv) final
 parse byte size using SI/IEC parser.
 
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, std::uint64_t &dest)
 contructor filling most attributes
 
const char * type_name () const final
 
bool process (int &argc, const char *const *&argv) final
 parse byte size using SI/IEC parser.
 
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
 
const char * type_name () const final
 
bool process (int &argc, const char *const *&argv) final
 "process" string argument just by storing it.
 
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
 
const char * type_name () const final
 
bool process (int &argc, const char *const *&argv) final
 "process" string argument just by storing it in vector.
 
void print_value (std::ostream &os) const final
 
 CmdlineParser ()
 Constructor.
 
 ~CmdlineParser ()
 Delete all added arguments.
 
void set_description (const std::string &description)
 Set description of program, text will be wrapped.
 
void set_author (const std::string &author)
 Set author of program, will be wrapped.
 
void set_verbose_process (bool verbose_process)
 Set verbose processing of command line arguments.
 
void print_usage (std::ostream &os)
 output nicely formatted usage information including description of all parameters and options.
 
void print_usage ()
 output to std::cout nicely formatted usage information including description of all parameters and options.
 
CmdlineParsersort ()
 sort options by key (but not the positional parameters)
 
bool process (int argc, const char *const *argv, std::ostream &os)
 parse command line options as specified by the options and parameters added.
 
bool process (int argc, const char *const *argv)
 parse command line options as specified by the options and parameters added.
 
void print_result (std::ostream &os)
 print nicely formatted result of processing
 
void print_result ()
 print nicely formatted result of processing to std::cout
 
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
 
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.
 
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
 
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
 
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.
 
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
 
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
 
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
 
void add_bytes (char key, const std::string &longkey, std::uint32_t &dest, const std::string &desc)
 add SI/IEC suffixes byte size option -key, –longkey and store to 32-bit dest
 
void add_bytes (char key, const std::string &longkey, std::uint64_t &dest, const std::string &desc)
 add SI/IEC suffixes byte size option -key, –longkey and store to 64-bit dest
 
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
 
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
 
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
 
void add_flag (const std::string &longkey, bool &dest, const std::string &desc)
 add boolean option flag –longkey with description and store to dest.
 
void add_int (const std::string &longkey, int &dest, const std::string &desc)
 add signed integer option –longkey with description and store to dest
 
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
 
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.
 
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
 
void add_float (const std::string &longkey, float &dest, const std::string &desc)
 add float option –longkey with description and store to dest
 
void add_double (const std::string &longkey, double &dest, const std::string &desc)
 add double option –longkey with description and store to dest
 
void add_bytes (const std::string &longkey, std::uint32_t &dest, const std::string &desc)
 add SI/IEC suffixes byte size option –longkey and store to 32-bit dest
 
void add_bytes (const std::string &longkey, std::uint64_t &dest, const std::string &desc)
 add SI/IEC suffixes byte size option –longkey and store to 64-bit dest
 
void add_string (const std::string &longkey, std::string &dest, const std::string &desc)
 add string option –longkey and store to dest
 
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
 
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
 
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.
 
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
 
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
 
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.
 
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
 
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
 
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
 
void add_bytes (char key, const std::string &longkey, const std::string &keytype, std::uint32_t &dest, const std::string &desc)
 add SI/IEC suffixes byte size option -key, –longkey [keytype] and store to 64-bit dest
 
void add_bytes (char key, const std::string &longkey, const std::string &keytype, std::uint64_t &dest, const std::string &desc)
 add SI/IEC suffixes byte size option -key, –longkey [keytype] and store to 64-bit dest
 
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
 
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
 
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
 
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
 
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.
 
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
 
void add_param_float (const std::string &name, float &dest, const std::string &desc)
 add float parameter [name] with description and store to dest
 
void add_param_double (const std::string &name, double &dest, const std::string &desc)
 add double parameter [name] with description and store to dest
 
void add_param_bytes (const std::string &name, std::uint32_t &dest, const std::string &desc)
 add SI/IEC suffixes byte size parameter [name] with description and store to dest
 
void add_param_bytes (const std::string &name, std::uint64_t &dest, const std::string &desc)
 add SI/IEC suffixes byte size parameter [name] with description and store to dest
 
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
 
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.
 
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
 
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
 
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.
 
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
 
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
 
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
 
void add_opt_param_bytes (const std::string &name, std::uint32_t &dest, const std::string &desc)
 add optional SI/IEC suffixes byte size parameter [name] with description and store to dest
 
void add_opt_param_bytes (const std::string &name, std::uint64_t &dest, const std::string &desc)
 add optional SI/IEC suffixes byte size parameter [name] with description and store to dest
 
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
 
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
 

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.
 

Public Attributes

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

Protected Attributes

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

Private Types

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

Private Member Functions

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

Private Attributes

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

Static Private Attributes

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

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 <cstdint>
#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'
std::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;
}
base class of all options and parameters
void set_author(const std::string &author)
Set author of program, will be wrapped.
virtual bool process(int &argc, const char *const *&argv)=0
process one item from command line for this argument
void add_bytes(char key, const std::string &longkey, std::uint32_t &dest, const std::string &desc)
add SI/IEC suffixes byte size option -key, –longkey and store to 32-bit dest
void print_result(std::ostream &os)
print nicely formatted result of processing
void set_description(const std::string &description)
Set description of program, text will be wrapped.
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
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

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 33 of file cmdline_parser.cpp.

Member Typedef Documentation

◆ ArgumentList

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

option and parameter list type

Definition at line 96 of file cmdline_parser.hpp.

Constructor & Destructor Documentation

◆ ~Argument()

virtual ~Argument ( )
virtualdefault

empty virtual destructor

◆ CmdlineParser()

Constructor.

Definition at line 491 of file cmdline_parser.cpp.

◆ ~CmdlineParser()

Delete all added arguments.

Definition at line 493 of file cmdline_parser.cpp.

Member Function Documentation

◆ add_bool() [1/3]

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 613 of file cmdline_parser.cpp.

◆ add_bool() [2/3]

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 517 of file cmdline_parser.cpp.

◆ add_bool() [3/3]

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 676 of file cmdline_parser.cpp.

◆ add_bytes() [1/6]

void add_bytes ( char  key,
const std::string &  longkey,
const std::string &  keytype,
std::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 577 of file cmdline_parser.cpp.

◆ add_bytes() [2/6]

void add_bytes ( char  key,
const std::string &  longkey,
const std::string &  keytype,
std::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 585 of file cmdline_parser.cpp.

◆ add_bytes() [3/6]

void add_bytes ( char  key,
const std::string &  longkey,
std::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 653 of file cmdline_parser.cpp.

◆ add_bytes() [4/6]

void add_bytes ( char  key,
const std::string &  longkey,
std::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 658 of file cmdline_parser.cpp.

◆ add_bytes() [5/6]

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

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

Definition at line 716 of file cmdline_parser.cpp.

◆ add_bytes() [6/6]

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

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

Definition at line 721 of file cmdline_parser.cpp.

◆ add_double() [1/3]

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 569 of file cmdline_parser.cpp.

◆ add_double() [2/3]

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 648 of file cmdline_parser.cpp.

◆ add_double() [3/3]

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 711 of file cmdline_parser.cpp.

◆ add_flag() [1/3]

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 618 of file cmdline_parser.cpp.

◆ add_flag() [2/3]

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 525 of file cmdline_parser.cpp.

◆ add_flag() [3/3]

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 681 of file cmdline_parser.cpp.

◆ add_float() [1/3]

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 561 of file cmdline_parser.cpp.

◆ add_float() [2/3]

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 643 of file cmdline_parser.cpp.

◆ add_float() [3/3]

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 706 of file cmdline_parser.cpp.

◆ add_int() [1/3]

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 531 of file cmdline_parser.cpp.

◆ add_int() [2/3]

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 623 of file cmdline_parser.cpp.

◆ add_int() [3/3]

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 686 of file cmdline_parser.cpp.

◆ add_opt_param_bytes() [1/2]

void add_opt_param_bytes ( const std::string &  name,
std::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 842 of file cmdline_parser.cpp.

◆ add_opt_param_bytes() [2/2]

void add_opt_param_bytes ( const std::string &  name,
std::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 849 of file cmdline_parser.cpp.

◆ add_opt_param_double()

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 835 of file cmdline_parser.cpp.

◆ add_opt_param_float()

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 829 of file cmdline_parser.cpp.

◆ add_opt_param_int()

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 805 of file cmdline_parser.cpp.

◆ add_opt_param_size_t()

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 823 of file cmdline_parser.cpp.

◆ add_opt_param_string()

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 856 of file cmdline_parser.cpp.

◆ add_opt_param_stringlist()

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 863 of file cmdline_parser.cpp.

◆ add_opt_param_uint()

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 818 of file cmdline_parser.cpp.

◆ add_opt_param_unsigned()

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 811 of file cmdline_parser.cpp.

◆ add_param_bytes() [1/2]

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

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

Definition at line 775 of file cmdline_parser.cpp.

◆ add_param_bytes() [2/2]

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

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

Definition at line 782 of file cmdline_parser.cpp.

◆ add_param_double()

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 769 of file cmdline_parser.cpp.

◆ add_param_float()

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 763 of file cmdline_parser.cpp.

◆ add_param_int()

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 739 of file cmdline_parser.cpp.

◆ add_param_size_t()

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 757 of file cmdline_parser.cpp.

◆ add_param_string()

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 789 of file cmdline_parser.cpp.

◆ add_param_stringlist()

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 795 of file cmdline_parser.cpp.

◆ add_param_uint()

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 752 of file cmdline_parser.cpp.

◆ add_param_unsigned()

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 745 of file cmdline_parser.cpp.

◆ add_size_t() [1/3]

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 553 of file cmdline_parser.cpp.

◆ add_size_t() [2/3]

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 638 of file cmdline_parser.cpp.

◆ add_size_t() [3/3]

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 701 of file cmdline_parser.cpp.

◆ add_string() [1/3]

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 593 of file cmdline_parser.cpp.

◆ add_string() [2/3]

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 663 of file cmdline_parser.cpp.

◆ add_string() [3/3]

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 726 of file cmdline_parser.cpp.

◆ add_stringlist() [1/3]

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 601 of file cmdline_parser.cpp.

◆ add_stringlist() [2/3]

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 668 of file cmdline_parser.cpp.

◆ add_stringlist() [3/3]

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 731 of file cmdline_parser.cpp.

◆ add_uint() [1/3]

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 547 of file cmdline_parser.cpp.

◆ add_uint() [2/3]

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 633 of file cmdline_parser.cpp.

◆ add_uint() [3/3]

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 696 of file cmdline_parser.cpp.

◆ add_unsigned() [1/3]

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 539 of file cmdline_parser.cpp.

◆ add_unsigned() [2/3]

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 628 of file cmdline_parser.cpp.

◆ add_unsigned() [3/3]

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 691 of file cmdline_parser.cpp.

◆ Argument()

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 53 of file cmdline_parser.cpp.

◆ ArgumentBool()

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 106 of file cmdline_parser.cpp.

◆ ArgumentBytes32()

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

contructor filling most attributes

Definition at line 310 of file cmdline_parser.cpp.

◆ ArgumentBytes64()

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

contructor filling most attributes

Definition at line 345 of file cmdline_parser.cpp.

◆ ArgumentDouble()

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 275 of file cmdline_parser.cpp.

◆ ArgumentFloat()

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 242 of file cmdline_parser.cpp.

◆ ArgumentInt()

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 134 of file cmdline_parser.cpp.

◆ ArgumentSizeT()

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 206 of file cmdline_parser.cpp.

◆ ArgumentString()

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 377 of file cmdline_parser.cpp.

◆ ArgumentStringlist()

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 407 of file cmdline_parser.cpp.

◆ ArgumentUnsigned()

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 170 of file cmdline_parser.cpp.

◆ calc_option_max()

void calc_option_max ( const Argument arg)
private

update maximum formatting width for new option

Definition at line 438 of file cmdline_parser.cpp.

◆ calc_param_max()

void calc_param_max ( const Argument arg)
private

update maximum formatting width for new parameter

Definition at line 443 of file cmdline_parser.cpp.

◆ option_text()

std::string option_text ( ) const
inline

return '-s, –longkey [keytype]'

Definition at line 80 of file cmdline_parser.cpp.

◆ output_wrap()

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 450 of file cmdline_parser.cpp.

◆ param_text()

std::string param_text ( ) const
inline

return 'longkey [keytype]'

Definition at line 71 of file cmdline_parser.cpp.

◆ print_option_error()

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

print error about option.

Definition at line 943 of file cmdline_parser.cpp.

◆ print_param_error()

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

print error about parameter.

Definition at line 956 of file cmdline_parser.cpp.

◆ print_result() [1/2]

void print_result ( )

print nicely formatted result of processing to std::cout

Definition at line 1161 of file cmdline_parser.cpp.

◆ print_result() [2/2]

void print_result ( std::ostream &  os)

print nicely formatted result of processing

Definition at line 1115 of file cmdline_parser.cpp.

◆ print_usage() [1/2]

void print_usage ( )

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

Definition at line 939 of file cmdline_parser.cpp.

◆ print_usage() [2/2]

void print_usage ( std::ostream &  os)

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

Definition at line 881 of file cmdline_parser.cpp.

◆ print_value() [1/11]

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

format value to ostream

◆ print_value() [2/11]

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

Definition at line 120 of file cmdline_parser.cpp.

◆ print_value() [3/11]

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

Definition at line 158 of file cmdline_parser.cpp.

◆ print_value() [4/11]

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

Definition at line 194 of file cmdline_parser.cpp.

◆ print_value() [5/11]

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

Definition at line 230 of file cmdline_parser.cpp.

◆ print_value() [6/11]

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

Definition at line 264 of file cmdline_parser.cpp.

◆ print_value() [7/11]

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

Definition at line 297 of file cmdline_parser.cpp.

◆ print_value() [8/11]

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

Definition at line 333 of file cmdline_parser.cpp.

◆ print_value() [9/11]

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

Definition at line 365 of file cmdline_parser.cpp.

◆ print_value() [10/11]

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

Definition at line 393 of file cmdline_parser.cpp.

◆ print_value() [11/11]

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

Definition at line 425 of file cmdline_parser.cpp.

◆ process() [1/13]

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

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

Definition at line 114 of file cmdline_parser.cpp.

◆ process() [2/13]

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

parse signed integer using sscanf.

Definition at line 142 of file cmdline_parser.cpp.

◆ process() [3/13]

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

parse unsigned integer using sscanf.

Definition at line 178 of file cmdline_parser.cpp.

◆ process() [4/13]

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

parse size_t using sscanf.

Definition at line 214 of file cmdline_parser.cpp.

◆ process() [5/13]

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

parse unsigned integer using sscanf.

Definition at line 250 of file cmdline_parser.cpp.

◆ process() [6/13]

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

parse unsigned integer using sscanf.

Definition at line 283 of file cmdline_parser.cpp.

◆ process() [7/13]

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

parse byte size using SI/IEC parser.

Definition at line 318 of file cmdline_parser.cpp.

◆ process() [8/13]

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

parse byte size using SI/IEC parser.

Definition at line 353 of file cmdline_parser.cpp.

◆ process() [9/13]

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

"process" string argument just by storing it.

Definition at line 385 of file cmdline_parser.cpp.

◆ process() [10/13]

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

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

Definition at line 417 of file cmdline_parser.cpp.

◆ process() [11/13]

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

process one item from command line for this argument

◆ process() [12/13]

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 1111 of file cmdline_parser.cpp.

◆ process() [13/13]

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 969 of file cmdline_parser.cpp.

◆ set_author()

void set_author ( const std::string &  author)

Set author of program, will be wrapped.

Definition at line 507 of file cmdline_parser.cpp.

◆ set_description()

void set_description ( const std::string &  description)

Set description of program, text will be wrapped.

Definition at line 503 of file cmdline_parser.cpp.

◆ set_verbose_process()

void set_verbose_process ( bool  verbose_process)

Set verbose processing of command line arguments.

Definition at line 511 of file cmdline_parser.cpp.

◆ sort()

CmdlineParser & sort ( )

sort options by key (but not the positional parameters)

Definition at line 873 of file cmdline_parser.cpp.

◆ type_name() [1/11]

virtual const char * type_name ( ) const
pure virtual

return formatted type name to user

◆ type_name() [2/11]

const char * type_name ( ) const
inlinefinal

Definition at line 111 of file cmdline_parser.cpp.

◆ type_name() [3/11]

const char * type_name ( ) const
inlinefinal

Definition at line 139 of file cmdline_parser.cpp.

◆ type_name() [4/11]

const char * type_name ( ) const
inlinefinal

Definition at line 175 of file cmdline_parser.cpp.

◆ type_name() [5/11]

const char * type_name ( ) const
inlinefinal

Definition at line 211 of file cmdline_parser.cpp.

◆ type_name() [6/11]

const char * type_name ( ) const
inlinefinal

Definition at line 247 of file cmdline_parser.cpp.

◆ type_name() [7/11]

const char * type_name ( ) const
inlinefinal

Definition at line 280 of file cmdline_parser.cpp.

◆ type_name() [8/11]

const char * type_name ( ) const
inlinefinal

Definition at line 315 of file cmdline_parser.cpp.

◆ type_name() [9/11]

const char * type_name ( ) const
inlinefinal

Definition at line 350 of file cmdline_parser.cpp.

◆ type_name() [10/11]

const char * type_name ( ) const
inlinefinal

Definition at line 382 of file cmdline_parser.cpp.

◆ type_name() [11/11]

const char * type_name ( ) const
inlinefinal

Definition at line 414 of file cmdline_parser.cpp.

Member Data Documentation

◆ author_

std::string author_
private

user set author of program, will be wrapped

Definition at line 117 of file cmdline_parser.hpp.

◆ desc_

std::string desc_

longer description, which will be wrapped

Definition at line 43 of file cmdline_parser.cpp.

◆ description_

std::string description_
private

user set description of program, will be wrapped

Definition at line 115 of file cmdline_parser.hpp.

◆ dest_ [1/10]

bool& dest_
protected

reference to boolean to set to true

Definition at line 102 of file cmdline_parser.cpp.

◆ dest_ [2/10]

int& dest_
protected

Definition at line 130 of file cmdline_parser.cpp.

◆ dest_ [3/10]

unsigned int& dest_
protected

Definition at line 166 of file cmdline_parser.cpp.

◆ dest_ [4/10]

size_t& dest_
protected

Definition at line 202 of file cmdline_parser.cpp.

◆ dest_ [5/10]

float& dest_
protected

Definition at line 238 of file cmdline_parser.cpp.

◆ dest_ [6/10]

double& dest_
protected

Definition at line 271 of file cmdline_parser.cpp.

◆ dest_ [7/10]

std::uint32_t& dest_
protected

Definition at line 306 of file cmdline_parser.cpp.

◆ dest_ [8/10]

std::uint64_t& dest_
protected

Definition at line 341 of file cmdline_parser.cpp.

◆ dest_ [9/10]

std::string& dest_
protected

Definition at line 373 of file cmdline_parser.cpp.

◆ dest_ [10/10]

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

Definition at line 403 of file cmdline_parser.cpp.

◆ found_

bool found_

found during processing of command line

Definition at line 47 of file cmdline_parser.cpp.

◆ key_

char key_

single letter short option, or 0 is none

Definition at line 37 of file cmdline_parser.cpp.

◆ keytype_

std::string keytype_

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

Definition at line 41 of file cmdline_parser.cpp.

◆ line_wrap_

unsigned int line_wrap_
private

set line wrap length

Definition at line 120 of file cmdline_parser.hpp.

◆ longkey_

std::string longkey_

long option key or name for parameters

Definition at line 39 of file cmdline_parser.cpp.

◆ max_type_name_

constexpr int max_type_name_
staticconstexprprivate

maximum length of a type_name() result

Definition at line 123 of file cmdline_parser.hpp.

◆ option_list_

ArgumentList option_list_
private

list of options available

Definition at line 99 of file cmdline_parser.hpp.

◆ option_max_width_

size_t option_max_width_
private

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

Definition at line 104 of file cmdline_parser.hpp.

◆ param_list_

ArgumentList param_list_
private

list of parameters, both required and optional

Definition at line 101 of file cmdline_parser.hpp.

◆ param_max_width_

size_t param_max_width_
private

formatting width for parameters, 'param <#>'

Definition at line 106 of file cmdline_parser.hpp.

◆ program_name_

const char* program_name_
private

argv[0] for usage.

Definition at line 109 of file cmdline_parser.hpp.

◆ repeated_

bool repeated_

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

Definition at line 49 of file cmdline_parser.cpp.

◆ required_

bool required_

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

Definition at line 45 of file cmdline_parser.cpp.

◆ verbose_process_

bool verbose_process_
private

verbose processing of arguments

Definition at line 112 of file cmdline_parser.hpp.


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