OFFIS DCMTK  Version 3.6.0
ofcmdln.h
1 /*
2  *
3  * Copyright (C) 1998-2010, OFFIS e.V.
4  * All rights reserved. See COPYRIGHT file for details.
5  *
6  * This software and supporting documentation were developed by
7  *
8  * OFFIS e.V.
9  * R&D Division Health
10  * Escherweg 2
11  * D-26121 Oldenburg, Germany
12  *
13  *
14  * Module: ofstd
15  *
16  * Author: Joerg Riesmeier
17  *
18  * Purpose: Handle command line arguments (Header)
19  *
20  * Last Update: $Author: joergr $
21  * Update Date: $Date: 2010-10-14 13:15:50 $
22  * CVS/RCS Revision: $Revision: 1.43 $
23  * Status: $State: Exp $
24  *
25  * CVS/RCS Log at end of file
26  *
27  */
28 
29 
30 #ifndef OFCMDLN_H
31 #define OFCMDLN_H
32 
33 #include "dcmtk/config/osconfig.h"
34 #include "dcmtk/ofstd/oftypes.h"
35 #include "dcmtk/ofstd/oflist.h"
36 #include "dcmtk/ofstd/ofstring.h"
37 #include "dcmtk/ofstd/ofconsol.h"
38 
39 #define INCLUDE_CSTDIO
40 #include "dcmtk/ofstd/ofstdinc.h"
41 
42 
43 /*--------------------*
44  * type declaration *
45  *--------------------*/
46 
48 typedef signed long OFCmdSignedInt;
50 typedef unsigned long OFCmdUnsignedInt;
52 typedef double OFCmdFloat;
54 typedef OFString OFCmdString;
55 
56 // necessary for MSVC5 :-/
57 typedef OFListIterator(OFString) OFListIterator_OFString;
58 
59 
60 /*------------------------*
61  * forward declarations *
62  *------------------------*/
63 
64 struct OFCmdOption;
65 struct OFCmdParamPos;
66 
67 
68 /*----------------------*
69  * struct declaration *
70  *----------------------*/
71 
75 struct OFCmdParam
76 {
77 
81  {
89  PM_MultiOptional
90  };
91 
98  OFCmdParam(const char *param,
99  const char *descr,
100  const E_ParamMode mode)
101  : ParamName(param),
102  ParamDescription(descr),
103  ParamMode(mode)
104  {
105  }
106 
113 
114 private:
115 
117  OFCmdParam& operator=(const OFCmdParam& arg);
118 };
119 
120 
121 /*---------------------*
122  * class declaration *
123  *---------------------*/
124 
130 {
131 
132  public:
133 
134  // --- enumerations
135 
139  {
156  };
157 
158 
162  {
175  };
176 
177 
181  {
194  };
195 
199  {
210  };
211 
212 
213  // --- constructor and destructor
214 
217  OFCommandLine();
218 
221  virtual ~OFCommandLine();
222 
223 
224  // --- initialization
225 
230  void setOptionChars(const char *chars);
231 
237  void setOptionColumns(const int longCols,
238  const int shortCols);
239 
244  void setParamColumn(const int column);
245 
258  OFBool addOption(const char *longOpt,
259  const char *shortOpt,
260  const int valueCount,
261  const char *valueDescr,
262  const char *optDescr,
263  const int flags = 0);
264 
275  OFBool addOption(const char *longOpt,
276  const char *shortOpt,
277  const char *optDescr,
278  const int flags = 0);
279 
291  OFBool addOption(const char *longOpt,
292  const int valueCount,
293  const char *valueDescr,
294  const char *optDescr,
295  const int flags = 0);
296 
306  OFBool addOption(const char *longOpt,
307  const char *optDescr,
308  const int flags = 0);
309 
317  void addGroup(const char *name,
318  const int longCols = 0,
319  const int shortCols = 0);
320 
328  void addSubGroup(const char *name,
329  const int longCols = 0,
330  const int shortCols = 0);
331 
341  OFBool addParam(const char *param,
342  const char *descr,
344 
353  OFBool addParam(const char *param,
355 
356 
357  // --- get information
358 
363  const OFString &getProgramName() const
364  {
365  return ProgramName;
366  }
367 
373  int getArgCount() const
374  {
375  return OFstatic_cast(int, ArgumentList.size());
376  }
377 
383  OFBool gotoFirstArg();
384 
390  OFBool gotoNextArg();
391 
399  OFBool getCurrentArg(const char *&arg);
400 
409  OFBool getCurrentArg(OFCmdString &arg);
410 
416  int getParamCount() const
417  {
418  return OFstatic_cast(int, ParamPosList.size());
419  }
420 
425  int getMinParamCount() const
426  {
427  return MinParamCount;
428  }
429 
434  int getMaxParamCount() const
435  {
436  return MaxParamCount;
437  }
438 
445  OFBool hasExclusiveOption() const
446  {
447  return ExclusiveOption;
448  }
449 
450 
451  // --- find/get parameter (parameter is an argument which is no option)
452 
459  OFBool findParam(const int pos);
460 
468  E_ParamValueStatus getParam(const int pos,
469  OFCmdSignedInt &value);
470 
481  OFCmdSignedInt &value,
482  const OFCmdSignedInt low,
483  const OFBool incl = OFTrue);
484 
495  OFCmdSignedInt &value,
496  const OFCmdSignedInt low,
497  const OFCmdSignedInt high);
498 
508  E_ParamValueStatus getParam(const int pos,
509  OFCmdUnsignedInt &value);
510 
523  OFCmdUnsignedInt &value,
524  const OFCmdUnsignedInt low,
525  const OFBool incl = OFTrue);
526 
539  OFCmdUnsignedInt &value,
540  const OFCmdUnsignedInt low,
541  const OFCmdUnsignedInt high);
542 
550  E_ParamValueStatus getParam(const int pos,
551  OFCmdFloat &value);
552 
563  OFCmdFloat &value,
564  const OFCmdFloat low,
565  const OFBool incl = OFTrue);
566 
577  OFCmdFloat &value,
578  const OFCmdFloat low,
579  const OFCmdFloat high);
580 
588  E_ParamValueStatus getParam(const int pos,
589  const char *&param);
590 
598  E_ParamValueStatus getParam(const int pos,
599  OFCmdString &param);
600 
601 
602  // --- find/get option (option is an argument which starts with an option character, see above)
603 
620  OFBool findOption(const char *longOpt,
621  const signed int pos = 0,
622  const E_FindOptionMode mode = FOM_Normal);
623 
629  OFBool gotoFirstOption();
630 
636  OFBool gotoNextOption();
637 
646  OFBool getCurrentOption(const char *&opt);
647 
656  OFBool getCurrentOption(OFCmdString &opt);
657 
660  void beginOptionBlock();
661 
664  void endOptionBlock();
665 
666 
667  // --- get value (used for option values)
668 
675  E_ValueStatus getValue(OFCmdSignedInt &value);
676 
685  E_ValueStatus getValueAndCheckMin(OFCmdSignedInt &value,
686  const OFCmdSignedInt low,
687  const OFBool incl = OFTrue);
688 
697  E_ValueStatus getValueAndCheckMinMax(OFCmdSignedInt &value,
698  const OFCmdSignedInt low,
699  const OFCmdSignedInt high);
700 
707  E_ValueStatus getValue(OFCmdUnsignedInt &value);
708 
717  E_ValueStatus getValueAndCheckMin(OFCmdUnsignedInt &value,
718  const OFCmdUnsignedInt low,
719  const OFBool incl = OFTrue);
720 
729  E_ValueStatus getValueAndCheckMinMax(OFCmdUnsignedInt &value,
730  const OFCmdUnsignedInt low,
731  const OFCmdUnsignedInt high);
732 
739  E_ValueStatus getValue(OFCmdFloat &value);
740 
749  E_ValueStatus getValueAndCheckMin(OFCmdFloat &value,
750  const OFCmdFloat low,
751  const OFBool incl = OFTrue);
752 
761  E_ValueStatus getValueAndCheckMinMax(OFCmdFloat &value,
762  const OFCmdFloat low,
763  const OFCmdFloat high);
764 
771  E_ValueStatus getValue(const char *&value);
772 
780 
781 
782  // --- parsing command line
783 
796  E_ParseStatus parseLine(int argCount,
797  char *argValue[],
798  const int flags = 0,
799  const int startPos = 1);
800 
801 
802  // --- get usage/status strings
803 
809  void getSyntaxString(OFString &syntaxStr) const;
810 
817  void getOptionString(OFString &optionStr) const;
818 
824  void getParamString(OFString &paramStr) const;
825 
831  void getStatusString(const E_ParseStatus status,
832  OFString &statusStr);
833 
839  void getStatusString(const E_ParamValueStatus status,
840  OFString &statusStr);
841 
847  void getStatusString(const E_ValueStatus status,
848  OFString &statusStr);
849 
850 
851  // --- flags (used for method parseLine)
852 
854  static const int PF_ExpandWildcards;
856  static const int PF_NoCommandFiles;
857 
858  // --- flags (used for method addOption)
859 
861  static const int AF_Exclusive;
863  static const int AF_Internal;
866  static const int AF_NoWarning;
867 
868  protected:
869 
872  OFBool checkOption(const OFString &option,
873  const OFBool mode = OFTrue) const;
874 
877  OFBool findParam(int pos,
878  OFListIterator(OFCmdParamPos *) &pos_iter);
879 
882  const OFCmdOption *findCmdOption(const OFString &option) const;
883 
886  void storeParameter(const OFString &param,
887  const int directOption = 0);
888 
891  E_ParseStatus parseCommandFile(const char *argValue,
892  OFList<OFString> &argList);
893 
896  int packColumnValues(int longCols,
897  int shortCols) const;
898 
901  void unpackColumnValues(const int value,
902  unsigned int &longCols,
903  unsigned int &shortCols) const;
904 
905 #ifdef HAVE_WINDOWS_H
906 
909  void expandWildcards(const OFString &param,
910  int directOption = 0);
911 #endif
912 
916 
919  OFBool getLastArg(OFString &arg);
920 
923  OFBool getMissingParam(OFString &param);
924 
925 
926  private:
927 
930 
935 
939  OFListIterator(OFString) ArgumentIterator;
940 
942  OFList<OFCmdParamPos *> ParamPosList;
944  OFList<OFListIterator_OFString> OptionPosList;
946  OFListIterator(OFListIterator_OFString) OptionPosIterator;
948  OFListIterator(OFListIterator_OFString) OptionBlockIterator;
949 
954 
957 
964 
969 
971  OFCmdParam::E_ParamMode LastParamMode;
972 
973 
974  private:
975 
977  OFCommandLine &operator=(const OFCommandLine &obj);
978 };
979 
980 
981 #endif
982 
983 
984 /*
985  *
986  * CVS/RCS Log:
987  * $Log: ofcmdln.h,v $
988  * Revision 1.43 2010-10-14 13:15:50 joergr
989  * Updated copyright header. Added reference to COPYRIGHT file.
990  *
991  * Revision 1.42 2010-04-28 13:23:18 joergr
992  * Added type cast to integer variable in order to avoid compiler warnings
993  * reported by VisualStudio 2005.
994  *
995  * Revision 1.41 2009-12-23 12:15:51 joergr
996  * Added support for getting the name of the program, i.e. the value of argv[0].
997  *
998  * Revision 1.40 2009-06-04 09:53:00 joergr
999  * Added new flag that can be used to avoid wrong warning messages (in debug
1000  * mode) that an option has possibly never been checked.
1001  *
1002  * Revision 1.39 2008-04-16 12:39:35 joergr
1003  * Added support for reverse search direction (left to right) to findOption().
1004  *
1005  * Revision 1.38 2006/07/27 13:16:11 joergr
1006  * Changed parameter "exclusive" of method addOption() from type OFBool into an
1007  * integer parameter "flags".
1008  * Added addOption() flag for internal options that are not shown in the syntax
1009  * usage output. Prepended prefix "PF_" to parseLine() flags.
1010  * Option "--help" is no longer an exclusive option by default.
1011  *
1012  * Revision 1.37 2005/12/08 16:05:48 meichel
1013  * Changed include path schema for all DCMTK header files
1014  *
1015  * Revision 1.36 2003/12/05 13:59:33 joergr
1016  * Fixed problem with retrieving option values using the new iteration feature.
1017  *
1018  * Revision 1.35 2003/12/05 10:36:03 joergr
1019  * Added support for iterating over command line arguments and options.
1020  * Removed leading underscore characters from preprocessor symbols (reserved
1021  * symbols). Updated copyright date where appropriate.
1022  *
1023  * Revision 1.34 2003/06/12 13:19:58 joergr
1024  * Added support for so-called command files ("@filename") which can be used to
1025  * summarize command line options and parameter
1026  *
1027  * Revision 1.33 2003/05/20 08:42:39 joergr
1028  * Renamed parameters/variables "string" to avoid name clash with STL class.
1029  *
1030  * Revision 1.32 2002/12/09 13:04:41 joergr
1031  * Replaced tab characters by spaces.
1032  *
1033  * Revision 1.31 2002/12/05 13:48:21 joergr
1034  * Make sure that no warning on "unchecked command line options" is reported in
1035  * debug mode when an exclusive option is used.
1036  *
1037  * Revision 1.30 2002/11/27 11:23:04 meichel
1038  * Adapted module ofstd to use of new header file ofstdinc.h
1039  *
1040  * Revision 1.29 2002/09/19 08:30:20 joergr
1041  * Added general support for "exclusive" command line options besides "--help",
1042  * e.g. "--version".
1043  *
1044  * Revision 1.28 2001/11/09 15:46:42 joergr
1045  * Renamed some of the getValue/getParam methods to avoid ambiguities reported
1046  * by certain compilers.
1047  *
1048  * Revision 1.27 2001/08/23 16:05:52 meichel
1049  * Added private undefined copy assignment operators to avoid gcc warnings
1050  *
1051  * Revision 1.26 2001/06/01 15:51:32 meichel
1052  * Updated copyright header
1053  *
1054  * Revision 1.25 2000/10/10 12:01:20 meichel
1055  * Created/updated doc++ comments
1056  *
1057  * Revision 1.24 2000/04/14 15:17:11 meichel
1058  * Adapted all ofstd library classes to consistently use ofConsole for output.
1059  *
1060  * Revision 1.23 2000/03/08 16:36:01 meichel
1061  * Updated copyright header.
1062  *
1063  * Revision 1.22 2000/03/07 15:38:49 joergr
1064  * Changed behaviour of class OFConsoleApplication to support automatic
1065  * evaluation of "--help" option for command line application with no
1066  * mandatory parameter.
1067  *
1068  * Revision 1.21 2000/03/03 14:02:46 meichel
1069  * Implemented library support for redirecting error messages into memory
1070  * instead of printing them to stdout/stderr for GUI applications.
1071  *
1072  * Revision 1.20 2000/03/02 12:39:11 joergr
1073  * Fixed inconsistency: console applications with no or only optional
1074  * parameters could not be started without any command line argument
1075  * because this was always regarded identical with "--help" (print usage).
1076  *
1077  * Revision 1.19 1999/10/04 10:02:31 joergr
1078  * Fixed bug in wildcard expansion (concerning "direct option" feature).
1079  *
1080  * Revision 1.18 1999/09/13 16:36:54 joergr
1081  * Corrected bug in OFCommandLine::findOption() regarding the optional
1082  * parameter 'pos' specifying a reference command line parameter.
1083  *
1084  * Revision 1.17 1999/09/06 16:48:25 joergr
1085  * Added support to method 'findOption()' to detect options which are
1086  * 'direct' predecessors of an optionally specified reference parameter.
1087  *
1088  * Revision 1.16 1999/05/04 08:38:26 joergr
1089  * Added DOC++ comments to header file.
1090  *
1091  * Revision 1.15 1999/04/29 15:21:45 joergr
1092  * Removed debug code.
1093  *
1094  * Revision 1.13 1999/04/28 13:13:16 joergr
1095  * Removed some '#ifdef DEBUG' statements from header files to avoid
1096  * problems with inconsistent compilations.
1097  *
1098  * Revision 1.12 1999/04/27 17:46:05 joergr
1099  * Added some comments (DOC++ style).
1100  *
1101  * Revision 1.10 1999/04/26 16:32:47 joergr
1102  * Added support to define minimum width of short and long option columns.
1103  * Removed bug: empty parameters have always been interpreted as options.
1104  * Enhanced support of wildcard expansion under Windows (now very similar
1105  * to Unix shells).
1106  *
1107  * Revision 1.9 1999/03/24 16:59:38 joergr
1108  * Added optional parameters to define minimum width of columns for short and
1109  * long options in syntax output.
1110  * Changed optional integer parameter in method findOption to enum type.
1111  *
1112  * Revision 1.8 1999/02/08 11:51:38 joergr
1113  * Removed '#include <iostream.h>' from ofcmdln.h.
1114  *
1115  * Revision 1.7 1999/02/05 14:07:07 joergr
1116  * Added automatic wildcard expansion for Windows compilers.
1117  *
1118  * Revision 1.6 1998/12/02 18:44:25 joergr
1119  * Introduced test whether added options are correct (starting with defined
1120  * option character followed by a character which is no number). Changed
1121  * parse routine to distinguish between options (normally starting mit - or
1122  * +) and signed numbers which can be valid parameters.
1123  *
1124  * Revision 1.5 1998/12/02 17:38:53 joergr
1125  * Introduced new enum type used to indicate the status when converting
1126  * parameter values (similar to option values). Changed return value of
1127  * getParam() methods to this type. Added corresponding getStatusString()
1128  * method to convert status code to strings.
1129  *
1130  * Revision 1.4 1998/12/02 15:19:49 joergr
1131  * Added methods to convert parameters to signed/unsigned integers and
1132  * floats. Changed return value of existing getParam() methods.
1133  *
1134  * Revision 1.3 1998/11/30 12:30:18 joergr
1135  * Use lists of pointers (!) to internal data structures to avoid errors with
1136  * MSVC5 (operator '==' was not defined to compare structures).
1137  *
1138  * Revision 1.2 1998/11/30 12:27:02 joergr
1139  * Introduced additional type definition to avoid errors with MSVC5 when
1140  * using ListIterators of ListIterators (syntax problems?).
1141  *
1142  * Revision 1.1 1998/11/27 12:34:47 joergr
1143  * Added class to handle command line arguments.
1144  *
1145  *
1146  */


Generated on Thu Dec 20 2012 for OFFIS DCMTK Version 3.6.0 by Doxygen 1.8.2