13 #ifndef STXXL_COMMON_CMDLINE_HEADER
14 #define STXXL_COMMON_CMDLINE_HEADER
67 argument(
char key,
const std::string& longkey,
const std::string& keytype,
68 const std::string& desc,
bool required)
69 : m_key(key), m_longkey(longkey), m_keytype(keytype),
70 m_desc(desc), m_required(required), m_found(false), m_repeated(false)
77 virtual const char * type_name()
const = 0;
80 virtual bool process(
int& argc,
const char*
const*& argv) = 0;
83 virtual void print_value(std::ostream& os)
const = 0;
88 std::string s = m_longkey;
89 if (m_keytype.size()) {
106 if (m_keytype.size()) {
107 s +=
' ' + m_keytype;
120 argument_flag(
char key,
const std::string& longkey,
const std::string& keytype,
121 const std::string& desc,
bool required,
bool& dest)
122 :
argument(key, longkey, keytype, desc, required),
130 virtual bool process(
int&,
const char*
const*&)
137 { os << (m_dest ?
"true" :
"false"); }
146 argument_int(
char key,
const std::string& longkey,
const std::string& keytype,
147 const std::string& desc,
bool required,
int& dest)
148 :
argument(key, longkey, keytype, desc, required),
153 {
return "integer"; }
156 virtual bool process(
int& argc,
const char*
const*& argv)
158 if (argc == 0)
return false;
159 if (sscanf(argv[0],
"%d", &m_dest) == 1) {
177 argument_uint(
char key,
const std::string& longkey,
const std::string& keytype,
178 const std::string& desc,
bool required,
unsigned int& dest)
179 :
argument(key, longkey, keytype, desc, required),
184 {
return "unsigned integer"; }
187 virtual bool process(
int& argc,
const char*
const*& argv)
189 if (argc == 0)
return false;
190 if (sscanf(argv[0],
"%u", &m_dest) == 1) {
209 const std::string& desc,
bool required,
uint32& dest)
210 :
argument(key, longkey, keytype, desc, required),
218 virtual bool process(
int& argc,
const char*
const*& argv)
220 if (argc == 0)
return false;
242 const std::string& desc,
bool required,
uint64& dest)
243 :
argument(key, longkey, keytype, desc, required),
251 virtual bool process(
int& argc,
const char*
const*& argv)
253 if (argc == 0)
return false;
273 const std::string& desc,
bool required, std::string& dest)
274 :
argument(key, longkey, keytype, desc, required),
282 virtual bool process(
int& argc,
const char*
const*& argv)
284 if (argc == 0)
return false;
291 { os <<
'"' << m_dest <<
'"'; }
301 const std::string& desc,
bool required, std::vector<std::string>& dest)
302 :
argument(key, longkey, keytype, desc, required),
309 {
return "string list"; }
312 virtual bool process(
int& argc,
const char*
const*& argv)
314 if (argc == 0)
return false;
315 m_dest.push_back(argv[0]);
323 for (
size_t i = 0; i < m_dest.size(); ++i)
325 if (i != 0) os <<
',';
326 os <<
'"' << m_dest[i] <<
'"';
361 static const int m_maxtypename = 16;
383 output_wrap(std::ostream& os,
const std::string& text,
size_t wraplen,
384 size_t indent_first = 0,
size_t indent_rest = 0,
385 size_t current = 0,
size_t indent_newline = 0);
393 m_verbose_process(true),
400 for (
size_t i = 0; i < m_optlist.size(); ++i)
404 for (
size_t i = 0; i < m_paramlist.size(); ++i)
405 delete m_paramlist[i];
412 m_description = description;
424 m_verbose_process = verbose_process;
430 void add_flag(
char key,
const std::string& longkey,
const std::string& keytype,
const std::string& desc,
bool& dest)
435 calc_opt_max(m_optlist.back());
439 void add_int(
char key,
const std::string& longkey,
const std::string& keytype,
const std::string& desc,
int& dest)
442 new argument_int(key, longkey, keytype, desc,
false, dest)
444 calc_opt_max(m_optlist.back());
448 void add_uint(
char key,
const std::string& longkey,
const std::string& keytype,
const std::string& desc,
unsigned int& dest)
453 calc_opt_max(m_optlist.back());
457 void add_bytes(
char key,
const std::string& longkey,
const std::string& keytype,
const std::string& desc,
stxxl::uint32& dest)
462 calc_opt_max(m_optlist.back());
466 void add_bytes(
char key,
const std::string& longkey,
const std::string& keytype,
const std::string& desc,
stxxl::uint64& dest)
471 calc_opt_max(m_optlist.back());
475 void add_string(
char key,
const std::string& longkey,
const std::string& keytype,
const std::string& desc, std::string& dest)
480 calc_opt_max(m_optlist.back());
484 void add_stringlist(
char key,
const std::string& longkey,
const std::string& keytype,
const std::string& desc, std::vector<std::string>& dest)
489 calc_opt_max(m_optlist.back());
493 void add_flag(
char key,
const std::string& longkey,
const std::string& desc,
bool& dest)
494 {
return add_flag(key, longkey,
"", desc, dest); }
497 void add_int(
char key,
const std::string& longkey,
const std::string& desc,
int& dest)
498 {
return add_int(key, longkey,
"", desc, dest); }
501 void add_uint(
char key,
const std::string& longkey,
const std::string& desc,
unsigned int& dest)
502 {
return add_uint(key, longkey,
"", desc, dest); }
506 {
return add_bytes(key, longkey,
"", desc, dest); }
510 {
return add_bytes(key, longkey,
"", desc, dest); }
513 void add_string(
char key,
const std::string& longkey,
const std::string& desc, std::string& dest)
514 {
return add_string(key, longkey,
"", desc, dest); }
517 void add_stringlist(
char key,
const std::string& longkey,
const std::string& desc, std::vector<std::string>& dest)
518 {
return add_stringlist(key, longkey,
"", desc, dest); }
523 void add_param_int(
const std::string& name,
const std::string& desc,
int& dest)
525 m_paramlist.push_back(
528 calc_param_max(m_paramlist.back());
532 void add_param_uint(
const std::string& name,
const std::string& desc,
unsigned int& dest)
534 m_paramlist.push_back(
537 calc_param_max(m_paramlist.back());
543 m_paramlist.push_back(
546 calc_param_max(m_paramlist.back());
552 m_paramlist.push_back(
555 calc_param_max(m_paramlist.back());
561 m_paramlist.push_back(
564 calc_param_max(m_paramlist.back());
571 m_paramlist.push_back(
574 calc_param_max(m_paramlist.back());
582 m_paramlist.push_back(
585 calc_param_max(m_paramlist.back());
591 m_paramlist.push_back(
594 calc_param_max(m_paramlist.back());
600 m_paramlist.push_back(
603 calc_param_max(m_paramlist.back());
609 m_paramlist.push_back(
612 calc_param_max(m_paramlist.back());
618 m_paramlist.push_back(
621 calc_param_max(m_paramlist.back());
628 m_paramlist.push_back(
631 calc_param_max(m_paramlist.back());
638 void print_usage(std::ostream& os = std::cout);
642 void print_option_error(
int argc,
const char*
const* argv,
const argument* arg,
646 void print_param_error(
int argc,
const char*
const* argv,
const argument* arg,
652 bool process(
int argc,
const char*
const* argv, std::ostream& os = std::cout);
655 void print_result(std::ostream& os = std::cout);
662 #endif // !STXXL_COMMON_CMDLINE_HEADER
bool m_required
required, process() fails if the option/parameter is not found.
virtual const char * type_name() const
return formatted type name to user
std::string m_longkey
long option key or name for parameters
std::string m_description
user set description of program, will be wrapped
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
const char * m_progname
argv[0] for usage.
argument_flag(char key, const std::string &longkey, const std::string &keytype, const std::string &desc, bool required, bool &dest)
contructor filling most attributes
argument(char key, const std::string &longkey, const std::string &keytype, const std::string &desc, bool required)
contructor filling most attributes
virtual bool process(int &argc, const char *const *&argv)
parse byte size using SI/IEC parser from stxxl.
virtual const char * type_name() const
return formatted type name to user
unsigned long long int uint64
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 ...
std::vector< std::string > & m_dest
virtual void print_value(std::ostream &os) const
format value to ostream
virtual void print_value(std::ostream &os) const
format value to ostream
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 ...
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
void add_bytes(char key, const std::string &longkey, const std::string &keytype, const std::string &desc, stxxl::uint32 &dest)
add SI/IEC suffixes byte size option -key, –longkey [keytype] and store to 64-bit dest ...
argument_string(char key, const std::string &longkey, const std::string &keytype, const std::string &desc, bool required, std::string &dest)
contructor filling most attributes
void set_verbose_process(bool verbose_process)
Set verbose processing of command line arguments.
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
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
cmdline_parser()
Construct new command line parser.
arglist_type m_optlist
list of options available
virtual bool process(int &argc, const char *const *&argv)
parse signed integer using sscanf.
virtual void print_value(std::ostream &os) const
format value to ostream
void calc_param_max(const argument *arg)
update maximum formatting width for new parameter
specialization of argument for SI/IEC suffixes byte size options or parameters
virtual const char * type_name() const
return formatted type name to user
argument_bytes32(char key, const std::string &longkey, const std::string &keytype, const std::string &desc, bool required, uint32 &dest)
contructor filling most attributes
virtual ~argument()
empty virtual destructor
base class of all options and parameters
void set_author(const std::string &author)
Set author of program, will be wrapped.
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.
std::string m_keytype
option type description, e.g. "<#>" to indicate numbers
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
void set_description(const std::string &description)
Set description of program, text will be wrapped.
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 ...
unsigned int m_linewrap
set line wrap length
virtual bool process(int &argc, const char *const *&argv)
"process" string argument just by storing it in vector.
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
virtual void print_value(std::ostream &os) const
format value to ostream
specialization of argument for string options or parameters
virtual void print_value(std::ostream &os) const
format value to ostream
std::string option_text() const
return '-s, –longkey [keytype]'
virtual const char * type_name() const
return formatted type name to user
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 ...
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 ...
virtual bool process(int &argc, const char *const *&argv)
parse byte size using SI/IEC parser from stxxl.
std::vector< argument * > arglist_type
option and parameter list type
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 ...
argument_int(char key, const std::string &longkey, const std::string &keytype, const std::string &desc, bool required, int &dest)
contructor filling most attributes
bool m_found
found during processing of command line
const Type & STXXL_MAX(const Type &a, const Type &b)
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 ...
bool m_repeated
repeated argument, i.e. std::vector<std::string>
#define STXXL_BEGIN_NAMESPACE
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
std::string m_author
user set author of program, will be wrapped
virtual void print_value(std::ostream &os) const
format value to ostream
void add_opt_param_bytes(const std::string &name, const std::string &desc, uint32 &dest)
add optional SI/IEC suffixes byte size parameter [name] with description and store to dest ...
std::string param_text() const
return 'longkey [keytype]'
bool parse_SI_IEC_size(const std::string &str, uint64 &size, char def_unit=0)
Parse a string like "343KB" or "44 GiB" into the corresponding size in bytes. Returns the number of b...
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 ...
std::string m_desc
longer description, which will be wrapped
virtual const char * type_name() const
return formatted type name to user
virtual void print_value(std::ostream &os) const
format value to ostream
int m_opt_maxlong
formatting width for options, '-s, –switch <#>'
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 ...
virtual bool process(int &, const char *const *&)
"process" argument: just set to true, no argument is used.
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
virtual bool process(int &argc, const char *const *&argv)
"process" string argument just by storing it.
bool & m_dest
reference to boolean to set to true
char m_key
single letter short option, or 0 is none
specialization of argument for unsigned integer options or parameters
void add_bytes(char key, const std::string &longkey, const std::string &desc, stxxl::uint32 &dest)
add SI/IEC suffixes byte size option -key, –longkey [keytype] and store to 32-bit dest ...
specialization of argument for integer options or parameters
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
void add_param_bytes(const std::string &name, const std::string &desc, uint32 &dest)
add SI/IEC suffixes byte size parameter [name] with description and store to dest ...
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
argument_uint(char key, const std::string &longkey, const std::string &keytype, const std::string &desc, bool required, unsigned int &dest)
contructor filling most attributes
specialization of argument for multiple string options or parameters
arglist_type m_paramlist
list of parameters, both required and optional
specialization of argument for SI/IEC suffixes byte size options or parameters
void calc_opt_max(const argument *arg)
update maximum formatting width for new option
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
argument_bytes64(char key, const std::string &longkey, const std::string &keytype, const std::string &desc, bool required, uint64 &dest)
contructor filling most attributes
virtual bool process(int &argc, const char *const *&argv)
parse unsigned integer using sscanf.
argument_stringlist(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
virtual const char * type_name() const
return formatted type name to user
bool m_verbose_process
verbose processing of arguments
specialization of argument for boolean flags (can only be set to true).
virtual const char * type_name() const
return formatted type name to user
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
#define STXXL_END_NAMESPACE
~cmdline_parser()
Delete all added arguments.
int m_param_maxlong
formatting width for parameters, 'param <#>'