13 #ifndef STXXL_COMMON_CMDLINE_HEADER
14 #define STXXL_COMMON_CMDLINE_HEADER
68 const std::string& keytype,
69 const std::string& desc,
bool required)
70 : m_key(key), m_longkey(longkey), m_keytype(keytype),
71 m_desc(desc), m_required(required), m_found(false),
79 virtual const char * type_name()
const = 0;
82 virtual bool process(
int& argc,
const char*
const*& argv) = 0;
85 virtual void print_value(std::ostream& os)
const = 0;
90 std::string s = m_longkey;
91 if (m_keytype.size()) {
108 if (m_keytype.size()) {
109 s +=
' ' + m_keytype;
123 const std::string& keytype,
124 const std::string& desc,
bool required,
bool& dest)
125 :
argument(key, longkey, keytype, desc, required),
133 virtual bool process(
int&,
const char*
const*&)
140 { os << (m_dest ?
"true" :
"false"); }
150 const std::string& keytype,
151 const std::string& desc,
bool required,
int& dest)
152 :
argument(key, longkey, keytype, desc, required),
157 {
return "integer"; }
160 virtual bool process(
int& argc,
const char*
const*& argv)
162 if (argc == 0)
return false;
163 if (sscanf(argv[0],
"%d", &m_dest) == 1) {
183 const std::string& keytype,
184 const std::string& desc,
bool required,
186 :
argument(key, longkey, keytype, desc, required),
191 {
return "unsigned integer"; }
194 virtual bool process(
int& argc,
const char*
const*& argv)
196 if (argc == 0)
return false;
197 if (sscanf(argv[0],
"%u", &m_dest) == 1) {
217 const std::string& keytype,
218 const std::string& desc,
bool required,
220 :
argument(key, longkey, keytype, desc, required),
228 virtual bool process(
int& argc,
const char*
const*& argv)
230 if (argc == 0)
return false;
231 if (sscanf(argv[0],
"%lf", &m_dest) == 1) {
252 const std::string& keytype,
253 const std::string& desc,
bool required,
uint32& dest)
254 :
argument(key, longkey, keytype, desc, required),
262 virtual bool process(
int& argc,
const char*
const*& argv)
264 if (argc == 0)
return false;
288 const std::string& keytype,
289 const std::string& desc,
bool required,
uint64& dest)
290 :
argument(key, longkey, keytype, desc, required),
298 virtual bool process(
int& argc,
const char*
const*& argv)
300 if (argc == 0)
return false;
321 const std::string& keytype,
322 const std::string& desc,
bool required,
324 :
argument(key, longkey, keytype, desc, required),
332 virtual bool process(
int& argc,
const char*
const*& argv)
334 if (argc == 0)
return false;
341 { os <<
'"' << m_dest <<
'"'; }
351 const std::string& keytype,
352 const std::string& desc,
bool required,
353 std::vector<std::string>& dest)
354 :
argument(key, longkey, keytype, desc, required),
361 {
return "string list"; }
364 virtual bool process(
int& argc,
const char*
const*& argv)
366 if (argc == 0)
return false;
367 m_dest.push_back(argv[0]);
375 for (
size_t i = 0; i < m_dest.size(); ++i)
377 if (i != 0) os <<
',';
378 os <<
'"' << m_dest[i] <<
'"';
413 static const int m_maxtypename = 16;
435 output_wrap(std::ostream& os,
const std::string& text,
size_t wraplen,
436 size_t indent_first = 0,
size_t indent_rest = 0,
437 size_t current = 0,
size_t indent_newline = 0);
445 m_verbose_process(true),
452 for (
size_t i = 0; i < m_optlist.size(); ++i)
456 for (
size_t i = 0; i < m_paramlist.size(); ++i)
457 delete m_paramlist[i];
464 m_description = description;
476 m_verbose_process = verbose_process;
483 void add_flag(
char key,
const std::string& longkey,
484 const std::string& keytype,
bool& dest,
485 const std::string& desc)
490 calc_opt_max(m_optlist.back());
495 void add_int(
char key,
const std::string& longkey,
496 const std::string& keytype,
int& dest,
497 const std::string& desc)
500 new argument_int(key, longkey, keytype, desc,
false, dest)
502 calc_opt_max(m_optlist.back());
507 void add_uint(
char key,
const std::string& longkey,
508 const std::string& keytype,
unsigned int& dest,
509 const std::string& desc)
514 calc_opt_max(m_optlist.back());
520 const std::string& keytype,
double& dest,
521 const std::string& desc)
526 calc_opt_max(m_optlist.back());
533 const std::string& desc)
538 calc_opt_max(m_optlist.back());
545 const std::string& desc)
550 calc_opt_max(m_optlist.back());
555 const std::string& keytype, std::string& dest,
556 const std::string& desc)
561 calc_opt_max(m_optlist.back());
566 const std::string& keytype,
567 std::vector<std::string>& dest,
568 const std::string& desc)
573 calc_opt_max(m_optlist.back());
578 void add_flag(
char key,
const std::string& longkey,
bool& dest,
579 const std::string& desc)
580 {
return add_flag(key, longkey,
"", dest, desc); }
584 void add_int(
char key,
const std::string& longkey,
int& dest,
585 const std::string& desc)
586 {
return add_int(key, longkey,
"", dest, desc); }
590 void add_uint(
char key,
const std::string& longkey,
unsigned int& dest,
591 const std::string& desc)
592 {
return add_uint(key, longkey,
"", dest, desc); }
596 void add_double(
char key,
const std::string& longkey,
double& dest,
597 const std::string& desc)
598 {
return add_double(key, longkey,
"", dest, desc); }
603 const std::string& desc)
604 {
return add_bytes(key, longkey,
"", dest, desc); }
609 const std::string& desc)
610 {
return add_bytes(key, longkey,
"", dest, desc); }
613 void add_string(
char key,
const std::string& longkey, std::string& dest,
614 const std::string& desc)
615 {
return add_string(key, longkey,
"", dest, desc); }
619 std::vector<std::string>& dest,
const std::string& desc)
620 {
return add_stringlist(key, longkey,
"", dest, desc); }
626 const std::string& desc)
628 m_paramlist.push_back(
631 calc_param_max(m_paramlist.back());
636 const std::string& desc)
638 m_paramlist.push_back(
641 calc_param_max(m_paramlist.back());
646 const std::string& desc)
648 m_paramlist.push_back(
651 calc_param_max(m_paramlist.back());
657 const std::string& desc)
659 m_paramlist.push_back(
662 calc_param_max(m_paramlist.back());
668 const std::string& desc)
670 m_paramlist.push_back(
673 calc_param_max(m_paramlist.back());
678 const std::string& desc)
680 m_paramlist.push_back(
683 calc_param_max(m_paramlist.back());
690 std::vector<std::string>& dest,
691 const std::string& desc)
693 m_paramlist.push_back(
696 calc_param_max(m_paramlist.back());
704 const std::string& desc)
706 m_paramlist.push_back(
709 calc_param_max(m_paramlist.back());
715 const std::string& desc)
717 m_paramlist.push_back(
720 calc_param_max(m_paramlist.back());
725 const std::string& desc)
727 m_paramlist.push_back(
730 calc_param_max(m_paramlist.back());
736 const std::string& desc)
738 m_paramlist.push_back(
741 calc_param_max(m_paramlist.back());
747 const std::string& desc)
749 m_paramlist.push_back(
752 calc_param_max(m_paramlist.back());
757 const std::string& desc)
759 m_paramlist.push_back(
762 calc_param_max(m_paramlist.back());
769 std::vector<std::string>& dest,
770 const std::string& desc)
772 m_paramlist.push_back(
775 calc_param_max(m_paramlist.back());
782 void print_usage(std::ostream& os = std::cout);
786 void print_option_error(
int argc,
const char*
const* argv,
791 void print_param_error(
int argc,
const char*
const* argv,
800 bool process(
int argc,
const char*
const* argv,
801 std::ostream& os = std::cout);
804 void print_result(std::ostream& os = std::cout);
811 #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
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
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.
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
virtual const char * type_name() const
return formatted type name to user
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_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_param_bytes(const std::string &name, uint64 &dest, const std::string &desc)
add SI/IEC suffixes byte size 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
unsigned long long int uint64
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_double(char key, const std::string &longkey, double &dest, const std::string &desc)
add double option -key, –longkey [keytype] 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 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 set_verbose_process(bool verbose_process)
Set verbose processing of command line arguments.
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.
void add_uint(char key, const std::string &longkey, unsigned int &dest, const std::string &desc)
add unsigned integer option -key, –longkey [keytype] with description and store to dest ...
virtual void print_value(std::ostream &os) const
format value to ostream
void add_bytes(char key, const std::string &longkey, stxxl::uint64 &dest, const std::string &desc)
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
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.
std::string m_keytype
option type description, e.g. "<#>" to indicate numbers
void set_description(const std::string &description)
Set description of program, text will be wrapped.
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_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
specialization of argument for double options or parameters
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_opt_param_bytes(const std::string &name, uint64 &dest, const std::string &desc)
add optional SI/IEC suffixes byte size parameter [name] 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_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
virtual bool process(int &argc, const char *const *&argv)
parse byte size using SI/IEC parser from stxxl.
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 ...
std::vector< argument * > arglist_type
option and parameter list type
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)
bool m_repeated
repeated argument, i.e. std::vector<std::string>
#define STXXL_BEGIN_NAMESPACE
void add_bytes(char key, const std::string &longkey, stxxl::uint32 &dest, const std::string &desc)
add SI/IEC suffixes byte size option -key, –longkey [keytype] and store to 32-bit 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]'
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
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...
std::string m_desc
longer description, which will be wrapped
void add_bytes(char key, const std::string &longkey, const std::string &keytype, stxxl::uint64 &dest, const std::string &desc)
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_bytes(const std::string &name, uint32 &dest, const std::string &desc)
add SI/IEC suffixes byte size parameter [name] with description and store to dest ...
virtual void print_value(std::ostream &os) const
format value to ostream
int m_opt_maxlong
formatting width for options, '-s, –switch <#>'
virtual bool process(int &, const char *const *&)
"process" argument: just set to true, no argument is used.
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_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 ...
specialization of argument for integer options or parameters
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
void add_param_double(const std::string &name, double &dest, const std::string &desc)
add double parameter [name] with description and store to dest
virtual bool process(int &argc, const char *const *&argv)
parse unsigned integer using sscanf.
void add_string(char key, const std::string &longkey, std::string &dest, const std::string &desc)
add string 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 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_opt_param_bytes(const std::string &name, uint32 &dest, const std::string &desc)
add optional SI/IEC suffixes byte size parameter [name] with description and store to dest ...
void calc_opt_max(const argument *arg)
update maximum formatting width for new option
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
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.
void add_bytes(char key, const std::string &longkey, const std::string &keytype, stxxl::uint32 &dest, const std::string &desc)
add SI/IEC suffixes byte size option -key, –longkey [keytype] and store to 64-bit dest ...
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
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 ...
argument_double(char key, const std::string &longkey, const std::string &keytype, const std::string &desc, bool required, double &dest)
contructor filling most attributes
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_stringlist(char key, const std::string &longkey, std::vector< std::string > &dest, const std::string &desc)
add string list option -key, –longkey [keytype] and store to dest
#define STXXL_END_NAMESPACE
~cmdline_parser()
Delete all added arguments.
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
int m_param_maxlong
formatting width for parameters, 'param <#>'
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.