13 #ifndef STXXL_COMMON_CMDLINE_HEADER
14 #define STXXL_COMMON_CMDLINE_HEADER
64 argument(
char key,
const std::string& longkey,
const std::string& keytype,
65 const std::string& desc,
bool required)
66 : m_key(key), m_longkey(longkey), m_keytype(keytype),
67 m_desc(desc), m_required(required), m_found(false), m_repeated(false)
74 virtual const char * type_name()
const = 0;
77 virtual bool process(
int& argc,
const char*
const*& argv) = 0;
80 virtual void print_value(std::ostream& os)
const = 0;
85 std::string s = m_longkey;
86 if (m_keytype.size()) {
103 if (m_keytype.size()) {
104 s +=
' ' + m_keytype;
117 argument_flag(
char key,
const std::string& longkey,
const std::string& keytype,
118 const std::string& desc,
bool required,
bool& dest)
119 :
argument(key, longkey, keytype, desc, required),
127 virtual bool process(
int&,
const char*
const*&)
134 { os << (m_dest ?
"true" :
"false"); }
143 argument_int(
char key,
const std::string& longkey,
const std::string& keytype,
144 const std::string& desc,
bool required,
int& dest)
145 :
argument(key, longkey, keytype, desc, required),
150 {
return "integer"; }
153 virtual bool process(
int& argc,
const char*
const*& argv)
155 if (argc == 0)
return false;
156 if (sscanf(argv[0],
"%d", &m_dest) == 1) {
174 argument_uint(
char key,
const std::string& longkey,
const std::string& keytype,
175 const std::string& desc,
bool required,
unsigned int& dest)
176 :
argument(key, longkey, keytype, desc, required),
181 {
return "unsigned integer"; }
184 virtual bool process(
int& argc,
const char*
const*& argv)
186 if (argc == 0)
return false;
187 if (sscanf(argv[0],
"%u", &m_dest) == 1) {
206 const std::string& desc,
bool required,
uint64& dest)
207 :
argument(key, longkey, keytype, desc, required),
215 virtual bool process(
int& argc,
const char*
const*& argv)
217 if (argc == 0)
return false;
237 const std::string& desc,
bool required, std::string& dest)
238 :
argument(key, longkey, keytype, desc, required),
246 virtual bool process(
int& argc,
const char*
const*& argv)
248 if (argc == 0)
return false;
255 { os <<
'"' << m_dest <<
'"'; }
265 const std::string& desc,
bool required, std::vector<std::string>& dest)
266 :
argument(key, longkey, keytype, desc, required),
273 {
return "string list"; }
276 virtual bool process(
int& argc,
const char*
const*& argv)
278 if (argc == 0)
return false;
279 m_dest.push_back(argv[0]);
287 for (
size_t i = 0; i < m_dest.size(); ++i)
289 if (i != 0) os <<
',';
290 os <<
'"' << m_dest[i] <<
'"';
325 static const int m_maxtypename = 16;
345 output_wrap(std::ostream& os,
const std::string& text,
size_t wraplen,
346 size_t indent_first = 0,
size_t indent_rest = 0,
347 size_t current = 0,
size_t indent_newline = 0);
355 m_verbose_process(true),
362 for (
size_t i = 0; i < m_optlist.size(); ++i)
366 for (
size_t i = 0; i < m_paramlist.size(); ++i)
367 delete m_paramlist[i];
374 m_description = description;
386 m_verbose_process = verbose_process;
392 void add_flag(
char key,
const std::string& longkey,
const std::string& keytype,
const std::string& desc,
bool& dest)
397 calc_opt_max(m_optlist.back());
401 void add_int(
char key,
const std::string& longkey,
const std::string& keytype,
const std::string& desc,
int& dest)
404 new argument_int(key, longkey, keytype, desc,
false, dest)
406 calc_opt_max(m_optlist.back());
410 void add_uint(
char key,
const std::string& longkey,
const std::string& keytype,
const std::string& desc,
unsigned int& dest)
415 calc_opt_max(m_optlist.back());
419 void add_bytes(
char key,
const std::string& longkey,
const std::string& keytype,
const std::string& desc,
stxxl::uint64& dest)
424 calc_opt_max(m_optlist.back());
428 void add_string(
char key,
const std::string& longkey,
const std::string& keytype,
const std::string& desc, std::string& dest)
433 calc_opt_max(m_optlist.back());
437 void add_stringlist(
char key,
const std::string& longkey,
const std::string& keytype,
const std::string& desc, std::vector<std::string>& dest)
442 calc_opt_max(m_optlist.back());
446 void add_flag(
char key,
const std::string& longkey,
const std::string& desc,
bool& dest)
447 {
return add_flag(key, longkey,
"", desc, dest); }
450 void add_int(
char key,
const std::string& longkey,
const std::string& desc,
int& dest)
451 {
return add_int(key, longkey,
"", desc, dest); }
454 void add_uint(
char key,
const std::string& longkey,
const std::string& desc,
unsigned int& dest)
455 {
return add_uint(key, longkey,
"", desc, dest); }
459 {
return add_bytes(key, longkey,
"", desc, dest); }
462 void add_string(
char key,
const std::string& longkey,
const std::string& desc, std::string& dest)
463 {
return add_string(key, longkey,
"", desc, dest); }
466 void add_stringlist(
char key,
const std::string& longkey,
const std::string& desc, std::vector<std::string>& dest)
467 {
return add_stringlist(key, longkey,
"", desc, dest); }
472 void add_param_int(
const std::string& name,
const std::string& desc,
int& dest)
474 m_paramlist.push_back(
477 calc_param_max(m_paramlist.back());
481 void add_param_uint(
const std::string& name,
const std::string& desc,
unsigned int& dest)
483 m_paramlist.push_back(
486 calc_param_max(m_paramlist.back());
492 m_paramlist.push_back(
495 calc_param_max(m_paramlist.back());
501 m_paramlist.push_back(
504 calc_param_max(m_paramlist.back());
511 m_paramlist.push_back(
514 calc_param_max(m_paramlist.back());
522 m_paramlist.push_back(
525 calc_param_max(m_paramlist.back());
531 m_paramlist.push_back(
534 calc_param_max(m_paramlist.back());
540 m_paramlist.push_back(
543 calc_param_max(m_paramlist.back());
549 m_paramlist.push_back(
552 calc_param_max(m_paramlist.back());
559 m_paramlist.push_back(
562 calc_param_max(m_paramlist.back());
569 void print_usage(std::ostream& os = std::cout);
573 void print_option_error(
int argc,
const char*
const* argv,
const argument* arg,
577 void print_param_error(
int argc,
const char*
const* argv,
const argument* arg,
583 bool process(
int argc,
const char*
const* argv, std::ostream& os = std::cout);
586 void print_result(std::ostream& os = std::cout);
591 #endif // !STXXL_COMMON_CMDLINE_HEADER
size_t m_opt_maxlong
formatting width for options, '-s, –switch <#>'
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 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 ...
virtual const char * type_name() const
return formatted type name to user
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
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
virtual ~argument()
empty virtual destructor
Command line parser which automatically fills variables and prints nice usage messages.
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
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 ...
const Tp & STXXL_MAX(const Tp &a, const Tp &b)
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
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
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 ...
virtual void print_value(std::ostream &os) const
format value to ostream
std::string m_desc
longer description, which will be wrapped
size_t m_param_maxlong
formatting width for parameters, 'param <#>'
virtual const char * type_name() const
return formatted type name to user
virtual bool process(int &argc, const char *const *&argv)
parse byte size using SI/IEC parser from stxxl.
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
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_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
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_bytes(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
specialization of argument for SI/IEC suffixes byte size options or parameters
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.