00001 /* 00002 * 00003 * Copyright (C) 1998-2005, OFFIS 00004 * 00005 * This software and supporting documentation were developed by 00006 * 00007 * Kuratorium OFFIS e.V. 00008 * Healthcare Information and Communication Systems 00009 * Escherweg 2 00010 * D-26121 Oldenburg, Germany 00011 * 00012 * THIS SOFTWARE IS MADE AVAILABLE, AS IS, AND OFFIS MAKES NO WARRANTY 00013 * REGARDING THE SOFTWARE, ITS PERFORMANCE, ITS MERCHANTABILITY OR 00014 * FITNESS FOR ANY PARTICULAR USE, FREEDOM FROM ANY COMPUTER DISEASES OR 00015 * ITS CONFORMITY TO ANY SPECIFICATION. THE ENTIRE RISK AS TO QUALITY AND 00016 * PERFORMANCE OF THE SOFTWARE IS WITH THE USER. 00017 * 00018 * Module: ofstd 00019 * 00020 * Author: Joerg Riesmeier 00021 * 00022 * Purpose: Handle command line arguments (Header) 00023 * 00024 * Last Update: $Author: meichel $ 00025 * Update Date: $Date: 2005/12/08 16:05:48 $ 00026 * CVS/RCS Revision: $Revision: 1.37 $ 00027 * Status: $State: Exp $ 00028 * 00029 * CVS/RCS Log at end of file 00030 * 00031 */ 00032 00033 00034 #ifndef OFCMDLN_H 00035 #define OFCMDLN_H 00036 00037 #include "dcmtk/config/osconfig.h" 00038 #include "dcmtk/ofstd/oftypes.h" 00039 #include "dcmtk/ofstd/oflist.h" 00040 #include "dcmtk/ofstd/ofstring.h" 00041 #include "dcmtk/ofstd/ofconsol.h" 00042 00043 #define INCLUDE_CSTDIO 00044 #include "dcmtk/ofstd/ofstdinc.h" 00045 00046 00047 /*--------------------* 00048 * type declaration * 00049 *--------------------*/ 00050 00052 typedef signed long OFCmdSignedInt; 00054 typedef unsigned long OFCmdUnsignedInt; 00056 typedef double OFCmdFloat; 00058 typedef OFString OFCmdString; 00059 00060 // necessary for MSVC5 :-/ 00061 typedef OFListIterator(OFString) OFListIterator_OFString; 00062 00063 00064 /*------------------------* 00065 * forward declarations * 00066 *------------------------*/ 00067 00068 struct OFCmdOption; 00069 struct OFCmdParamPos; 00070 00071 00072 /*----------------------* 00073 * struct declaration * 00074 *----------------------*/ 00075 00079 struct OFCmdParam 00080 { 00081 00084 enum E_ParamMode 00085 { 00087 PM_Mandatory, 00089 PM_Optional, 00091 PM_MultiMandatory, 00093 PM_MultiOptional 00094 }; 00095 00102 OFCmdParam(const char *param, 00103 const char *descr, 00104 const E_ParamMode mode) 00105 : ParamName(param), 00106 ParamDescription(descr), 00107 ParamMode(mode) 00108 { 00109 } 00110 00112 const OFString ParamName; 00114 const OFString ParamDescription; 00116 const E_ParamMode ParamMode; 00117 00118 private: 00119 00121 OFCmdParam& operator=(const OFCmdParam& arg); 00122 }; 00123 00124 00125 /*---------------------* 00126 * class declaration * 00127 *---------------------*/ 00128 00133 class OFCommandLine 00134 { 00135 00136 public: 00137 00138 // --- enumerations 00139 00142 enum E_ParseStatus 00143 { 00145 PS_Normal, 00147 PS_NoArguments, 00149 PS_UnknownOption, 00151 PS_MissingValue, 00153 PS_MissingParameter, 00155 PS_TooManyParameters, 00157 PS_CannotOpenCommandFile 00158 }; 00159 00160 00163 enum E_ValueStatus 00164 { 00166 VS_Normal, 00168 VS_Invalid, 00170 VS_NoMore, 00172 VS_Empty, 00174 VS_Underflow, 00176 VS_Overflow 00177 }; 00178 00179 00182 enum E_ParamValueStatus 00183 { 00185 PVS_Normal, 00187 PVS_Invalid, 00189 PVS_CantFind, 00191 PVS_Empty, 00193 PVS_Underflow, 00195 PVS_Overflow 00196 }; 00197 00200 enum E_FindOptionMode 00201 { 00203 FOM_Normal, 00205 FOM_First, 00207 FOM_Next 00208 }; 00209 00210 00211 // --- constructor and destructor 00212 00215 OFCommandLine(); 00216 00219 virtual ~OFCommandLine(); 00220 00221 00222 // --- initialization 00223 00228 void setOptionChars(const char *chars); 00229 00235 void setOptionColumns(const int longCols, 00236 const int shortCols); 00237 00242 void setParamColumn(const int column); 00243 00258 OFBool addOption(const char *longOpt, 00259 const char *shortOpt, 00260 const int valueCount, 00261 const char *valueDescr, 00262 const char *optDescr, 00263 const OFBool exclusive = OFFalse); 00264 00277 OFBool addOption(const char *longOpt, 00278 const char *shortOpt, 00279 const char *optDescr, 00280 const OFBool exclusive = OFFalse); 00281 00295 OFBool addOption(const char *longOpt, 00296 const int valueCount, 00297 const char *valueDescr, 00298 const char *optDescr, 00299 const OFBool exclusive = OFFalse); 00300 00312 OFBool addOption(const char *longOpt, 00313 const char *optDescr, 00314 const OFBool exclusive = OFFalse); 00315 00323 void addGroup(const char *name, 00324 const int longCols = 0, 00325 const int shortCols = 0); 00326 00334 void addSubGroup(const char *name, 00335 const int longCols = 0, 00336 const int shortCols = 0); 00337 00347 OFBool addParam(const char *param, 00348 const char *descr, 00349 const OFCmdParam::E_ParamMode mode = OFCmdParam::PM_Mandatory); 00350 00359 OFBool addParam(const char *param, 00360 const OFCmdParam::E_ParamMode mode = OFCmdParam::PM_Mandatory); 00361 00362 00363 // --- get information 00364 00370 int getArgCount() const 00371 { 00372 return ArgumentList.size(); 00373 } 00374 00380 OFBool gotoFirstArg(); 00381 00387 OFBool gotoNextArg(); 00388 00396 OFBool getCurrentArg(const char *&arg); 00397 00406 OFBool getCurrentArg(OFCmdString &arg); 00407 00413 int getParamCount() const 00414 { 00415 return ParamPosList.size(); 00416 } 00417 00422 int getMinParamCount() const 00423 { 00424 return MinParamCount; 00425 } 00426 00431 int getMaxParamCount() const 00432 { 00433 return MaxParamCount; 00434 } 00435 00442 OFBool hasExclusiveOption() const 00443 { 00444 return ExclusiveOption; 00445 } 00446 00447 00448 // --- find/get parameter (parameter is an argument which is no option) 00449 00456 OFBool findParam(const int pos); 00457 00458 00466 E_ParamValueStatus getParam(const int pos, 00467 OFCmdSignedInt &value); 00468 00478 E_ParamValueStatus getParamAndCheckMin(const int pos, 00479 OFCmdSignedInt &value, 00480 const OFCmdSignedInt low, 00481 const OFBool incl = OFTrue); 00482 00492 E_ParamValueStatus getParamAndCheckMinMax(const int pos, 00493 OFCmdSignedInt &value, 00494 const OFCmdSignedInt low, 00495 const OFCmdSignedInt high); 00496 00506 E_ParamValueStatus getParam(const int pos, 00507 OFCmdUnsignedInt &value); 00508 00520 E_ParamValueStatus getParamAndCheckMin(const int pos, 00521 OFCmdUnsignedInt &value, 00522 const OFCmdUnsignedInt low, 00523 const OFBool incl = OFTrue); 00524 00536 E_ParamValueStatus getParamAndCheckMinMax(const int pos, 00537 OFCmdUnsignedInt &value, 00538 const OFCmdUnsignedInt low, 00539 const OFCmdUnsignedInt high); 00540 00548 E_ParamValueStatus getParam(const int pos, 00549 OFCmdFloat &value); 00550 00560 E_ParamValueStatus getParamAndCheckMin(const int pos, 00561 OFCmdFloat &value, 00562 const OFCmdFloat low, 00563 const OFBool incl = OFTrue); 00564 00574 E_ParamValueStatus getParamAndCheckMinMax(const int pos, 00575 OFCmdFloat &value, 00576 const OFCmdFloat low, 00577 const OFCmdFloat high); 00578 00586 E_ParamValueStatus getParam(const int pos, 00587 const char *¶m); 00588 00596 E_ParamValueStatus getParam(const int pos, 00597 OFCmdString ¶m); 00598 00599 00600 // --- find/get option (option is an argument which starts with an option character, see above) 00601 00616 OFBool findOption(const char *longOpt, 00617 const signed int pos = 0, 00618 const E_FindOptionMode mode = FOM_Normal); 00619 00625 OFBool gotoFirstOption(); 00626 00632 OFBool gotoNextOption(); 00633 00642 OFBool getCurrentOption(const char *&opt); 00643 00652 OFBool getCurrentOption(OFCmdString &opt); 00653 00656 void beginOptionBlock(); 00657 00660 void endOptionBlock(); 00661 00662 00663 // --- get value (used for option values) 00664 00671 E_ValueStatus getValue(OFCmdSignedInt &value); 00672 00681 E_ValueStatus getValueAndCheckMin(OFCmdSignedInt &value, 00682 const OFCmdSignedInt low, 00683 const OFBool incl = OFTrue); 00684 00693 E_ValueStatus getValueAndCheckMinMax(OFCmdSignedInt &value, 00694 const OFCmdSignedInt low, 00695 const OFCmdSignedInt high); 00696 00703 E_ValueStatus getValue(OFCmdUnsignedInt &value); 00704 00713 E_ValueStatus getValueAndCheckMin(OFCmdUnsignedInt &value, 00714 const OFCmdUnsignedInt low, 00715 const OFBool incl = OFTrue); 00716 00725 E_ValueStatus getValueAndCheckMinMax(OFCmdUnsignedInt &value, 00726 const OFCmdUnsignedInt low, 00727 const OFCmdUnsignedInt high); 00728 00735 E_ValueStatus getValue(OFCmdFloat &value); 00736 00745 E_ValueStatus getValueAndCheckMin(OFCmdFloat &value, 00746 const OFCmdFloat low, 00747 const OFBool incl = OFTrue); 00748 00757 E_ValueStatus getValueAndCheckMinMax(OFCmdFloat &value, 00758 const OFCmdFloat low, 00759 const OFCmdFloat high); 00760 00767 E_ValueStatus getValue(const char *&value); 00768 00775 E_ValueStatus getValue(OFCmdString &value); 00776 00777 00778 // --- parsing command line 00779 00792 E_ParseStatus parseLine(int argCount, 00793 char *argValue[], 00794 const int flags = 0, 00795 const int startPos = 1); 00796 00797 00798 // --- get usage/status strings 00799 00805 void getSyntaxString(OFString &syntaxStr) const; 00806 00813 void getOptionString(OFString &optionStr) const; 00814 00820 void getParamString(OFString ¶mStr) const; 00821 00827 void getStatusString(const E_ParseStatus status, 00828 OFString &statusStr); 00829 00835 void getStatusString(const E_ParamValueStatus status, 00836 OFString &statusStr); 00837 00843 void getStatusString(const E_ValueStatus status, 00844 OFString &statusStr); 00845 00846 00847 // --- flags (used for method parseLine) 00848 00850 static const int ExpandWildcards; 00852 static const int NoCommandFiles; 00853 00854 00855 protected: 00856 00859 OFBool checkOption(const OFString &option, 00860 const OFBool mode = OFTrue) const; 00861 00864 OFBool findParam(int pos, 00865 OFListIterator(OFCmdParamPos *) &pos_iter); 00866 00869 const OFCmdOption *findCmdOption(const OFString &option) const; 00870 00873 void storeParameter(const OFString ¶m, 00874 const int directOption = 0); 00875 00878 E_ParseStatus parseCommandFile(const char *argValue, 00879 OFList<OFString> &argList); 00880 00883 int packColumnValues(int longCols, 00884 int shortCols) const; 00885 00888 void unpackColumnValues(const int value, 00889 unsigned int &longCols, 00890 unsigned int &shortCols) const; 00891 00892 #ifdef HAVE_WINDOWS_H 00893 00896 void expandWildcards(const OFString ¶m, 00897 int directOption = 0); 00898 #endif 00899 00902 E_ParseStatus checkParamCount(); 00903 00906 OFBool getLastArg(OFString &arg); 00907 00910 OFBool getMissingParam(OFString ¶m); 00911 00912 00913 private: 00914 00916 OFList<OFCmdOption *> ValidOptionList; 00918 OFList<OFCmdParam *> ValidParamList; 00919 00921 OFList<OFString> ArgumentList; 00923 OFListIterator(OFString) ArgumentIterator; 00924 00926 OFList<OFCmdParamPos *> ParamPosList; 00928 OFList<OFListIterator_OFString> OptionPosList; 00930 OFListIterator(OFListIterator_OFString) OptionPosIterator; 00932 OFListIterator(OFListIterator_OFString) OptionBlockIterator; 00933 00935 OFBool OptionBlockMode; 00937 OFString OptionChars; 00938 00940 OFBool ExclusiveOption; 00941 00943 int LongColumn; 00945 int ShortColumn; 00947 int ParamColumn; 00948 00950 int MinParamCount; 00952 int MaxParamCount; 00953 00955 OFCmdParam::E_ParamMode LastParamMode; 00956 00957 00958 private: 00959 00961 OFCommandLine &operator=(const OFCommandLine &obj); 00962 }; 00963 00964 00965 #endif 00966 00967 00968 /* 00969 * 00970 * CVS/RCS Log: 00971 * $Log: ofcmdln.h,v $ 00972 * Revision 1.37 2005/12/08 16:05:48 meichel 00973 * Changed include path schema for all DCMTK header files 00974 * 00975 * Revision 1.36 2003/12/05 13:59:33 joergr 00976 * Fixed problem with retrieving option values using the new iteration feature. 00977 * 00978 * Revision 1.35 2003/12/05 10:36:03 joergr 00979 * Added support for iterating over command line arguments and options. 00980 * Removed leading underscore characters from preprocessor symbols (reserved 00981 * symbols). Updated copyright date where appropriate. 00982 * 00983 * Revision 1.34 2003/06/12 13:19:58 joergr 00984 * Added support for so-called command files ("@filename") which can be used to 00985 * summarize command line options and parameter 00986 * 00987 * Revision 1.33 2003/05/20 08:42:39 joergr 00988 * Renamed parameters/variables "string" to avoid name clash with STL class. 00989 * 00990 * Revision 1.32 2002/12/09 13:04:41 joergr 00991 * Replaced tab characters by spaces. 00992 * 00993 * Revision 1.31 2002/12/05 13:48:21 joergr 00994 * Make sure that no warning on "unchecked command line options" is reported in 00995 * debug mode when an exclusive option is used. 00996 * 00997 * Revision 1.30 2002/11/27 11:23:04 meichel 00998 * Adapted module ofstd to use of new header file ofstdinc.h 00999 * 01000 * Revision 1.29 2002/09/19 08:30:20 joergr 01001 * Added general support for "exclusive" command line options besides "--help", 01002 * e.g. "--version". 01003 * 01004 * Revision 1.28 2001/11/09 15:46:42 joergr 01005 * Renamed some of the getValue/getParam methods to avoid ambiguities reported 01006 * by certain compilers. 01007 * 01008 * Revision 1.27 2001/08/23 16:05:52 meichel 01009 * Added private undefined copy assignment operators to avoid gcc warnings 01010 * 01011 * Revision 1.26 2001/06/01 15:51:32 meichel 01012 * Updated copyright header 01013 * 01014 * Revision 1.25 2000/10/10 12:01:20 meichel 01015 * Created/updated doc++ comments 01016 * 01017 * Revision 1.24 2000/04/14 15:17:11 meichel 01018 * Adapted all ofstd library classes to consistently use ofConsole for output. 01019 * 01020 * Revision 1.23 2000/03/08 16:36:01 meichel 01021 * Updated copyright header. 01022 * 01023 * Revision 1.22 2000/03/07 15:38:49 joergr 01024 * Changed behaviour of class OFConsoleApplication to support automatic 01025 * evaluation of "--help" option for command line application with no 01026 * mandatory parameter. 01027 * 01028 * Revision 1.21 2000/03/03 14:02:46 meichel 01029 * Implemented library support for redirecting error messages into memory 01030 * instead of printing them to stdout/stderr for GUI applications. 01031 * 01032 * Revision 1.20 2000/03/02 12:39:11 joergr 01033 * Fixed inconsistency: console applications with no or only optional 01034 * parameters could not be started without any command line argument 01035 * because this was always regarded identical with "--help" (print usage). 01036 * 01037 * Revision 1.19 1999/10/04 10:02:31 joergr 01038 * Fixed bug in wildcard expansion (concerning "direct option" feature). 01039 * 01040 * Revision 1.18 1999/09/13 16:36:54 joergr 01041 * Corrected bug in OFCommandLine::findOption() regarding the optional 01042 * parameter 'pos' specifying a reference command line parameter. 01043 * 01044 * Revision 1.17 1999/09/06 16:48:25 joergr 01045 * Added support to method 'findOption()' to detect options which are 01046 * 'direct' predecessors of an optionally specified reference parameter. 01047 * 01048 * Revision 1.16 1999/05/04 08:38:26 joergr 01049 * Added DOC++ comments to header file. 01050 * 01051 * Revision 1.15 1999/04/29 15:21:45 joergr 01052 * Removed debug code. 01053 * 01054 * Revision 1.13 1999/04/28 13:13:16 joergr 01055 * Removed some '#ifdef DEBUG' statements from header files to avoid 01056 * problems with inconsistent compilations. 01057 * 01058 * Revision 1.12 1999/04/27 17:46:05 joergr 01059 * Added some comments (DOC++ style). 01060 * 01061 * Revision 1.10 1999/04/26 16:32:47 joergr 01062 * Added support to define minimum width of short and long option columns. 01063 * Removed bug: empty parameters have always been interpreted as options. 01064 * Enhanced support of wildcard expansion under Windows (now very similar 01065 * to Unix shells). 01066 * 01067 * Revision 1.9 1999/03/24 16:59:38 joergr 01068 * Added optional parameters to define minimum width of columns for short and 01069 * long options in syntax output. 01070 * Changed optional integer parameter in method findOption to enum type. 01071 * 01072 * Revision 1.8 1999/02/08 11:51:38 joergr 01073 * Removed '#include <iostream.h>' from ofcmdln.h. 01074 * 01075 * Revision 1.7 1999/02/05 14:07:07 joergr 01076 * Added automatic wildcard expansion for Windows compilers. 01077 * 01078 * Revision 1.6 1998/12/02 18:44:25 joergr 01079 * Introduced test whether added options are correct (starting with defined 01080 * option character followed by a character which is no number). Changed 01081 * parse routine to distinguish between options (normally starting mit - or 01082 * +) and signed numbers which can be valid parameters. 01083 * 01084 * Revision 1.5 1998/12/02 17:38:53 joergr 01085 * Introduced new enum type used to indicate the status when converting 01086 * parameter values (similar to option values). Changed return value of 01087 * getParam() methods to this type. Added corresponding getStatusString() 01088 * method to convert status code to strings. 01089 * 01090 * Revision 1.4 1998/12/02 15:19:49 joergr 01091 * Added methods to convert parameters to signed/unsigned integers and 01092 * floats. Changed return value of existing getParam() methods. 01093 * 01094 * Revision 1.3 1998/11/30 12:30:18 joergr 01095 * Use lists of pointers (!) to internal data structures to avoid errors with 01096 * MSVC5 (operator '==' was not defined to compare structures). 01097 * 01098 * Revision 1.2 1998/11/30 12:27:02 joergr 01099 * Introduced additional type definition to avoid errors with MSVC5 when 01100 * using ListIterators of ListIterators (syntax problems?). 01101 * 01102 * Revision 1.1 1998/11/27 12:34:47 joergr 01103 * Added class to handle command line arguments. 01104 * 01105 * 01106 */