00001 /* 00002 * 00003 * Copyright (C) 1998-2010, OFFIS e.V. 00004 * All rights reserved. See COPYRIGHT file for details. 00005 * 00006 * This software and supporting documentation were developed by 00007 * 00008 * OFFIS e.V. 00009 * R&D Division Health 00010 * Escherweg 2 00011 * D-26121 Oldenburg, Germany 00012 * 00013 * 00014 * Module: ofstd 00015 * 00016 * Author: Joerg Riesmeier 00017 * 00018 * Purpose: Handle command line arguments (Header) 00019 * 00020 * Last Update: $Author: joergr $ 00021 * Update Date: $Date: 2010-10-14 13:15:50 $ 00022 * CVS/RCS Revision: $Revision: 1.43 $ 00023 * Status: $State: Exp $ 00024 * 00025 * CVS/RCS Log at end of file 00026 * 00027 */ 00028 00029 00030 #ifndef OFCMDLN_H 00031 #define OFCMDLN_H 00032 00033 #include "dcmtk/config/osconfig.h" 00034 #include "dcmtk/ofstd/oftypes.h" 00035 #include "dcmtk/ofstd/oflist.h" 00036 #include "dcmtk/ofstd/ofstring.h" 00037 #include "dcmtk/ofstd/ofconsol.h" 00038 00039 #define INCLUDE_CSTDIO 00040 #include "dcmtk/ofstd/ofstdinc.h" 00041 00042 00043 /*--------------------* 00044 * type declaration * 00045 *--------------------*/ 00046 00048 typedef signed long OFCmdSignedInt; 00050 typedef unsigned long OFCmdUnsignedInt; 00052 typedef double OFCmdFloat; 00054 typedef OFString OFCmdString; 00055 00056 // necessary for MSVC5 :-/ 00057 typedef OFListIterator(OFString) OFListIterator_OFString; 00058 00059 00060 /*------------------------* 00061 * forward declarations * 00062 *------------------------*/ 00063 00064 struct OFCmdOption; 00065 struct OFCmdParamPos; 00066 00067 00068 /*----------------------* 00069 * struct declaration * 00070 *----------------------*/ 00071 00075 struct OFCmdParam 00076 { 00077 00080 enum E_ParamMode 00081 { 00083 PM_Mandatory, 00085 PM_Optional, 00087 PM_MultiMandatory, 00089 PM_MultiOptional 00090 }; 00091 00098 OFCmdParam(const char *param, 00099 const char *descr, 00100 const E_ParamMode mode) 00101 : ParamName(param), 00102 ParamDescription(descr), 00103 ParamMode(mode) 00104 { 00105 } 00106 00108 const OFString ParamName; 00110 const OFString ParamDescription; 00112 const E_ParamMode ParamMode; 00113 00114 private: 00115 00117 OFCmdParam& operator=(const OFCmdParam& arg); 00118 }; 00119 00120 00121 /*---------------------* 00122 * class declaration * 00123 *---------------------*/ 00124 00129 class OFCommandLine 00130 { 00131 00132 public: 00133 00134 // --- enumerations 00135 00138 enum E_ParseStatus 00139 { 00141 PS_Normal, 00143 PS_NoArguments, 00145 PS_ExclusiveOption, 00147 PS_UnknownOption, 00149 PS_MissingValue, 00151 PS_MissingParameter, 00153 PS_TooManyParameters, 00155 PS_CannotOpenCommandFile 00156 }; 00157 00158 00161 enum E_ValueStatus 00162 { 00164 VS_Normal, 00166 VS_Invalid, 00168 VS_NoMore, 00170 VS_Empty, 00172 VS_Underflow, 00174 VS_Overflow 00175 }; 00176 00177 00180 enum E_ParamValueStatus 00181 { 00183 PVS_Normal, 00185 PVS_Invalid, 00187 PVS_CantFind, 00189 PVS_Empty, 00191 PVS_Underflow, 00193 PVS_Overflow 00194 }; 00195 00198 enum E_FindOptionMode 00199 { 00201 FOM_Normal, 00203 FOM_First, 00205 FOM_Next, 00207 FOM_FirstFromLeft, 00209 FOM_NextFromLeft 00210 }; 00211 00212 00213 // --- constructor and destructor 00214 00217 OFCommandLine(); 00218 00221 virtual ~OFCommandLine(); 00222 00223 00224 // --- initialization 00225 00230 void setOptionChars(const char *chars); 00231 00237 void setOptionColumns(const int longCols, 00238 const int shortCols); 00239 00244 void setParamColumn(const int column); 00245 00258 OFBool addOption(const char *longOpt, 00259 const char *shortOpt, 00260 const int valueCount, 00261 const char *valueDescr, 00262 const char *optDescr, 00263 const int flags = 0); 00264 00275 OFBool addOption(const char *longOpt, 00276 const char *shortOpt, 00277 const char *optDescr, 00278 const int flags = 0); 00279 00291 OFBool addOption(const char *longOpt, 00292 const int valueCount, 00293 const char *valueDescr, 00294 const char *optDescr, 00295 const int flags = 0); 00296 00306 OFBool addOption(const char *longOpt, 00307 const char *optDescr, 00308 const int flags = 0); 00309 00317 void addGroup(const char *name, 00318 const int longCols = 0, 00319 const int shortCols = 0); 00320 00328 void addSubGroup(const char *name, 00329 const int longCols = 0, 00330 const int shortCols = 0); 00331 00341 OFBool addParam(const char *param, 00342 const char *descr, 00343 const OFCmdParam::E_ParamMode mode = OFCmdParam::PM_Mandatory); 00344 00353 OFBool addParam(const char *param, 00354 const OFCmdParam::E_ParamMode mode = OFCmdParam::PM_Mandatory); 00355 00356 00357 // --- get information 00358 00363 const OFString &getProgramName() const 00364 { 00365 return ProgramName; 00366 } 00367 00373 int getArgCount() const 00374 { 00375 return OFstatic_cast(int, ArgumentList.size()); 00376 } 00377 00383 OFBool gotoFirstArg(); 00384 00390 OFBool gotoNextArg(); 00391 00399 OFBool getCurrentArg(const char *&arg); 00400 00409 OFBool getCurrentArg(OFCmdString &arg); 00410 00416 int getParamCount() const 00417 { 00418 return OFstatic_cast(int, ParamPosList.size()); 00419 } 00420 00425 int getMinParamCount() const 00426 { 00427 return MinParamCount; 00428 } 00429 00434 int getMaxParamCount() const 00435 { 00436 return MaxParamCount; 00437 } 00438 00445 OFBool hasExclusiveOption() const 00446 { 00447 return ExclusiveOption; 00448 } 00449 00450 00451 // --- find/get parameter (parameter is an argument which is no option) 00452 00459 OFBool findParam(const int pos); 00460 00468 E_ParamValueStatus getParam(const int pos, 00469 OFCmdSignedInt &value); 00470 00480 E_ParamValueStatus getParamAndCheckMin(const int pos, 00481 OFCmdSignedInt &value, 00482 const OFCmdSignedInt low, 00483 const OFBool incl = OFTrue); 00484 00494 E_ParamValueStatus getParamAndCheckMinMax(const int pos, 00495 OFCmdSignedInt &value, 00496 const OFCmdSignedInt low, 00497 const OFCmdSignedInt high); 00498 00508 E_ParamValueStatus getParam(const int pos, 00509 OFCmdUnsignedInt &value); 00510 00522 E_ParamValueStatus getParamAndCheckMin(const int pos, 00523 OFCmdUnsignedInt &value, 00524 const OFCmdUnsignedInt low, 00525 const OFBool incl = OFTrue); 00526 00538 E_ParamValueStatus getParamAndCheckMinMax(const int pos, 00539 OFCmdUnsignedInt &value, 00540 const OFCmdUnsignedInt low, 00541 const OFCmdUnsignedInt high); 00542 00550 E_ParamValueStatus getParam(const int pos, 00551 OFCmdFloat &value); 00552 00562 E_ParamValueStatus getParamAndCheckMin(const int pos, 00563 OFCmdFloat &value, 00564 const OFCmdFloat low, 00565 const OFBool incl = OFTrue); 00566 00576 E_ParamValueStatus getParamAndCheckMinMax(const int pos, 00577 OFCmdFloat &value, 00578 const OFCmdFloat low, 00579 const OFCmdFloat high); 00580 00588 E_ParamValueStatus getParam(const int pos, 00589 const char *¶m); 00590 00598 E_ParamValueStatus getParam(const int pos, 00599 OFCmdString ¶m); 00600 00601 00602 // --- find/get option (option is an argument which starts with an option character, see above) 00603 00620 OFBool findOption(const char *longOpt, 00621 const signed int pos = 0, 00622 const E_FindOptionMode mode = FOM_Normal); 00623 00629 OFBool gotoFirstOption(); 00630 00636 OFBool gotoNextOption(); 00637 00646 OFBool getCurrentOption(const char *&opt); 00647 00656 OFBool getCurrentOption(OFCmdString &opt); 00657 00660 void beginOptionBlock(); 00661 00664 void endOptionBlock(); 00665 00666 00667 // --- get value (used for option values) 00668 00675 E_ValueStatus getValue(OFCmdSignedInt &value); 00676 00685 E_ValueStatus getValueAndCheckMin(OFCmdSignedInt &value, 00686 const OFCmdSignedInt low, 00687 const OFBool incl = OFTrue); 00688 00697 E_ValueStatus getValueAndCheckMinMax(OFCmdSignedInt &value, 00698 const OFCmdSignedInt low, 00699 const OFCmdSignedInt high); 00700 00707 E_ValueStatus getValue(OFCmdUnsignedInt &value); 00708 00717 E_ValueStatus getValueAndCheckMin(OFCmdUnsignedInt &value, 00718 const OFCmdUnsignedInt low, 00719 const OFBool incl = OFTrue); 00720 00729 E_ValueStatus getValueAndCheckMinMax(OFCmdUnsignedInt &value, 00730 const OFCmdUnsignedInt low, 00731 const OFCmdUnsignedInt high); 00732 00739 E_ValueStatus getValue(OFCmdFloat &value); 00740 00749 E_ValueStatus getValueAndCheckMin(OFCmdFloat &value, 00750 const OFCmdFloat low, 00751 const OFBool incl = OFTrue); 00752 00761 E_ValueStatus getValueAndCheckMinMax(OFCmdFloat &value, 00762 const OFCmdFloat low, 00763 const OFCmdFloat high); 00764 00771 E_ValueStatus getValue(const char *&value); 00772 00779 E_ValueStatus getValue(OFCmdString &value); 00780 00781 00782 // --- parsing command line 00783 00796 E_ParseStatus parseLine(int argCount, 00797 char *argValue[], 00798 const int flags = 0, 00799 const int startPos = 1); 00800 00801 00802 // --- get usage/status strings 00803 00809 void getSyntaxString(OFString &syntaxStr) const; 00810 00817 void getOptionString(OFString &optionStr) const; 00818 00824 void getParamString(OFString ¶mStr) const; 00825 00831 void getStatusString(const E_ParseStatus status, 00832 OFString &statusStr); 00833 00839 void getStatusString(const E_ParamValueStatus status, 00840 OFString &statusStr); 00841 00847 void getStatusString(const E_ValueStatus status, 00848 OFString &statusStr); 00849 00850 00851 // --- flags (used for method parseLine) 00852 00854 static const int PF_ExpandWildcards; 00856 static const int PF_NoCommandFiles; 00857 00858 // --- flags (used for method addOption) 00859 00861 static const int AF_Exclusive; 00863 static const int AF_Internal; 00866 static const int AF_NoWarning; 00867 00868 protected: 00869 00872 OFBool checkOption(const OFString &option, 00873 const OFBool mode = OFTrue) const; 00874 00877 OFBool findParam(int pos, 00878 OFListIterator(OFCmdParamPos *) &pos_iter); 00879 00882 const OFCmdOption *findCmdOption(const OFString &option) const; 00883 00886 void storeParameter(const OFString ¶m, 00887 const int directOption = 0); 00888 00891 E_ParseStatus parseCommandFile(const char *argValue, 00892 OFList<OFString> &argList); 00893 00896 int packColumnValues(int longCols, 00897 int shortCols) const; 00898 00901 void unpackColumnValues(const int value, 00902 unsigned int &longCols, 00903 unsigned int &shortCols) const; 00904 00905 #ifdef HAVE_WINDOWS_H 00906 00909 void expandWildcards(const OFString ¶m, 00910 int directOption = 0); 00911 #endif 00912 00915 E_ParseStatus checkParamCount(); 00916 00919 OFBool getLastArg(OFString &arg); 00920 00923 OFBool getMissingParam(OFString ¶m); 00924 00925 00926 private: 00927 00929 OFString ProgramName; 00930 00932 OFList<OFCmdOption *> ValidOptionList; 00934 OFList<OFCmdParam *> ValidParamList; 00935 00937 OFList<OFString> ArgumentList; 00939 OFListIterator(OFString) ArgumentIterator; 00940 00942 OFList<OFCmdParamPos *> ParamPosList; 00944 OFList<OFListIterator_OFString> OptionPosList; 00946 OFListIterator(OFListIterator_OFString) OptionPosIterator; 00948 OFListIterator(OFListIterator_OFString) OptionBlockIterator; 00949 00951 OFBool OptionBlockMode; 00953 OFString OptionChars; 00954 00956 OFBool ExclusiveOption; 00957 00959 int LongColumn; 00961 int ShortColumn; 00963 int ParamColumn; 00964 00966 int MinParamCount; 00968 int MaxParamCount; 00969 00971 OFCmdParam::E_ParamMode LastParamMode; 00972 00973 00974 private: 00975 00977 OFCommandLine &operator=(const OFCommandLine &obj); 00978 }; 00979 00980 00981 #endif 00982 00983 00984 /* 00985 * 00986 * CVS/RCS Log: 00987 * $Log: ofcmdln.h,v $ 00988 * Revision 1.43 2010-10-14 13:15:50 joergr 00989 * Updated copyright header. Added reference to COPYRIGHT file. 00990 * 00991 * Revision 1.42 2010-04-28 13:23:18 joergr 00992 * Added type cast to integer variable in order to avoid compiler warnings 00993 * reported by VisualStudio 2005. 00994 * 00995 * Revision 1.41 2009-12-23 12:15:51 joergr 00996 * Added support for getting the name of the program, i.e. the value of argv[0]. 00997 * 00998 * Revision 1.40 2009-06-04 09:53:00 joergr 00999 * Added new flag that can be used to avoid wrong warning messages (in debug 01000 * mode) that an option has possibly never been checked. 01001 * 01002 * Revision 1.39 2008-04-16 12:39:35 joergr 01003 * Added support for reverse search direction (left to right) to findOption(). 01004 * 01005 * Revision 1.38 2006/07/27 13:16:11 joergr 01006 * Changed parameter "exclusive" of method addOption() from type OFBool into an 01007 * integer parameter "flags". 01008 * Added addOption() flag for internal options that are not shown in the syntax 01009 * usage output. Prepended prefix "PF_" to parseLine() flags. 01010 * Option "--help" is no longer an exclusive option by default. 01011 * 01012 * Revision 1.37 2005/12/08 16:05:48 meichel 01013 * Changed include path schema for all DCMTK header files 01014 * 01015 * Revision 1.36 2003/12/05 13:59:33 joergr 01016 * Fixed problem with retrieving option values using the new iteration feature. 01017 * 01018 * Revision 1.35 2003/12/05 10:36:03 joergr 01019 * Added support for iterating over command line arguments and options. 01020 * Removed leading underscore characters from preprocessor symbols (reserved 01021 * symbols). Updated copyright date where appropriate. 01022 * 01023 * Revision 1.34 2003/06/12 13:19:58 joergr 01024 * Added support for so-called command files ("@filename") which can be used to 01025 * summarize command line options and parameter 01026 * 01027 * Revision 1.33 2003/05/20 08:42:39 joergr 01028 * Renamed parameters/variables "string" to avoid name clash with STL class. 01029 * 01030 * Revision 1.32 2002/12/09 13:04:41 joergr 01031 * Replaced tab characters by spaces. 01032 * 01033 * Revision 1.31 2002/12/05 13:48:21 joergr 01034 * Make sure that no warning on "unchecked command line options" is reported in 01035 * debug mode when an exclusive option is used. 01036 * 01037 * Revision 1.30 2002/11/27 11:23:04 meichel 01038 * Adapted module ofstd to use of new header file ofstdinc.h 01039 * 01040 * Revision 1.29 2002/09/19 08:30:20 joergr 01041 * Added general support for "exclusive" command line options besides "--help", 01042 * e.g. "--version". 01043 * 01044 * Revision 1.28 2001/11/09 15:46:42 joergr 01045 * Renamed some of the getValue/getParam methods to avoid ambiguities reported 01046 * by certain compilers. 01047 * 01048 * Revision 1.27 2001/08/23 16:05:52 meichel 01049 * Added private undefined copy assignment operators to avoid gcc warnings 01050 * 01051 * Revision 1.26 2001/06/01 15:51:32 meichel 01052 * Updated copyright header 01053 * 01054 * Revision 1.25 2000/10/10 12:01:20 meichel 01055 * Created/updated doc++ comments 01056 * 01057 * Revision 1.24 2000/04/14 15:17:11 meichel 01058 * Adapted all ofstd library classes to consistently use ofConsole for output. 01059 * 01060 * Revision 1.23 2000/03/08 16:36:01 meichel 01061 * Updated copyright header. 01062 * 01063 * Revision 1.22 2000/03/07 15:38:49 joergr 01064 * Changed behaviour of class OFConsoleApplication to support automatic 01065 * evaluation of "--help" option for command line application with no 01066 * mandatory parameter. 01067 * 01068 * Revision 1.21 2000/03/03 14:02:46 meichel 01069 * Implemented library support for redirecting error messages into memory 01070 * instead of printing them to stdout/stderr for GUI applications. 01071 * 01072 * Revision 1.20 2000/03/02 12:39:11 joergr 01073 * Fixed inconsistency: console applications with no or only optional 01074 * parameters could not be started without any command line argument 01075 * because this was always regarded identical with "--help" (print usage). 01076 * 01077 * Revision 1.19 1999/10/04 10:02:31 joergr 01078 * Fixed bug in wildcard expansion (concerning "direct option" feature). 01079 * 01080 * Revision 1.18 1999/09/13 16:36:54 joergr 01081 * Corrected bug in OFCommandLine::findOption() regarding the optional 01082 * parameter 'pos' specifying a reference command line parameter. 01083 * 01084 * Revision 1.17 1999/09/06 16:48:25 joergr 01085 * Added support to method 'findOption()' to detect options which are 01086 * 'direct' predecessors of an optionally specified reference parameter. 01087 * 01088 * Revision 1.16 1999/05/04 08:38:26 joergr 01089 * Added DOC++ comments to header file. 01090 * 01091 * Revision 1.15 1999/04/29 15:21:45 joergr 01092 * Removed debug code. 01093 * 01094 * Revision 1.13 1999/04/28 13:13:16 joergr 01095 * Removed some '#ifdef DEBUG' statements from header files to avoid 01096 * problems with inconsistent compilations. 01097 * 01098 * Revision 1.12 1999/04/27 17:46:05 joergr 01099 * Added some comments (DOC++ style). 01100 * 01101 * Revision 1.10 1999/04/26 16:32:47 joergr 01102 * Added support to define minimum width of short and long option columns. 01103 * Removed bug: empty parameters have always been interpreted as options. 01104 * Enhanced support of wildcard expansion under Windows (now very similar 01105 * to Unix shells). 01106 * 01107 * Revision 1.9 1999/03/24 16:59:38 joergr 01108 * Added optional parameters to define minimum width of columns for short and 01109 * long options in syntax output. 01110 * Changed optional integer parameter in method findOption to enum type. 01111 * 01112 * Revision 1.8 1999/02/08 11:51:38 joergr 01113 * Removed '#include <iostream.h>' from ofcmdln.h. 01114 * 01115 * Revision 1.7 1999/02/05 14:07:07 joergr 01116 * Added automatic wildcard expansion for Windows compilers. 01117 * 01118 * Revision 1.6 1998/12/02 18:44:25 joergr 01119 * Introduced test whether added options are correct (starting with defined 01120 * option character followed by a character which is no number). Changed 01121 * parse routine to distinguish between options (normally starting mit - or 01122 * +) and signed numbers which can be valid parameters. 01123 * 01124 * Revision 1.5 1998/12/02 17:38:53 joergr 01125 * Introduced new enum type used to indicate the status when converting 01126 * parameter values (similar to option values). Changed return value of 01127 * getParam() methods to this type. Added corresponding getStatusString() 01128 * method to convert status code to strings. 01129 * 01130 * Revision 1.4 1998/12/02 15:19:49 joergr 01131 * Added methods to convert parameters to signed/unsigned integers and 01132 * floats. Changed return value of existing getParam() methods. 01133 * 01134 * Revision 1.3 1998/11/30 12:30:18 joergr 01135 * Use lists of pointers (!) to internal data structures to avoid errors with 01136 * MSVC5 (operator '==' was not defined to compare structures). 01137 * 01138 * Revision 1.2 1998/11/30 12:27:02 joergr 01139 * Introduced additional type definition to avoid errors with MSVC5 when 01140 * using ListIterators of ListIterators (syntax problems?). 01141 * 01142 * Revision 1.1 1998/11/27 12:34:47 joergr 01143 * Added class to handle command line arguments. 01144 * 01145 * 01146 */