iRoCS Toolbox  1.1.0
CmdLine.hh
Go to the documentation of this file.
1 /**************************************************************************
2  *
3  * Copyright (C) 2005-2015 Olaf Ronneberger, Jörg Mechnich, Florian Pigorsch,
4  * Mario Emmenlauer, Thorsten Schmidt
5  *
6  * Image Analysis Lab, University of Freiburg, Germany
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  *
22  **************************************************************************/
23 
24 /**************************************************************************
25 ** Title: command line parser
26 ** $RCSfile$
27 ** $Revision: 4823 $$Name$
28 ** $Date: 2011-11-08 10:58:55 +0100 (Tue, 08 Nov 2011) $
29 ** Copyright: GPL $Author: tschmidt $
30 ** Description:
31 **
32 **
33 **
34 **-------------------------------------------------------------------------
35 **
36 ** $Log$
37 ** Revision 1.13 2005/06/09 07:54:33 tschmidt
38 ** - Changing enum names from bla to CmdLinebla
39 ** - Adding check for terminal width (I guess changed by emmenlau)
40 **
41 ** Revision 1.12 2003/01/31 02:52:28 mechnich
42 ** added XML output of all CmdLine arguments
43 **
44 ** Revision 1.11 2002/10/28 13:11:33 mechnich
45 ** made everything compile with gcc 3.2
46 ** removed promptUser() from CmdLine.*
47 **
48 ** Revision 1.10 2002/06/03 15:20:52 pigorsch
49 ** - removed UsageLevel
50 **
51 ** Revision 1.9 2002/05/14 08:16:10 pigorsch
52 ** - removed functions longPrefix() and shortPrefix()
53 **
54 ** Revision 1.8 2002/04/22 15:57:11 pigorsch
55 ** - added function replace()
56 ** - moved function basename() from .hh to.cc
57 **
58 ** Revision 1.7 2002/04/18 14:05:02 pigorsch
59 ** - removed #include "config.hh"
60 **
61 ** Revision 1.6 2002/04/18 14:01:00 pigorsch
62 ** - added some docu
63 **
64 ** Revision 1.5 2002/04/10 11:06:53 pigorsch
65 ** - fixed typo
66 **
67 ** Revision 1.4 2002/04/04 12:01:16 pigorsch
68 ** - moved error classes to Errors.hh
69 ** - added longname support
70 ** - added default help arguments
71 ** - added emtpy "mainpage" doxygen comment
72 **
73 ** Revision 1.3 2002/03/27 10:26:06 pigorsch
74 ** - fixed name / longname function headers
75 **
76 ** Revision 1.2 2002/03/27 10:24:19 pigorsch
77 ** - added long name support
78 **
79 ** Revision 1.1 2002/03/26 07:36:28 ronneber
80 ** restructuring for autoconf
81 **
82 ** Revision 1.1.1.1 2002/03/22 13:45:07 pigorsch
83 ** moved from polsoft repository
84 **
85 **
86 **
87 **************************************************************************/
88 
89 /**************************************************************************
90 **/
100 #ifndef CMDLINE_HH
101 #define CMDLINE_HH
102 
103 #ifdef HAVE_CONFIG_H
104 #include <config.hh>
105 #endif
106 
107 #include "ArgIter.hh"
108 #include "CmdArg.hh"
109 #include "CmdArgs.hh"
110 #include "Errors.hh"
111 
112 #include <iostream>
113 #include <list>
114 #include <string>
115 #include <cstddef>
116 
117 // For ioctls to find terminal width on linux
118 #ifdef linux
119 #include <sys/ioctl.h>
120 #endif
121 
122 
123 #define LEFTMARGIN 5
124 #define STDCOLS 79
125 #define MAXCOLS 120
126 
127 /*======================================================================*/
136 /*======================================================================*/
137 class CmdLine
138 {
139 public:
140  /*====================================================================*/
144  /*====================================================================*/
146  {
149  FLAG_NO_ABORT = 0x004,
150  FLAG_OPTS_FIRST = 0x008,
151  FLAG_QUIET = 0x080,
152  FLAG_ALL = 0x0FF
153  };
154 
155 
156  /*====================================================================*/
160  /*====================================================================*/
162  {
163  STATUS_NO_ERROR = 0x000,
173  };
174 
175 
176  /*====================================================================*/
180  /*====================================================================*/
182  {
187  STATE_ALL = 0x07
188  };
189 
190 
191  /*====================================================================*/
195  /*====================================================================*/
197  {
200  PARSE_WANT_VAL = 0x02,
202  PARSE_ALL = 0x07
203  };
204 
205 
206  /*====================================================================*/
215  /*====================================================================*/
216  CmdLine(
217  std::string name,
218  std::string longname,
219  bool help=true);
220 
221  /*====================================================================*/
235  /*====================================================================*/
236  CmdLine(
237  std::string name,
238  std::string longname,
239  bool help,
240  CmdArg* cmdarg1 ...);
241 
242 
243  /*====================================================================*/
247  /*====================================================================*/
248  virtual
249  ~CmdLine();
250 
251 
252  /*====================================================================*/
258  /*====================================================================*/
259  std::string
260  name() const;
261 
262 
263  /*====================================================================*/
269  /*====================================================================*/
270  void
271  name(const std::string& value);
272 
273 
274  /*====================================================================*/
280  /*====================================================================*/
281  std::string
282  longname() const;
283 
284 
285  /*====================================================================*/
291  /*====================================================================*/
292  void
293  longname(const std::string& value);
294 
295 
296  /*====================================================================*/
302  /*====================================================================*/
303  std::string
304  description() const;
305 
306 
307  /*====================================================================*/
313  /*====================================================================*/
314  void
315  description(const std::string& description);
316 
317 
318  /*====================================================================*/
328  /*====================================================================*/
329  CmdLine&
330  append(CmdArg* cmdarg);
331 
332 
333  /*====================================================================*/
343  /*====================================================================*/
344  CmdLine&
345  append(CmdArg& cmdarg);
346 
347 
348  /*====================================================================*/
356  /*====================================================================*/
357  std::ostream&
358  usage(std::ostream& os) const;
359 
360 
361  /*====================================================================*/
367  /*====================================================================*/
368  std::ostream&
369  usage() const;
370 
371 
372  /*====================================================================*/
380  /*====================================================================*/
381  std::ostream&
382  usageXML(std::ostream& os) const;
383 
384 
385  /*====================================================================*/
393  /*====================================================================*/
394  std::ostream&
395  usageHTML(std::ostream& os) const;
396 
397 
398  /*====================================================================*/
408  /*====================================================================*/
409  std::ostream&
410  usageLATEX(std::ostream& os) const;
411 
412 
413  /*====================================================================*/
419  /*====================================================================*/
421  status() const;
422 
423 
424  /*====================================================================*/
430  /*====================================================================*/
431  void
433 
434 
435  /*====================================================================*/
441  /*====================================================================*/
442  void
444 
445 
446  /*====================================================================*/
454  /*====================================================================*/
455  bool
457 
458 
459  /*====================================================================*/
465  /*====================================================================*/
467  state() const;
468 
469 
470  /*====================================================================*/
476  /*====================================================================*/
477  void
479 
480 
481  /*====================================================================*/
487  /*====================================================================*/
488  void
490 
491 
492  /*====================================================================*/
499  /*====================================================================*/
500  void
502 
503 
504  /*====================================================================*/
512  /*====================================================================*/
513  bool hasState(CmdLine::CmdLineState s) const;
514 
515 
516  /*====================================================================*/
522  /*====================================================================*/
524  flags() const;
525 
526 
527  /*====================================================================*/
533  /*====================================================================*/
534  void
536 
537 
538  /*====================================================================*/
544  /*====================================================================*/
545  void
547 
548 
549  /*====================================================================*/
555  /*====================================================================*/
556  void
558 
559 
560  /*====================================================================*/
568  /*====================================================================*/
569  bool hasFlag(CmdLine::CmdLineFlags f) const;
570 
571 
572  /*====================================================================*/
578  /*====================================================================*/
579  std::ostream&
580  error() const;
581 
582 
583  /*====================================================================*/
589  /*====================================================================*/
590  void
591  error(std::ostream& os);
592 
593 
594  /*====================================================================*/
605  /*====================================================================*/
607  parse(
608  ArgIter& argiter,
609  bool autoProcessing=true);
610 
611 
612  /*====================================================================*/
618  /*====================================================================*/
620  prologue();
621 
622 
623  /*====================================================================*/
631  /*====================================================================*/
633  parseArg(const char* arg);
634 
635 
636  /*====================================================================*/
644  /*====================================================================*/
646  epilogue();
647 
648 
649  /*====================================================================*/
655  /*====================================================================*/
656  unsigned int
657  nargsParsed() const;
658 
659 
660  /*====================================================================*/
669  /*====================================================================*/
670  CmdArg*
671  getArg(char shortName) const;
672 
673 
674  /*====================================================================*/
683  /*====================================================================*/
684  CmdArg*
685  getArg(const std::string& keyword) const;
686 
687 
688 private:
689  CmdLine::CmdLineParseState pParseState;
690  CmdLine::CmdLineState pState;
691  CmdLine::CmdLineFlags pFlags;
692  CmdLine::CmdLineStatus pStatus;
693  unsigned int pNArgsParsed;
694  std::string pName;
695  std::string pLongName;
696  std::string pDescription;
697  CmdArg* pMatchedArg;
698  std::list<CmdArg*> pArguments;
699  std::ostream* pErr;
703 
704 
705  /*====================================================================*/
709  /*====================================================================*/
710  enum CmdLineMatch
711  {
712  MATCH_NONE,
713  MATCH_PARTIAL,
714  MATCH_EXACT
715  };
716 
717  /*====================================================================*/
730  /*====================================================================*/
731  CmdLineMatch
732  static strmatch(
733  const std::string& src,
734  const std::string& attempt,
735  size_t len=0);
736 
737 
738  /*====================================================================*/
757  /*====================================================================*/
758  static void
759  strindent(
760  std::ostream& os,
761  const std::string& title,
762  unsigned int indent,
763  const std::string& text);
764 
765 
766  /*====================================================================*/
774  /*====================================================================*/
775  static std::string
776  basename(const std::string& filename);
777 
778 
779  /*====================================================================*/
783  /*====================================================================*/
784  CmdLine(const CmdLine&)
785  :pParseState(PARSE_START_STATE),
786  pState(STATE_START_STATE),
787  pFlags(FLAG_OPTS_FIRST),
788  pStatus(STATUS_NO_ERROR),
789  pNArgsParsed(0),
790  pName(""),
791  pLongName(""),
792  pDescription(""),
793  pMatchedArg(0),
794  pErr(0),
795  pUsageArg(0, "help", "Display usage message."),
796  pUsageHTMLArg(0, "usagehtml", "Display HTML usage message."),
797  pUsageXMLArg(0, "usagexml", "Display XML usage message.")
798  {};
799 
800 
801  /*====================================================================*/
805  /*====================================================================*/
806  CmdLine&
807  operator=(const CmdLine&)
808  {
809  return *this;
810  };
811 
812 
813  /*====================================================================*/
817  /*====================================================================*/
818  enum UsageSyntax
819  {
820  USAGE_SHORT_ONLY = 0,
821  USAGE_LONG_ONLY = 1,
822  USAGE_BOTH = 2
823  };
824 
825 
826  /*====================================================================*/
836  /*====================================================================*/
837  bool
838  handleArg(
839  CmdArg* cmdarg,
840  const char* arg,
841  const char*& endptr);
842 
843 
844  /*====================================================================*/
848  /*====================================================================*/
849  void
850  ck_need_val();
851 
852 
853  /*====================================================================*/
859  /*====================================================================*/
860  UsageSyntax
861  syntax() const;
862 
863 
864  /*====================================================================*/
871  /*====================================================================*/
873  missingArgs();
874 
875 
876  /*====================================================================*/
884  /*====================================================================*/
885  CmdArg*
886  matchShort(char optchar) const;
887 
888 
889  /*====================================================================*/
899  /*====================================================================*/
900  CmdArg*
901  matchLong(
902  const std::string& kwd,
903  ptrdiff_t len,
904  bool& is_ambiguous) const;
905 
906 
907  /*====================================================================*/
913  /*====================================================================*/
914  CmdArg*
915  matchPos() const;
916 
917  /*====================================================================*/
925  /*====================================================================*/
926  static bool
927  isShort(const char* s);
928 
929 
930  /*====================================================================*/
938  /*====================================================================*/
939  static bool
940  isLong(const char* s);
941 
942 
943  /*====================================================================*/
951  /*====================================================================*/
952  static bool
953  isEndOfOptions(const char* s);
954 
955 
956  /*====================================================================*/
966  /*====================================================================*/
968  parseShort(const char* arg);
969 
970 
971  /*====================================================================*/
981  /*====================================================================*/
983  parseLong(const char* arg);
984 
985 
986  /*====================================================================*/
997  /*====================================================================*/
999  parseValue(const char* arg);
1000 
1001 
1002  /*====================================================================*/
1011  /*====================================================================*/
1012  std::ostream&
1013  argError(
1014  const std::string& error_str,
1015  const CmdArg* cmdarg) const;
1016 
1017 
1018  /*====================================================================*/
1027  /*====================================================================*/
1028  unsigned int
1029  printSynopsis(
1030  UsageSyntax syntax,
1031  std::ostream& os) const;
1032 
1033 
1034  /*====================================================================*/
1042  /*====================================================================*/
1043  void
1044  printDescriptions(
1045  UsageSyntax syntax,
1046  std::ostream& os,
1047  unsigned int longest) const;
1048 
1049  /*====================================================================*/
1059  /*====================================================================*/
1060  static
1061  std::string
1062  replace(
1063  std::string s,
1064  const std::string& replacedText,
1065  const std::string& replacedBy);
1066 
1067 };
1068 
1069 #include "CmdLine.icc"
1070 
1071 #endif
void clearState(CmdLine::CmdLineState s=STATE_ALL)
Clear only the given state flags.
unsigned int nargsParsed() const
Find out the number of arguments parsed so far.
CmdLine::CmdLineState state() const
Return CmdLine&#39;s state.
A required argument value was not specified.
Definition: CmdLine.hh:165
An invalid value was specified for an arg.
Definition: CmdLine.hh:171
std::ostream & error() const
Return CmdLine&#39;s error output stream.
No options after positional parameters.
Definition: CmdLine.hh:150
std::string longname() const
Return previously specified program long name.
The CmdArg class is the abstract base class for all command line arguments.
Definition: CmdArg.hh:102
CmdLine::CmdLineFlags flags() const
Return CmdLine&#39;s flags.
std::ostream & usage() const
Print usage message on CmdLine&#39;s error output stream.
The last parsed CmdArg may optionally get a value.
Definition: CmdLine.hh:200
The last parsed CmdArg requires a value.
Definition: CmdLine.hh:201
CmdLine::CmdLineStatus status() const
Return CmdLine&#39;s status.
std::string description() const
Return previously specified program description.
Don&#39;t quit upon syntax error.
Definition: CmdLine.hh:149
An ambiguous keyword prefix was specified.
Definition: CmdLine.hh:168
CmdLine(std::string name, std::string longname, bool help=true)
Constructor.
std::ostream & usageLATEX(std::ostream &os) const
Print LATEX usage message on the given output stream.
The CmdLine class is a parser for command line arguments.
Definition: CmdLine.hh:137
std::ostream & usageHTML(std::ostream &os) const
Print HTML usage message on the given output stream.
CmdLine::CmdLineStatus parse(ArgIter &argiter, bool autoProcessing=true)
Parse a set of arguments.
bool hasState(CmdLine::CmdLineState s) const
Checks if the given state flags are set.
void setState(CmdLine::CmdLineState s)
Set only given state flags.
CmdLine::CmdLineStatus epilogue()
Perform the necessary post-processing.
Long options were used.
Definition: CmdLine.hh:186
std::string name() const
Return previously specified program name.
Short options were used.
Definition: CmdLine.hh:185
Don&#39;t print syntax error messages.
Definition: CmdLine.hh:151
CmdLine & append(CmdArg *cmdarg)
Append specified CmdArg to the list of CmdArgs.
Value needs to be in separate token.
Definition: CmdLine.hh:167
CmdArg * getArg(char shortName) const
Retrieve an argument based on its short name.
End of options (–) occured.
Definition: CmdLine.hh:184
An invalid keyword was specified.
Definition: CmdLine.hh:170
A required argument was not specified.
Definition: CmdLine.hh:164
Prompt the user for missing required args.
Definition: CmdLine.hh:148
bool hasStatus(CmdLine::CmdLineStatus s) const
Checks if the given status flags are set.
Ignore character-case for short-options.
Definition: CmdLine.hh:147
Value needs to be in same token.
Definition: CmdLine.hh:166
CmdLine::CmdLineStatus prologue()
Perform the necessary pre-processing.
CmdLine::CmdLineStatus parseArg(const char *arg)
Parse a single argument.
CmdLineState
State flags for CmdLine.
Definition: CmdLine.hh:181
The next token is required.
Definition: CmdLine.hh:199
CmdLineParseState
Parsing state flags for CmdLine.
Definition: CmdLine.hh:196
void clearFlags(CmdLine::CmdLineFlags f=FLAG_ALL)
Clear only the given flags.
The ArgIter class is an abstract class used by the CmdLine class for fetching command line arguments...
Definition: ArgIter.hh:73
void setStatus(CmdLine::CmdLineStatus s)
Set only given status flags.
Parsing of a CmdArg is completely finished.
Definition: CmdLine.hh:198
Too many positional args were specified.
Definition: CmdLine.hh:172
virtual ~CmdLine()
Destructor.
CmdLineFlags
Configuration flags for CmdLine.
Definition: CmdLine.hh:145
An invalid option was specified.
Definition: CmdLine.hh:169
bool hasFlag(CmdLine::CmdLineFlags f) const
Checks if the given flags are set.
std::ostream & usageXML(std::ostream &os) const
Print XML usage information on the given output stream.
void setFlags(CmdLine::CmdLineFlags f)
Set only the given flags.
CmdLineStatus
Status flags for CmdLine.
Definition: CmdLine.hh:161