STXXL  1.4-dev
 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,
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),
72  m_repeated(false)
73  { }
74 
75  //! empty virtual destructor
76  virtual ~argument() { }
77 
78  //! return formatted type name to user
79  virtual const char * type_name() const = 0;
80 
81  //! process one item from command line for this argument
82  virtual bool process(int& argc, const char* const*& argv) = 0;
83 
84  //! format value to ostream
85  virtual void print_value(std::ostream& os) const = 0;
86 
87  //! return 'longkey [keytype]'
88  std::string param_text() const
89  {
90  std::string s = m_longkey;
91  if (m_keytype.size()) {
92  s += ' ' + m_keytype;
93  }
94  return s;
95  }
96 
97  //! return '-s, --longkey [keytype]'
98  std::string option_text() const
99  {
100  std::string s;
101  if (m_key) {
102  s += '-';
103  s += m_key;
104  s += ", ";
105  }
106  s += "--";
107  s += m_longkey;
108  if (m_keytype.size()) {
109  s += ' ' + m_keytype;
110  }
111  return s;
112  }
113  };
114 
115  //! specialization of argument for boolean flags (can only be set to true).
116  struct argument_flag : public argument
117  {
118  //! reference to boolean to set to true
119  bool& m_dest;
120 
121  //! contructor filling most attributes
122  argument_flag(char key, const std::string& longkey,
123  const std::string& keytype,
124  const std::string& desc, bool required, bool& dest)
125  : argument(key, longkey, keytype, desc, required),
126  m_dest(dest)
127  { }
128 
129  virtual const char * type_name() const
130  { return "flag"; }
131 
132  //! "process" argument: just set to true, no argument is used.
133  virtual bool process(int&, const char* const*&)
134  {
135  m_dest = true;
136  return true;
137  }
138 
139  virtual void print_value(std::ostream& os) const
140  { os << (m_dest ? "true" : "false"); }
141  };
142 
143  //! specialization of argument for integer options or parameters
144  struct argument_int : public argument
145  {
146  int& m_dest;
147 
148  //! contructor filling most attributes
149  argument_int(char key, const std::string& longkey,
150  const std::string& keytype,
151  const std::string& desc, bool required, int& dest)
152  : argument(key, longkey, keytype, desc, required),
153  m_dest(dest)
154  { }
155 
156  virtual const char * type_name() const
157  { return "integer"; }
158 
159  //! parse signed integer using sscanf.
160  virtual bool process(int& argc, const char* const*& argv)
161  {
162  if (argc == 0) return false;
163  if (sscanf(argv[0], "%d", &m_dest) == 1) {
164  --argc, ++argv;
165  return true;
166  }
167  else {
168  return false;
169  }
170  }
171 
172  virtual void print_value(std::ostream& os) const
173  { os << m_dest; }
174  };
175 
176  //! specialization of argument for unsigned integer options or parameters
177  struct argument_uint : public argument
178  {
179  unsigned int& m_dest;
180 
181  //! contructor filling most attributes
182  argument_uint(char key, const std::string& longkey,
183  const std::string& keytype,
184  const std::string& desc, bool required,
185  unsigned int& dest)
186  : argument(key, longkey, keytype, desc, required),
187  m_dest(dest)
188  { }
189 
190  virtual const char * type_name() const
191  { return "unsigned integer"; }
192 
193  //! parse unsigned integer using sscanf.
194  virtual bool process(int& argc, const char* const*& argv)
195  {
196  if (argc == 0) return false;
197  if (sscanf(argv[0], "%u", &m_dest) == 1) {
198  --argc, ++argv;
199  return true;
200  }
201  else {
202  return false;
203  }
204  }
205 
206  virtual void print_value(std::ostream& os) const
207  { os << m_dest; }
208  };
209 
210  //! specialization of argument for double options or parameters
211  struct argument_double : public argument
212  {
213  double& m_dest;
214 
215  //! contructor filling most attributes
216  argument_double(char key, const std::string& longkey,
217  const std::string& keytype,
218  const std::string& desc, bool required,
219  double& dest)
220  : argument(key, longkey, keytype, desc, required),
221  m_dest(dest)
222  { }
223 
224  virtual const char * type_name() const
225  { return "double"; }
226 
227  //! parse unsigned integer using sscanf.
228  virtual bool process(int& argc, const char* const*& argv)
229  {
230  if (argc == 0) return false;
231  if (sscanf(argv[0], "%lf", &m_dest) == 1) {
232  --argc, ++argv;
233  return true;
234  }
235  else {
236  return false;
237  }
238  }
239 
240  virtual void print_value(std::ostream& os) const
241  { os << m_dest; }
242  };
243 
244  //! specialization of argument for SI/IEC suffixes byte size options or
245  //! parameters
246  struct argument_bytes32 : public argument
247  {
249 
250  //! contructor filling most attributes
251  argument_bytes32(char key, const std::string& longkey,
252  const std::string& keytype,
253  const std::string& desc, bool required, uint32& dest)
254  : argument(key, longkey, keytype, desc, required),
255  m_dest(dest)
256  { }
257 
258  virtual const char * type_name() const
259  { return "bytes"; }
260 
261  //! parse byte size using SI/IEC parser from stxxl.
262  virtual bool process(int& argc, const char* const*& argv)
263  {
264  if (argc == 0) return false;
265  uint64 dest;
266  if (parse_SI_IEC_size(argv[0], dest) &&
267  (uint64)(m_dest = (uint32)dest) == dest) {
268  --argc, ++argv;
269  return true;
270  }
271  else {
272  return false;
273  }
274  }
275 
276  virtual void print_value(std::ostream& os) const
277  { os << m_dest; }
278  };
279 
280  //! specialization of argument for SI/IEC suffixes byte size options or
281  //! parameters
282  struct argument_bytes64 : public argument
283  {
285 
286  //! contructor filling most attributes
287  argument_bytes64(char key, const std::string& longkey,
288  const std::string& keytype,
289  const std::string& desc, bool required, uint64& dest)
290  : argument(key, longkey, keytype, desc, required),
291  m_dest(dest)
292  { }
293 
294  virtual const char * type_name() const
295  { return "bytes"; }
296 
297  //! parse byte size using SI/IEC parser from stxxl.
298  virtual bool process(int& argc, const char* const*& argv)
299  {
300  if (argc == 0) return false;
301  if (parse_SI_IEC_size(argv[0], m_dest)) {
302  --argc, ++argv;
303  return true;
304  }
305  else {
306  return false;
307  }
308  }
309 
310  virtual void print_value(std::ostream& os) const
311  { os << m_dest; }
312  };
313 
314  //! specialization of argument for string options or parameters
315  struct argument_string : public argument
316  {
317  std::string& m_dest;
318 
319  //! contructor filling most attributes
320  argument_string(char key, const std::string& longkey,
321  const std::string& keytype,
322  const std::string& desc, bool required,
323  std::string& dest)
324  : argument(key, longkey, keytype, desc, required),
325  m_dest(dest)
326  { }
327 
328  virtual const char * type_name() const
329  { return "string"; }
330 
331  //! "process" string argument just by storing it.
332  virtual bool process(int& argc, const char* const*& argv)
333  {
334  if (argc == 0) return false;
335  m_dest = argv[0];
336  --argc, ++argv;
337  return true;
338  }
339 
340  virtual void print_value(std::ostream& os) const
341  { os << '"' << m_dest << '"'; }
342  };
343 
344  //! specialization of argument for multiple string options or parameters
346  {
347  std::vector<std::string>& m_dest;
348 
349  //! contructor filling most attributes
350  argument_stringlist(char key, const std::string& longkey,
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),
355  m_dest(dest)
356  {
357  m_repeated = true;
358  }
359 
360  virtual const char * type_name() const
361  { return "string list"; }
362 
363  //! "process" string argument just by storing it in vector.
364  virtual bool process(int& argc, const char* const*& argv)
365  {
366  if (argc == 0) return false;
367  m_dest.push_back(argv[0]);
368  --argc, ++argv;
369  return true;
370  }
371 
372  virtual void print_value(std::ostream& os) const
373  {
374  os << '[';
375  for (size_t i = 0; i < m_dest.size(); ++i)
376  {
377  if (i != 0) os << ',';
378  os << '"' << m_dest[i] << '"';
379  }
380  os << ']';
381  }
382  };
383 
384 protected:
385  //! option and parameter list type
386  typedef std::vector<argument*> arglist_type;
387 
388  //! list of options available
390  //! list of parameters, both required and optional
392 
393  //! formatting width for options, '-s, --switch <#>'
395  //! formatting width for parameters, 'param <#>'
397 
398  //! argv[0] for usage.
399  const char* m_progname;
400 
401  //! verbose processing of arguments
403 
404  //! user set description of program, will be wrapped
405  std::string m_description;
406  //! user set author of program, will be wrapped
407  std::string m_author;
408 
409  //! set line wrap length
410  unsigned int m_linewrap;
411 
412  //! maximum length of a type_name() result
413  static const int m_maxtypename = 16;
414 
415 private:
416  //! update maximum formatting width for new option
417  void calc_opt_max(const argument* arg)
418  {
419  m_opt_maxlong = STXXL_MAX((int)arg->option_text().size() + 2,
420  m_opt_maxlong);
421  }
422 
423  //! update maximum formatting width for new parameter
424  void calc_param_max(const argument* arg)
425  {
426  m_param_maxlong = STXXL_MAX((int)arg->param_text().size() + 2,
427  m_param_maxlong);
428  }
429 
430 public:
431  //! Wrap a long string at spaces into lines. Prefix is added
432  //! unconditionally to each line. Lines are wrapped after wraplen
433  //! characters if possible.
434  static void
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);
438 
439 public:
440  //! Construct new command line parser
442  : m_opt_maxlong(8),
443  m_param_maxlong(8),
444  m_progname(NULL),
445  m_verbose_process(true),
446  m_linewrap(80)
447  { }
448 
449  //! Delete all added arguments
451  {
452  for (size_t i = 0; i < m_optlist.size(); ++i)
453  delete m_optlist[i];
454  m_optlist.clear();
455 
456  for (size_t i = 0; i < m_paramlist.size(); ++i)
457  delete m_paramlist[i];
458  m_paramlist.clear();
459  }
460 
461  //! Set description of program, text will be wrapped
462  void set_description(const std::string& description)
463  {
464  m_description = description;
465  }
466 
467  //! Set author of program, will be wrapped.
468  void set_author(const std::string& author)
469  {
470  m_author = author;
471  }
472 
473  //! Set verbose processing of command line arguments
474  void set_verbose_process(bool verbose_process)
475  {
476  m_verbose_process = verbose_process;
477  }
478 
479  // ************************************************************************
480 
481  //! add boolean option flag -key, --longkey [keytype] with description and
482  //! store to dest
483  void add_flag(char key, const std::string& longkey,
484  const std::string& keytype, bool& dest,
485  const std::string& desc)
486  {
487  m_optlist.push_back(
488  new argument_flag(key, longkey, keytype, desc, false, dest)
489  );
490  calc_opt_max(m_optlist.back());
491  }
492 
493  //! add signed integer option -key, --longkey [keytype] with description
494  //! and store to dest
495  void add_int(char key, const std::string& longkey,
496  const std::string& keytype, int& dest,
497  const std::string& desc)
498  {
499  m_optlist.push_back(
500  new argument_int(key, longkey, keytype, desc, false, dest)
501  );
502  calc_opt_max(m_optlist.back());
503  }
504 
505  //! add unsigned integer option -key, --longkey [keytype] with description
506  //! and store to dest
507  void add_uint(char key, const std::string& longkey,
508  const std::string& keytype, unsigned int& dest,
509  const std::string& desc)
510  {
511  m_optlist.push_back(
512  new argument_uint(key, longkey, keytype, desc, false, dest)
513  );
514  calc_opt_max(m_optlist.back());
515  }
516 
517  //! add double option -key, --longkey [keytype] with description and store
518  //! to dest
519  void add_double(char key, const std::string& longkey,
520  const std::string& keytype, double& dest,
521  const std::string& desc)
522  {
523  m_optlist.push_back(
524  new argument_double(key, longkey, keytype, desc, false, dest)
525  );
526  calc_opt_max(m_optlist.back());
527  }
528 
529  //! add SI/IEC suffixes byte size option -key, --longkey [keytype] and
530  //! store to 64-bit dest
531  void add_bytes(char key, const std::string& longkey,
532  const std::string& keytype, stxxl::uint32& dest,
533  const std::string& desc)
534  {
535  m_optlist.push_back(
536  new argument_bytes32(key, longkey, keytype, desc, false, dest)
537  );
538  calc_opt_max(m_optlist.back());
539  }
540 
541  //! add SI/IEC suffixes byte size option -key, --longkey [keytype] and
542  //! store to 64-bit dest
543  void add_bytes(char key, const std::string& longkey,
544  const std::string& keytype, stxxl::uint64& dest,
545  const std::string& desc)
546  {
547  m_optlist.push_back(
548  new argument_bytes64(key, longkey, keytype, desc, false, dest)
549  );
550  calc_opt_max(m_optlist.back());
551  }
552 
553  //! add string option -key, --longkey [keytype] and store to dest
554  void add_string(char key, const std::string& longkey,
555  const std::string& keytype, std::string& dest,
556  const std::string& desc)
557  {
558  m_optlist.push_back(
559  new argument_string(key, longkey, keytype, desc, false, dest)
560  );
561  calc_opt_max(m_optlist.back());
562  }
563 
564  //! add string list option -key, --longkey [keytype] and store to dest
565  void add_stringlist(char key, const std::string& longkey,
566  const std::string& keytype,
567  std::vector<std::string>& dest,
568  const std::string& desc)
569  {
570  m_optlist.push_back(
571  new argument_stringlist(key, longkey, keytype, desc, false, dest)
572  );
573  calc_opt_max(m_optlist.back());
574  }
575 
576  //! add boolean option flag -key, --longkey with description and store to
577  //! dest
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); }
581 
582  //! add signed integer option -key, --longkey with description and store to
583  //! dest
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); }
587 
588  //! add unsigned integer option -key, --longkey [keytype] with description
589  //! and store to dest
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); }
593 
594  //! add double option -key, --longkey [keytype] with description and store
595  //! to dest
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); }
599 
600  //! add SI/IEC suffixes byte size option -key, --longkey [keytype] and
601  //! store to 32-bit dest
602  void add_bytes(char key, const std::string& longkey, stxxl::uint32& dest,
603  const std::string& desc)
604  { return add_bytes(key, longkey, "", dest, desc); }
605 
606  //! add SI/IEC suffixes byte size option -key, --longkey [keytype] and
607  //! store to 64-bit dest
608  void add_bytes(char key, const std::string& longkey, stxxl::uint64& dest,
609  const std::string& desc)
610  { return add_bytes(key, longkey, "", dest, desc); }
611 
612  //! add string option -key, --longkey [keytype] and store to dest
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); }
616 
617  //! add string list option -key, --longkey [keytype] and store to dest
618  void add_stringlist(char key, const std::string& longkey,
619  std::vector<std::string>& dest, const std::string& desc)
620  { return add_stringlist(key, longkey, "", dest, desc); }
621 
622  // ************************************************************************
623 
624  //! add signed integer parameter [name] with description and store to dest
625  void add_param_int(const std::string& name, int& dest,
626  const std::string& desc)
627  {
628  m_paramlist.push_back(
629  new argument_int(0, name, "", desc, true, dest)
630  );
631  calc_param_max(m_paramlist.back());
632  }
633 
634  //! add unsigned integer parameter [name] with description and store to dest
635  void add_param_uint(const std::string& name, unsigned int& dest,
636  const std::string& desc)
637  {
638  m_paramlist.push_back(
639  new argument_uint(0, name, "", desc, true, dest)
640  );
641  calc_param_max(m_paramlist.back());
642  }
643 
644  //! add double parameter [name] with description and store to dest
645  void add_param_double(const std::string& name, double& dest,
646  const std::string& desc)
647  {
648  m_paramlist.push_back(
649  new argument_double(0, name, "", desc, true, dest)
650  );
651  calc_param_max(m_paramlist.back());
652  }
653 
654  //! add SI/IEC suffixes byte size parameter [name] with description and
655  //! store to dest
656  void add_param_bytes(const std::string& name, uint32& dest,
657  const std::string& desc)
658  {
659  m_paramlist.push_back(
660  new argument_bytes32(0, name, "", desc, true, dest)
661  );
662  calc_param_max(m_paramlist.back());
663  }
664 
665  //! add SI/IEC suffixes byte size parameter [name] with description and
666  //! store to dest
667  void add_param_bytes(const std::string& name, uint64& dest,
668  const std::string& desc)
669  {
670  m_paramlist.push_back(
671  new argument_bytes64(0, name, "", desc, true, dest)
672  );
673  calc_param_max(m_paramlist.back());
674  }
675 
676  //! add string parameter [name] with description and store to dest
677  void add_param_string(const std::string& name, std::string& dest,
678  const std::string& desc)
679  {
680  m_paramlist.push_back(
681  new argument_string(0, name, "", desc, true, dest)
682  );
683  calc_param_max(m_paramlist.back());
684  }
685 
686  //! add string list parameter [name] with description and store to dest.
687  //! \warning this parameter must be last, as it will gobble all non-option
688  //! arguments!
689  void add_param_stringlist(const std::string& name,
690  std::vector<std::string>& dest,
691  const std::string& desc)
692  {
693  m_paramlist.push_back(
694  new argument_stringlist(0, name, "", desc, true, dest)
695  );
696  calc_param_max(m_paramlist.back());
697  }
698 
699  // ************************************************************************
700 
701  //! add optional signed integer parameter [name] with description and store
702  //! to dest
703  void add_opt_param_int(const std::string& name, int& dest,
704  const std::string& desc)
705  {
706  m_paramlist.push_back(
707  new argument_int(0, name, "", desc, false, dest)
708  );
709  calc_param_max(m_paramlist.back());
710  }
711 
712  //! add optional unsigned integer parameter [name] with description and
713  //! store to dest
714  void add_opt_param_uint(const std::string& name, unsigned int& dest,
715  const std::string& desc)
716  {
717  m_paramlist.push_back(
718  new argument_uint(0, name, "", desc, false, dest)
719  );
720  calc_param_max(m_paramlist.back());
721  }
722 
723  //! add optional double parameter [name] with description and store to dest
724  void add_opt_param_double(const std::string& name, double& dest,
725  const std::string& desc)
726  {
727  m_paramlist.push_back(
728  new argument_double(0, name, "", desc, false, dest)
729  );
730  calc_param_max(m_paramlist.back());
731  }
732 
733  //! add optional SI/IEC suffixes byte size parameter [name] with
734  //! description and store to dest
735  void add_opt_param_bytes(const std::string& name, uint32& dest,
736  const std::string& desc)
737  {
738  m_paramlist.push_back(
739  new argument_bytes32(0, name, "", desc, false, dest)
740  );
741  calc_param_max(m_paramlist.back());
742  }
743 
744  //! add optional SI/IEC suffixes byte size parameter [name] with
745  //! description and store to dest
746  void add_opt_param_bytes(const std::string& name, uint64& dest,
747  const std::string& desc)
748  {
749  m_paramlist.push_back(
750  new argument_bytes64(0, name, "", desc, false, dest)
751  );
752  calc_param_max(m_paramlist.back());
753  }
754 
755  //! add optional string parameter [name] with description and store to dest
756  void add_opt_param_string(const std::string& name, std::string& dest,
757  const std::string& desc)
758  {
759  m_paramlist.push_back(
760  new argument_string(0, name, "", desc, false, dest)
761  );
762  calc_param_max(m_paramlist.back());
763  }
764 
765  //! add optional string parameter [name] with description and store to dest
766  //! \warning this parameter must be last, as it will gobble all non-option
767  //! arguments!
768  void add_opt_param_stringlist(const std::string& name,
769  std::vector<std::string>& dest,
770  const std::string& desc)
771  {
772  m_paramlist.push_back(
773  new argument_stringlist(0, name, "", desc, false, dest)
774  );
775  calc_param_max(m_paramlist.back());
776  }
777 
778  // ************************************************************************
779 
780  //! output nicely formatted usage information including description of all
781  //! parameters and options.
782  void print_usage(std::ostream& os = std::cout);
783 
784 private:
785  //! print error about option.
786  void print_option_error(int argc, const char* const* argv,
787  const argument* arg,
788  std::ostream& os);
789 
790  //! print error about parameter.
791  void print_param_error(int argc, const char* const* argv,
792  const argument* arg,
793  std::ostream& os);
794 
795 public:
796  //! parse command line options as specified by the options and parameters
797  //! added.
798  //! \return true if command line is okay and all required parameters are
799  //! present.
800  bool process(int argc, const char* const* argv,
801  std::ostream& os = std::cout);
802 
803  //! print nicely formatted result of processing
804  void print_result(std::ostream& os = std::cout);
805 };
806 
807 //! \}
808 
810 
811 #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:328
virtual const char * type_name() const
return formatted type name to user
Definition: cmdline.h:224
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:405
const char * m_progname
argv[0] for usage.
Definition: cmdline.h:399
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:122
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:298
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
Definition: cmdline.h:578
virtual const char * type_name() const
return formatted type name to user
Definition: cmdline.h:156
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
Definition: cmdline.h:625
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
Definition: cmdline.h:756
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 ...
Definition: cmdline.h:667
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
Definition: cmdline.h:724
unsigned long long int uint64
Definition: types.h:39
std::vector< std::string > & m_dest
Definition: cmdline.h:347
virtual void print_value(std::ostream &os) const
format value to ostream
Definition: cmdline.h:340
virtual void print_value(std::ostream &os) const
format value to ostream
Definition: cmdline.h:139
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
Definition: cmdline.h:596
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:320
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 ...
Definition: cmdline.h:483
void set_verbose_process(bool verbose_process)
Set verbose processing of command line arguments.
Definition: cmdline.h:474
cmdline_parser()
Construct new command line parser.
Definition: cmdline.h:441
arglist_type m_optlist
list of options available
Definition: cmdline.h:389
virtual bool process(int &argc, const char *const *&argv)
parse signed integer using sscanf.
Definition: cmdline.h:160
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 ...
Definition: cmdline.h:590
virtual void print_value(std::ostream &os) const
format value to ostream
Definition: cmdline.h:240
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 ...
Definition: cmdline.h:608
virtual void print_value(std::ostream &os) const
format value to ostream
Definition: cmdline.h:372
void calc_param_max(const argument *arg)
update maximum formatting width for new parameter
Definition: cmdline.h:424
specialization of argument for SI/IEC suffixes byte size options or parameters
Definition: cmdline.h:282
virtual const char * type_name() const
return formatted type name to user
Definition: cmdline.h:258
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:251
virtual ~argument()
empty virtual destructor
Definition: cmdline.h:76
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:468
std::string m_keytype
option type description, e.g. &quot;&lt;#&gt;&quot; to indicate numbers
Definition: cmdline.h:56
void set_description(const std::string &description)
Set description of program, text will be wrapped.
Definition: cmdline.h:462
unsigned int m_linewrap
set line wrap length
Definition: cmdline.h:410
virtual bool process(int &argc, const char *const *&argv)
&quot;process&quot; string argument just by storing it in vector.
Definition: cmdline.h:364
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
Definition: cmdline.h:703
specialization of argument for double options or parameters
Definition: cmdline.h:211
virtual void print_value(std::ostream &os) const
format value to ostream
Definition: cmdline.h:206
specialization of argument for string options or parameters
Definition: cmdline.h:315
virtual void print_value(std::ostream &os) const
format value to ostream
Definition: cmdline.h:276
std::string option_text() const
return &#39;-s, –longkey [keytype]&#39;
Definition: cmdline.h:98
unsigned int uint32
Definition: types.h:37
virtual const char * type_name() const
return formatted type name to user
Definition: cmdline.h:129
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 ...
Definition: cmdline.h:746
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
Definition: cmdline.h:584
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
Definition: cmdline.h:554
virtual bool process(int &argc, const char *const *&argv)
parse byte size using SI/IEC parser from stxxl.
Definition: cmdline.h:262
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 ...
Definition: cmdline.h:714
std::vector< argument * > arglist_type
option and parameter list type
Definition: cmdline.h:386
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:149
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
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_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 ...
Definition: cmdline.h:602
std::string m_author
user set author of program, will be wrapped
Definition: cmdline.h:407
virtual void print_value(std::ostream &os) const
format value to ostream
Definition: cmdline.h:172
std::string param_text() const
return &#39;longkey [keytype]&#39;
Definition: cmdline.h:88
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
Definition: cmdline.h:519
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
std::string m_desc
longer description, which will be wrapped
Definition: cmdline.h:58
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 ...
Definition: cmdline.h:543
virtual const char * type_name() const
return formatted type name to user
Definition: cmdline.h:190
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 ...
Definition: cmdline.h:656
virtual void print_value(std::ostream &os) const
format value to ostream
Definition: cmdline.h:310
int m_opt_maxlong
formatting width for options, &#39;-s, –switch &lt;#&gt;&#39;
Definition: cmdline.h:394
virtual bool process(int &, const char *const *&)
&quot;process&quot; argument: just set to true, no argument is used.
Definition: cmdline.h:133
virtual bool process(int &argc, const char *const *&argv)
&quot;process&quot; string argument just by storing it.
Definition: cmdline.h:332
bool & m_dest
reference to boolean to set to true
Definition: cmdline.h:119
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:177
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 ...
Definition: cmdline.h:507
specialization of argument for integer options or parameters
Definition: cmdline.h:144
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
Definition: cmdline.h:565
void add_param_double(const std::string &name, double &dest, const std::string &desc)
add double parameter [name] with description and store to dest
Definition: cmdline.h:645
virtual bool process(int &argc, const char *const *&argv)
parse unsigned integer using sscanf.
Definition: cmdline.h:228
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
Definition: cmdline.h:613
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:182
specialization of argument for multiple string options or parameters
Definition: cmdline.h:345
arglist_type m_paramlist
list of parameters, both required and optional
Definition: cmdline.h:391
specialization of argument for SI/IEC suffixes byte size options or parameters
Definition: cmdline.h:246
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
Definition: cmdline.h:635
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 ...
Definition: cmdline.h:735
void calc_opt_max(const argument *arg)
update maximum formatting width for new option
Definition: cmdline.h:417
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
Definition: cmdline.h:677
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:287
virtual bool process(int &argc, const char *const *&argv)
parse unsigned integer using sscanf.
Definition: cmdline.h:194
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 ...
Definition: cmdline.h:531
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:350
virtual const char * type_name() const
return formatted type name to user
Definition: cmdline.h:294
bool m_verbose_process
verbose processing of arguments
Definition: cmdline.h:402
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 ...
Definition: cmdline.h:495
argument_double(char key, const std::string &longkey, const std::string &keytype, const std::string &desc, bool required, double &dest)
contructor filling most attributes
Definition: cmdline.h:216
specialization of argument for boolean flags (can only be set to true).
Definition: cmdline.h:116
virtual const char * type_name() const
return formatted type name to user
Definition: cmdline.h:360
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
Definition: cmdline.h:618
#define STXXL_END_NAMESPACE
Definition: namespace.h:17
~cmdline_parser()
Delete all added arguments.
Definition: cmdline.h:450
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
Definition: cmdline.h:768
int m_param_maxlong
formatting width for parameters, &#39;param &lt;#&gt;&#39;
Definition: cmdline.h:396
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.
Definition: cmdline.h:689