STXXL  1.4.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cmdline.h
Go to the documentation of this file.
1 /***************************************************************************
2  * include/stxxl/bits/common/cmdline.h
3  *
4  * Part of the STXXL. See http://stxxl.sourceforge.net
5  *
6  * Copyright (C) 2013 Timo Bingmann <[email protected]>
7  *
8  * Distributed under the Boost Software License, Version 1.0.
9  * (See accompanying file LICENSE_1_0.txt or copy at
10  * http://www.boost.org/LICENSE_1_0.txt)
11  **************************************************************************/
12 
13 #ifndef STXXL_COMMON_CMDLINE_HEADER
14 #define STXXL_COMMON_CMDLINE_HEADER
15 
16 #include <cstddef>
17 #include <cstdio>
18 #include <iostream>
19 #include <string>
20 #include <vector>
21 
24 #include <stxxl/bits/namespace.h>
25 #include <stxxl/bits/noncopyable.h>
26 
28 
29 //! \addtogroup support
30 //! \{
31 
32 /**
33  * Command line parser which automatically fills variables and prints nice
34  * usage messages.
35  *
36  * This is a straightforward command line parser in C++, which will recognize
37  * short options -s, long options --long and parameters, both required and
38  * optional. It will automatically parse integers and <b>byte sizes</b> with
39  * SI/IEC suffixes (e.g. 1 GiB). It also works with lists of strings,
40  * e.g. multiple filenames.
41  *
42  * Maybe most important it will nicely format the options and parameters
43  * description using word wrapping.
44  */
45 class cmdline_parser : private noncopyable
46 {
47 protected:
48  //! base class of all options and parameters
49  struct argument
50  {
51  //! single letter short option, or 0 is none
52  char m_key;
53  //! long option key or name for parameters
54  std::string m_longkey;
55  //! option type description, e.g. "<#>" to indicate numbers
56  std::string m_keytype;
57  //! longer description, which will be wrapped
58  std::string m_desc;
59  //! required, process() fails if the option/parameter is not found.
60  bool m_required;
61  //! found during processing of command line
62  bool m_found;
63  //! repeated argument, i.e. std::vector<std::string>
64  bool m_repeated;
65 
66  //! contructor filling most attributes
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)
71  { }
72 
73  //! empty virtual destructor
74  virtual ~argument() { }
75 
76  //! return formatted type name to user
77  virtual const char * type_name() const = 0;
78 
79  //! process one item from command line for this argument
80  virtual bool process(int& argc, const char* const*& argv) = 0;
81 
82  //! format value to ostream
83  virtual void print_value(std::ostream& os) const = 0;
84 
85  //! return 'longkey [keytype]'
86  std::string param_text() const
87  {
88  std::string s = m_longkey;
89  if (m_keytype.size()) {
90  s += ' ' + m_keytype;
91  }
92  return s;
93  }
94 
95  //! return '-s, --longkey [keytype]'
96  std::string option_text() const
97  {
98  std::string s;
99  if (m_key) {
100  s += '-';
101  s += m_key;
102  s += ", ";
103  }
104  s += "--";
105  s += m_longkey;
106  if (m_keytype.size()) {
107  s += ' ' + m_keytype;
108  }
109  return s;
110  }
111  };
112 
113  //! specialization of argument for boolean flags (can only be set to true).
114  struct argument_flag : public argument
115  {
116  //! reference to boolean to set to true
117  bool& m_dest;
118 
119  //! contructor filling most attributes
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),
123  m_dest(dest)
124  { }
125 
126  virtual const char * type_name() const
127  { return "flag"; }
128 
129  //! "process" argument: just set to true, no argument is used.
130  virtual bool process(int&, const char* const*&)
131  {
132  m_dest = true;
133  return true;
134  }
135 
136  virtual void print_value(std::ostream& os) const
137  { os << (m_dest ? "true" : "false"); }
138  };
139 
140  //! specialization of argument for integer options or parameters
141  struct argument_int : public argument
142  {
143  int& m_dest;
144 
145  //! contructor filling most attributes
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),
149  m_dest(dest)
150  { }
151 
152  virtual const char * type_name() const
153  { return "integer"; }
154 
155  //! parse signed integer using sscanf.
156  virtual bool process(int& argc, const char* const*& argv)
157  {
158  if (argc == 0) return false;
159  if (sscanf(argv[0], "%d", &m_dest) == 1) {
160  --argc, ++argv;
161  return true;
162  } else {
163  return false;
164  }
165  }
166 
167  virtual void print_value(std::ostream& os) const
168  { os << m_dest; }
169  };
170 
171  //! specialization of argument for unsigned integer options or parameters
172  struct argument_uint : public argument
173  {
174  unsigned int& m_dest;
175 
176  //! contructor filling most attributes
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),
180  m_dest(dest)
181  { }
182 
183  virtual const char * type_name() const
184  { return "unsigned integer"; }
185 
186  //! parse unsigned integer using sscanf.
187  virtual bool process(int& argc, const char* const*& argv)
188  {
189  if (argc == 0) return false;
190  if (sscanf(argv[0], "%u", &m_dest) == 1) {
191  --argc, ++argv;
192  return true;
193  } else {
194  return false;
195  }
196  }
197 
198  virtual void print_value(std::ostream& os) const
199  { os << m_dest; }
200  };
201 
202  //! specialization of argument for SI/IEC suffixes byte size options or parameters
203  struct argument_bytes32 : public argument
204  {
206 
207  //! contructor filling most attributes
208  argument_bytes32(char key, const std::string& longkey, const std::string& keytype,
209  const std::string& desc, bool required, uint32& dest)
210  : argument(key, longkey, keytype, desc, required),
211  m_dest(dest)
212  { }
213 
214  virtual const char * type_name() const
215  { return "bytes"; }
216 
217  //! parse byte size using SI/IEC parser from stxxl.
218  virtual bool process(int& argc, const char* const*& argv)
219  {
220  if (argc == 0) return false;
221  uint64 dest;
222  if (parse_SI_IEC_size(argv[0], dest) &&
223  (uint64)(m_dest = (uint32)dest) == dest) {
224  --argc, ++argv;
225  return true;
226  } else {
227  return false;
228  }
229  }
230 
231  virtual void print_value(std::ostream& os) const
232  { os << m_dest; }
233  };
234 
235  //! specialization of argument for SI/IEC suffixes byte size options or parameters
236  struct argument_bytes64 : public argument
237  {
239 
240  //! contructor filling most attributes
241  argument_bytes64(char key, const std::string& longkey, const std::string& keytype,
242  const std::string& desc, bool required, uint64& dest)
243  : argument(key, longkey, keytype, desc, required),
244  m_dest(dest)
245  { }
246 
247  virtual const char * type_name() const
248  { return "bytes"; }
249 
250  //! parse byte size using SI/IEC parser from stxxl.
251  virtual bool process(int& argc, const char* const*& argv)
252  {
253  if (argc == 0) return false;
254  if (parse_SI_IEC_size(argv[0], m_dest)) {
255  --argc, ++argv;
256  return true;
257  } else {
258  return false;
259  }
260  }
261 
262  virtual void print_value(std::ostream& os) const
263  { os << m_dest; }
264  };
265 
266  //! specialization of argument for string options or parameters
267  struct argument_string : public argument
268  {
269  std::string& m_dest;
270 
271  //! contructor filling most attributes
272  argument_string(char key, const std::string& longkey, const std::string& keytype,
273  const std::string& desc, bool required, std::string& dest)
274  : argument(key, longkey, keytype, desc, required),
275  m_dest(dest)
276  { }
277 
278  virtual const char * type_name() const
279  { return "string"; }
280 
281  //! "process" string argument just by storing it.
282  virtual bool process(int& argc, const char* const*& argv)
283  {
284  if (argc == 0) return false;
285  m_dest = argv[0];
286  --argc, ++argv;
287  return true;
288  }
289 
290  virtual void print_value(std::ostream& os) const
291  { os << '"' << m_dest << '"'; }
292  };
293 
294  //! specialization of argument for multiple string options or parameters
296  {
297  std::vector<std::string>& m_dest;
298 
299  //! contructor filling most attributes
300  argument_stringlist(char key, const std::string& longkey, const std::string& keytype,
301  const std::string& desc, bool required, std::vector<std::string>& dest)
302  : argument(key, longkey, keytype, desc, required),
303  m_dest(dest)
304  {
305  m_repeated = true;
306  }
307 
308  virtual const char * type_name() const
309  { return "string list"; }
310 
311  //! "process" string argument just by storing it in vector.
312  virtual bool process(int& argc, const char* const*& argv)
313  {
314  if (argc == 0) return false;
315  m_dest.push_back(argv[0]);
316  --argc, ++argv;
317  return true;
318  }
319 
320  virtual void print_value(std::ostream& os) const
321  {
322  os << '[';
323  for (size_t i = 0; i < m_dest.size(); ++i)
324  {
325  if (i != 0) os << ',';
326  os << '"' << m_dest[i] << '"';
327  }
328  os << ']';
329  }
330  };
331 
332 protected:
333  //! option and parameter list type
334  typedef std::vector<argument*> arglist_type;
335 
336  //! list of options available
338  //! list of parameters, both required and optional
340 
341  //! formatting width for options, '-s, --switch <#>'
343  //! formatting width for parameters, 'param <#>'
345 
346  //! argv[0] for usage.
347  const char* m_progname;
348 
349  //! verbose processing of arguments
351 
352  //! user set description of program, will be wrapped
353  std::string m_description;
354  //! user set author of program, will be wrapped
355  std::string m_author;
356 
357  //! set line wrap length
358  unsigned int m_linewrap;
359 
360  //! maximum length of a type_name() result
361  static const int m_maxtypename = 16;
362 
363 private:
364  //! update maximum formatting width for new option
365  void calc_opt_max(const argument* arg)
366  {
367  m_opt_maxlong = STXXL_MAX((int)arg->option_text().size() + 2,
368  m_opt_maxlong);
369  }
370 
371  //! update maximum formatting width for new parameter
372  void calc_param_max(const argument* arg)
373  {
374  m_param_maxlong = STXXL_MAX((int)arg->param_text().size() + 2,
375  m_param_maxlong);
376  }
377 
378 public:
379  //! Wrap a long string at spaces into lines. Prefix is added
380  //! unconditionally to each line. Lines are wrapped after wraplen
381  //! characters if possible.
382  static void
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);
386 
387 public:
388  //! Construct new command line parser
390  : m_opt_maxlong(8),
391  m_param_maxlong(8),
392  m_progname(NULL),
393  m_verbose_process(true),
394  m_linewrap(80)
395  { }
396 
397  //! Delete all added arguments
399  {
400  for (size_t i = 0; i < m_optlist.size(); ++i)
401  delete m_optlist[i];
402  m_optlist.clear();
403 
404  for (size_t i = 0; i < m_paramlist.size(); ++i)
405  delete m_paramlist[i];
406  m_paramlist.clear();
407  }
408 
409  //! Set description of program, text will be wrapped
410  void set_description(const std::string& description)
411  {
412  m_description = description;
413  }
414 
415  //! Set author of program, will be wrapped.
416  void set_author(const std::string& author)
417  {
418  m_author = author;
419  }
420 
421  //! Set verbose processing of command line arguments
422  void set_verbose_process(bool verbose_process)
423  {
424  m_verbose_process = verbose_process;
425  }
426 
427  // ************************************************************************
428 
429  //! add boolean option flag -key, --longkey [keytype] with description and store to dest
430  void add_flag(char key, const std::string& longkey, const std::string& keytype, const std::string& desc, bool& dest)
431  {
432  m_optlist.push_back(
433  new argument_flag(key, longkey, keytype, desc, false, dest)
434  );
435  calc_opt_max(m_optlist.back());
436  }
437 
438  //! add signed integer option -key, --longkey [keytype] with description and store to dest
439  void add_int(char key, const std::string& longkey, const std::string& keytype, const std::string& desc, int& dest)
440  {
441  m_optlist.push_back(
442  new argument_int(key, longkey, keytype, desc, false, dest)
443  );
444  calc_opt_max(m_optlist.back());
445  }
446 
447  //! add unsigned integer option -key, --longkey [keytype] with description and store to dest
448  void add_uint(char key, const std::string& longkey, const std::string& keytype, const std::string& desc, unsigned int& dest)
449  {
450  m_optlist.push_back(
451  new argument_uint(key, longkey, keytype, desc, false, dest)
452  );
453  calc_opt_max(m_optlist.back());
454  }
455 
456  //! add SI/IEC suffixes byte size option -key, --longkey [keytype] and store to 64-bit dest
457  void add_bytes(char key, const std::string& longkey, const std::string& keytype, const std::string& desc, stxxl::uint32& dest)
458  {
459  m_optlist.push_back(
460  new argument_bytes32(key, longkey, keytype, desc, false, dest)
461  );
462  calc_opt_max(m_optlist.back());
463  }
464 
465  //! add SI/IEC suffixes byte size option -key, --longkey [keytype] and store to 64-bit dest
466  void add_bytes(char key, const std::string& longkey, const std::string& keytype, const std::string& desc, stxxl::uint64& dest)
467  {
468  m_optlist.push_back(
469  new argument_bytes64(key, longkey, keytype, desc, false, dest)
470  );
471  calc_opt_max(m_optlist.back());
472  }
473 
474  //! add string option -key, --longkey [keytype] and store to dest
475  void add_string(char key, const std::string& longkey, const std::string& keytype, const std::string& desc, std::string& dest)
476  {
477  m_optlist.push_back(
478  new argument_string(key, longkey, keytype, desc, false, dest)
479  );
480  calc_opt_max(m_optlist.back());
481  }
482 
483  //! add string list option -key, --longkey [keytype] and store to dest
484  void add_stringlist(char key, const std::string& longkey, const std::string& keytype, const std::string& desc, std::vector<std::string>& dest)
485  {
486  m_optlist.push_back(
487  new argument_stringlist(key, longkey, keytype, desc, false, dest)
488  );
489  calc_opt_max(m_optlist.back());
490  }
491 
492  //! add boolean option flag -key, --longkey with description and store to dest
493  void add_flag(char key, const std::string& longkey, const std::string& desc, bool& dest)
494  { return add_flag(key, longkey, "", desc, dest); }
495 
496  //! add signed integer option -key, --longkey with description and store to 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); }
499 
500  //! add unsigned integer option -key, --longkey [keytype] with description and store to 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); }
503 
504  //! add SI/IEC suffixes byte size option -key, --longkey [keytype] and store to 32-bit dest
505  void add_bytes(char key, const std::string& longkey, const std::string& desc, stxxl::uint32& dest)
506  { return add_bytes(key, longkey, "", desc, dest); }
507 
508  //! add SI/IEC suffixes byte size option -key, --longkey [keytype] and store to 64-bit dest
509  void add_bytes(char key, const std::string& longkey, const std::string& desc, stxxl::uint64& dest)
510  { return add_bytes(key, longkey, "", desc, dest); }
511 
512  //! add string option -key, --longkey [keytype] and store to 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); }
515 
516  //! add string list option -key, --longkey [keytype] and store to 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); }
519 
520  // ************************************************************************
521 
522  //! add signed integer parameter [name] with description and store to dest
523  void add_param_int(const std::string& name, const std::string& desc, int& dest)
524  {
525  m_paramlist.push_back(
526  new argument_int(0, name, "", desc, true, dest)
527  );
528  calc_param_max(m_paramlist.back());
529  }
530 
531  //! add unsigned integer parameter [name] with description and store to dest
532  void add_param_uint(const std::string& name, const std::string& desc, unsigned int& dest)
533  {
534  m_paramlist.push_back(
535  new argument_uint(0, name, "", desc, true, dest)
536  );
537  calc_param_max(m_paramlist.back());
538  }
539 
540  //! add SI/IEC suffixes byte size parameter [name] with description and store to dest
541  void add_param_bytes(const std::string& name, const std::string& desc, uint32& dest)
542  {
543  m_paramlist.push_back(
544  new argument_bytes32(0, name, "", desc, true, dest)
545  );
546  calc_param_max(m_paramlist.back());
547  }
548 
549  //! add SI/IEC suffixes byte size parameter [name] with description and store to dest
550  void add_param_bytes(const std::string& name, const std::string& desc, uint64& dest)
551  {
552  m_paramlist.push_back(
553  new argument_bytes64(0, name, "", desc, true, dest)
554  );
555  calc_param_max(m_paramlist.back());
556  }
557 
558  //! add string parameter [name] with description and store to dest
559  void add_param_string(const std::string& name, const std::string& desc, std::string& dest)
560  {
561  m_paramlist.push_back(
562  new argument_string(0, name, "", desc, true, dest)
563  );
564  calc_param_max(m_paramlist.back());
565  }
566 
567  //! add string list parameter [name] with description and store to dest.
568  //! \warning this parameter must be last, as it will gobble all non-option arguments!
569  void add_param_stringlist(const std::string& name, const std::string& desc, std::vector<std::string>& dest)
570  {
571  m_paramlist.push_back(
572  new argument_stringlist(0, name, "", desc, true, dest)
573  );
574  calc_param_max(m_paramlist.back());
575  }
576 
577  // ************************************************************************
578 
579  //! add optional signed integer parameter [name] with description and store to dest
580  void add_opt_param_int(const std::string& name, const std::string& desc, int& dest)
581  {
582  m_paramlist.push_back(
583  new argument_int(0, name, "", desc, false, dest)
584  );
585  calc_param_max(m_paramlist.back());
586  }
587 
588  //! add optional unsigned integer parameter [name] with description and store to dest
589  void add_opt_param_uint(const std::string& name, const std::string& desc, unsigned int& dest)
590  {
591  m_paramlist.push_back(
592  new argument_uint(0, name, "", desc, false, dest)
593  );
594  calc_param_max(m_paramlist.back());
595  }
596 
597  //! add optional SI/IEC suffixes byte size parameter [name] with description and store to dest
598  void add_opt_param_bytes(const std::string& name, const std::string& desc, uint32& dest)
599  {
600  m_paramlist.push_back(
601  new argument_bytes32(0, name, "", desc, false, dest)
602  );
603  calc_param_max(m_paramlist.back());
604  }
605 
606  //! add optional SI/IEC suffixes byte size parameter [name] with description and store to dest
607  void add_opt_param_bytes(const std::string& name, const std::string& desc, uint64& dest)
608  {
609  m_paramlist.push_back(
610  new argument_bytes64(0, name, "", desc, false, dest)
611  );
612  calc_param_max(m_paramlist.back());
613  }
614 
615  //! add optional string parameter [name] with description and store to dest
616  void add_opt_param_string(const std::string& name, const std::string& desc, std::string& dest)
617  {
618  m_paramlist.push_back(
619  new argument_string(0, name, "", desc, false, dest)
620  );
621  calc_param_max(m_paramlist.back());
622  }
623 
624  //! add optional string parameter [name] with description and store to dest
625  //! \warning this parameter must be last, as it will gobble all non-option arguments!
626  void add_opt_param_stringlist(const std::string& name, const std::string& desc, std::vector<std::string>& dest)
627  {
628  m_paramlist.push_back(
629  new argument_stringlist(0, name, "", desc, false, dest)
630  );
631  calc_param_max(m_paramlist.back());
632  }
633 
634  // ************************************************************************
635 
636  //! output nicely formatted usage information including description of all
637  //! parameters and options.
638  void print_usage(std::ostream& os = std::cout);
639 
640 private:
641  //! print error about option.
642  void print_option_error(int argc, const char* const* argv, const argument* arg,
643  std::ostream& os);
644 
645  //! print error about parameter.
646  void print_param_error(int argc, const char* const* argv, const argument* arg,
647  std::ostream& os);
648 
649 public:
650  //! parse command line options as specified by the options and parameters added.
651  //! \return true if command line is okay and all required parameters are present.
652  bool process(int argc, const char* const* argv, std::ostream& os = std::cout);
653 
654  //! print nicely formatted result of processing
655  void print_result(std::ostream& os = std::cout);
656 };
657 
658 //! \}
659 
661 
662 #endif // !STXXL_COMMON_CMDLINE_HEADER
bool m_required
required, process() fails if the option/parameter is not found.
Definition: cmdline.h:60
virtual const char * type_name() const
return formatted type name to user
Definition: cmdline.h:278
std::string m_longkey
long option key or name for parameters
Definition: cmdline.h:54
std::string m_description
user set description of program, will be wrapped
Definition: cmdline.h:353
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
Definition: cmdline.h:626
const char * m_progname
argv[0] for usage.
Definition: cmdline.h:347
argument_flag(char key, const std::string &longkey, const std::string &keytype, const std::string &desc, bool required, bool &dest)
contructor filling most attributes
Definition: cmdline.h:120
argument(char key, const std::string &longkey, const std::string &keytype, const std::string &desc, bool required)
contructor filling most attributes
Definition: cmdline.h:67
virtual bool process(int &argc, const char *const *&argv)
parse byte size using SI/IEC parser from stxxl.
Definition: cmdline.h:251
virtual const char * type_name() const
return formatted type name to user
Definition: cmdline.h:152
unsigned long long int uint64
Definition: types.h:39
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 ...
Definition: cmdline.h:550
std::vector< std::string > & m_dest
Definition: cmdline.h:297
virtual void print_value(std::ostream &os) const
format value to ostream
Definition: cmdline.h:290
virtual void print_value(std::ostream &os) const
format value to ostream
Definition: cmdline.h:136
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 ...
Definition: cmdline.h:509
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
Definition: cmdline.h:559
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 ...
Definition: cmdline.h:457
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
Definition: cmdline.h:272
void set_verbose_process(bool verbose_process)
Set verbose processing of command line arguments.
Definition: cmdline.h:422
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
Definition: cmdline.h:493
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
Definition: cmdline.h:616
cmdline_parser()
Construct new command line parser.
Definition: cmdline.h:389
arglist_type m_optlist
list of options available
Definition: cmdline.h:337
virtual bool process(int &argc, const char *const *&argv)
parse signed integer using sscanf.
Definition: cmdline.h:156
virtual void print_value(std::ostream &os) const
format value to ostream
Definition: cmdline.h:320
void calc_param_max(const argument *arg)
update maximum formatting width for new parameter
Definition: cmdline.h:372
specialization of argument for SI/IEC suffixes byte size options or parameters
Definition: cmdline.h:236
virtual const char * type_name() const
return formatted type name to user
Definition: cmdline.h:214
argument_bytes32(char key, const std::string &longkey, const std::string &keytype, const std::string &desc, bool required, uint32 &dest)
contructor filling most attributes
Definition: cmdline.h:208
virtual ~argument()
empty virtual destructor
Definition: cmdline.h:74
base class of all options and parameters
Definition: cmdline.h:49
void set_author(const std::string &author)
Set author of program, will be wrapped.
Definition: cmdline.h:416
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.
Definition: cmdline.h:569
std::string m_keytype
option type description, e.g. &quot;&lt;#&gt;&quot; to indicate numbers
Definition: cmdline.h:56
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
Definition: cmdline.h:532
void set_description(const std::string &description)
Set description of program, text will be wrapped.
Definition: cmdline.h:410
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 ...
Definition: cmdline.h:589
unsigned int m_linewrap
set line wrap length
Definition: cmdline.h:358
virtual bool process(int &argc, const char *const *&argv)
&quot;process&quot; string argument just by storing it in vector.
Definition: cmdline.h:312
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
Definition: cmdline.h:523
virtual void print_value(std::ostream &os) const
format value to ostream
Definition: cmdline.h:198
specialization of argument for string options or parameters
Definition: cmdline.h:267
virtual void print_value(std::ostream &os) const
format value to ostream
Definition: cmdline.h:231
std::string option_text() const
return &#39;-s, –longkey [keytype]&#39;
Definition: cmdline.h:96
unsigned int uint32
Definition: types.h:37
virtual const char * type_name() const
return formatted type name to user
Definition: cmdline.h:126
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 ...
Definition: cmdline.h:439
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 ...
Definition: cmdline.h:607
virtual bool process(int &argc, const char *const *&argv)
parse byte size using SI/IEC parser from stxxl.
Definition: cmdline.h:218
std::vector< argument * > arglist_type
option and parameter list type
Definition: cmdline.h:334
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 ...
Definition: cmdline.h:501
argument_int(char key, const std::string &longkey, const std::string &keytype, const std::string &desc, bool required, int &dest)
contructor filling most attributes
Definition: cmdline.h:146
bool m_found
found during processing of command line
Definition: cmdline.h:62
const Type & STXXL_MAX(const Type &a, const Type &b)
Definition: utils.h:153
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 ...
Definition: cmdline.h:448
bool m_repeated
repeated argument, i.e. std::vector&lt;std::string&gt;
Definition: cmdline.h:64
#define STXXL_BEGIN_NAMESPACE
Definition: namespace.h:16
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
Definition: cmdline.h:513
std::string m_author
user set author of program, will be wrapped
Definition: cmdline.h:355
virtual void print_value(std::ostream &os) const
format value to ostream
Definition: cmdline.h:167
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 ...
Definition: cmdline.h:598
std::string param_text() const
return &#39;longkey [keytype]&#39;
Definition: cmdline.h:86
bool parse_SI_IEC_size(const std::string &str, uint64 &size, char def_unit=0)
Parse a string like &quot;343KB&quot; or &quot;44 GiB&quot; into the corresponding size in bytes. Returns the number of b...
Definition: utils.cpp:24
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 ...
Definition: cmdline.h:466
std::string m_desc
longer description, which will be wrapped
Definition: cmdline.h:58
virtual const char * type_name() const
return formatted type name to user
Definition: cmdline.h:183
virtual void print_value(std::ostream &os) const
format value to ostream
Definition: cmdline.h:262
int m_opt_maxlong
formatting width for options, &#39;-s, –switch &lt;#&gt;&#39;
Definition: cmdline.h:342
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 ...
Definition: cmdline.h:430
virtual bool process(int &, const char *const *&)
&quot;process&quot; argument: just set to true, no argument is used.
Definition: cmdline.h:130
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
Definition: cmdline.h:497
virtual bool process(int &argc, const char *const *&argv)
&quot;process&quot; string argument just by storing it.
Definition: cmdline.h:282
bool & m_dest
reference to boolean to set to true
Definition: cmdline.h:117
char m_key
single letter short option, or 0 is none
Definition: cmdline.h:52
specialization of argument for unsigned integer options or parameters
Definition: cmdline.h:172
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 ...
Definition: cmdline.h:505
specialization of argument for integer options or parameters
Definition: cmdline.h:141
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
Definition: cmdline.h:475
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 ...
Definition: cmdline.h:541
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
Definition: cmdline.h:484
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
Definition: cmdline.h:177
specialization of argument for multiple string options or parameters
Definition: cmdline.h:295
arglist_type m_paramlist
list of parameters, both required and optional
Definition: cmdline.h:339
specialization of argument for SI/IEC suffixes byte size options or parameters
Definition: cmdline.h:203
void calc_opt_max(const argument *arg)
update maximum formatting width for new option
Definition: cmdline.h:365
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
Definition: cmdline.h:517
argument_bytes64(char key, const std::string &longkey, const std::string &keytype, const std::string &desc, bool required, uint64 &dest)
contructor filling most attributes
Definition: cmdline.h:241
virtual bool process(int &argc, const char *const *&argv)
parse unsigned integer using sscanf.
Definition: cmdline.h:187
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
Definition: cmdline.h:300
virtual const char * type_name() const
return formatted type name to user
Definition: cmdline.h:247
bool m_verbose_process
verbose processing of arguments
Definition: cmdline.h:350
specialization of argument for boolean flags (can only be set to true).
Definition: cmdline.h:114
virtual const char * type_name() const
return formatted type name to user
Definition: cmdline.h:308
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
Definition: cmdline.h:580
#define STXXL_END_NAMESPACE
Definition: namespace.h:17
~cmdline_parser()
Delete all added arguments.
Definition: cmdline.h:398
int m_param_maxlong
formatting width for parameters, &#39;param &lt;#&gt;&#39;
Definition: cmdline.h:344