STXXL
1.4.0
|
Command line parser which automatically fills variables and prints nice usage messages.
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.
Maybe most important it will nicely format the options and parameters description using word wrapping.
Classes | |
struct | argument |
base class of all options and parameters More... | |
struct | argument_bytes |
specialization of argument for SI/IEC suffixes byte size options or parameters More... | |
struct | argument_flag |
specialization of argument for boolean flags (can only be set to true). More... | |
struct | argument_int |
specialization of argument for integer options or parameters More... | |
struct | argument_string |
specialization of argument for string options or parameters More... | |
struct | argument_stringlist |
specialization of argument for multiple string options or parameters More... | |
struct | argument_uint |
specialization of argument for unsigned integer options or parameters More... | |
Public Member Functions | |
cmdline_parser () | |
Construct new command line parser. More... | |
~cmdline_parser () | |
Delete all added arguments. More... | |
void | add_bytes (char key, const std::string &longkey, const std::string &keytype, const std::string &desc, stxxl::uint64 &dest) |
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 &desc, stxxl::uint64 &dest) |
add SI/IEC suffixes byte size option -key, –longkey [keytype] and store to 64-bit dest More... | |
void | add_flag (char key, const std::string &longkey, const std::string &keytype, const std::string &desc, bool &dest) |
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 &desc, bool &dest) |
add boolean option flag -key, –longkey with description and store to dest More... | |
void | add_int (char key, const std::string &longkey, const std::string &keytype, const std::string &desc, int &dest) |
add signed integer option -key, –longkey [keytype] with description and store to dest More... | |
void | add_int (char key, const std::string &longkey, const std::string &desc, int &dest) |
add signed integer option -key, –longkey with description and store to dest More... | |
void | add_opt_param_bytes (const std::string &name, const std::string &desc, uint64 &dest) |
add optional SI/IEC suffixes byte size parameter [name] with description and store to dest More... | |
void | add_opt_param_int (const std::string &name, const std::string &desc, int &dest) |
add optional signed integer parameter [name] with description and store to dest More... | |
void | add_opt_param_string (const std::string &name, const std::string &desc, std::string &dest) |
add optional string parameter [name] with description and store to dest More... | |
void | add_opt_param_stringlist (const std::string &name, const std::string &desc, std::vector< std::string > &dest) |
add optional string parameter [name] with description and store to dest More... | |
void | add_opt_param_uint (const std::string &name, const std::string &desc, unsigned int &dest) |
add optional unsigned integer parameter [name] with description and store to dest More... | |
void | add_param_bytes (const std::string &name, const std::string &desc, uint64 &dest) |
add SI/IEC suffixes byte size parameter [name] with description and store to dest More... | |
void | add_param_int (const std::string &name, const std::string &desc, int &dest) |
add signed integer parameter [name] with description and store to dest More... | |
void | add_param_string (const std::string &name, const std::string &desc, std::string &dest) |
add string parameter [name] with description and store to dest More... | |
void | add_param_stringlist (const std::string &name, const std::string &desc, std::vector< std::string > &dest) |
add string list parameter [name] with description and store to dest. More... | |
void | add_param_uint (const std::string &name, const std::string &desc, unsigned int &dest) |
add unsigned integer parameter [name] with description and store to dest More... | |
void | add_string (char key, const std::string &longkey, const std::string &keytype, const std::string &desc, std::string &dest) |
add string option -key, –longkey [keytype] and store to dest More... | |
void | add_string (char key, const std::string &longkey, const std::string &desc, std::string &dest) |
add string option -key, –longkey [keytype] and store to dest More... | |
void | add_stringlist (char key, const std::string &longkey, const std::string &keytype, const std::string &desc, std::vector< std::string > &dest) |
add string list option -key, –longkey [keytype] and store to dest More... | |
void | add_stringlist (char key, const std::string &longkey, const std::string &desc, std::vector< std::string > &dest) |
add string list option -key, –longkey [keytype] and store to dest More... | |
void | add_uint (char key, const std::string &longkey, const std::string &keytype, const std::string &desc, unsigned int &dest) |
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 &desc, unsigned int &dest) |
add unsigned integer option -key, –longkey [keytype] with description and store to dest More... | |
void | print_result (std::ostream &os=std::cout) |
print nicely formatted result of processing More... | |
void | print_usage (std::ostream &os=std::cout) |
output nicely formatted usage information including description of all parameters and options. More... | |
bool | process (int argc, const char *const *argv, std::ostream &os=std::cout) |
parse command line options as specified by the options and parameters added. More... | |
void | set_author (const std::string &author) |
Set author of program, will be wrapped. More... | |
void | set_description (const std::string &description) |
Set description of program, text will be wrapped. More... | |
void | set_verbose_process (bool verbose_process) |
Set verbose processing of command line arguments. 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. Prefix is added unconditionally to each line. Lines are wrapped after wraplen characters if possible. More... | |
Protected Types | |
typedef std::vector< argument * > | arglist_type |
option and parameter list type More... | |
Protected Attributes | |
std::string | m_author |
user set author of program, will be wrapped More... | |
std::string | m_description |
user set description of program, will be wrapped More... | |
unsigned int | m_linewrap |
set line wrap length More... | |
size_t | m_opt_maxlong |
formatting width for options, '-s, –switch <#>' More... | |
arglist_type | m_optlist |
list of options available More... | |
size_t | m_param_maxlong |
formatting width for parameters, 'param <#>' More... | |
arglist_type | m_paramlist |
list of parameters, both required and optional More... | |
const char * | m_progname |
argv[0] for usage. More... | |
bool | m_verbose_process |
verbose processing of arguments More... | |
Static Protected Attributes | |
static const int | m_maxtypename = 16 |
maximum length of a type_name() result More... | |
Private Member Functions | |
void | calc_opt_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 Member Functions inherited from stxxl::noncopyable | |
noncopyable () | |
|
protected |
|
inline |
|
inline |
|
inline |
add SI/IEC suffixes byte size option -key, –longkey [keytype] and store to 64-bit dest
|
inline |
|
inline |
add boolean option flag -key, –longkey [keytype] with description and store to dest
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
add string parameter [name] with description and store to dest
|
inline |
|
inline |
|
inline |
add string option -key, –longkey [keytype] and store to dest
|
inline |
|
inline |
|
inline |
|
inline |
add unsigned integer option -key, –longkey [keytype] with description and store to dest
|
inline |
|
inlineprivate |
update maximum formatting width for new option
Definition at line 329 of file cmdline.h.
References stxxl::cmdline_parser::argument::option_text(), and stxxl::STXXL_MAX().
|
inlineprivate |
update maximum formatting width for new parameter
Definition at line 335 of file cmdline.h.
References stxxl::cmdline_parser::argument::param_text(), and stxxl::STXXL_MAX().
|
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 22 of file cmdline.cpp.
|
private |
print error about option.
Definition at line 129 of file cmdline.cpp.
References stxxl::cmdline_parser::argument::option_text(), and stxxl::cmdline_parser::argument::type_name().
|
private |
print error about parameter.
Definition at line 143 of file cmdline.cpp.
References stxxl::cmdline_parser::argument::param_text(), and stxxl::cmdline_parser::argument::type_name().
void stxxl::cmdline_parser::print_result | ( | std::ostream & | os = std::cout | ) |
print nicely formatted result of processing
Definition at line 303 of file cmdline.cpp.
References stxxl::cmdline_parser::argument::option_text(), stxxl::cmdline_parser::argument::param_text(), stxxl::cmdline_parser::argument::print_value(), stxxl::STXXL_MAX(), and stxxl::cmdline_parser::argument::type_name().
void stxxl::cmdline_parser::print_usage | ( | std::ostream & | os = std::cout | ) |
output nicely formatted usage information including description of all parameters and options.
Definition at line 62 of file cmdline.cpp.
References stxxl::cmdline_parser::argument::m_desc, stxxl::cmdline_parser::argument::m_longkey, stxxl::cmdline_parser::argument::m_repeated, stxxl::cmdline_parser::argument::m_required, stxxl::cmdline_parser::argument::option_text(), and stxxl::cmdline_parser::argument::param_text().
bool stxxl::cmdline_parser::process | ( | int | argc, |
const char *const * | argv, | ||
std::ostream & | os = std::cout |
||
) |
parse command line options as specified by the options and parameters added.
Definition at line 157 of file cmdline.cpp.
|
inline |
Set author of program, will be wrapped.
|
inline |
Set description of program, text will be wrapped.
|
inline |
|
protected |
|
protected |
|
protected |
|
staticprotected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |