00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "TArgument.hh"
00020 #include "TRegularExpression.hh"
00021
00022 static const Tsize_t _buflen = 127;
00023 static Tchar _buf[ _buflen + 1 ];
00024
00025 TArgument::TArgument( Tint argc, Tchar** argv, const Tstring& usage )
00026 : theNumberOfOriginalArguments( argc ),
00027 theOriginalArguments( argv ),
00028 theOptionTable(),
00029 theOptionMap(),
00030 theProgramName( basename( Tstring( theOriginalArguments[ 0 ] ) ) ),
00031 theDirectoryName( dirname( Tstring( theOriginalArguments[ 0 ] ) ) ),
00032 theElements(),
00033 theNumberOfElements( 0 ),
00034 theUsage( usage )
00035 {
00036 if ( usage.empty() || usage.size() == 0 ) {
00037 Tostrstream strbuf( _buf, _buflen );
00038 strbuf << "Usage: " + theProgramName << " [OPTION]... [FILE]..." << Tendl;
00039 strbuf << Tendl;
00040 strbuf << "Mandatory arguments to long options are mandatory for short options too.";
00041 theUsage = strbuf.str();
00042 }
00043
00044 Analyse();
00045
00046 if ( theOptionMap.HasOption( "help" ) || theOptionMap.HasOption( "h" ) ) {
00047 ShowHelp();
00048 }
00049 if ( theOptionMap.HasOption( "version" ) || theOptionMap.HasOption( "v" ) ) {
00050 ShowVersion();
00051 }
00052 }
00053
00054 TArgument::TArgument( Tint argc, Tchar** argv, const TOptionTable& table, const Tstring& usage )
00055 : theNumberOfOriginalArguments( argc ),
00056 theOriginalArguments( argv ),
00057 theOptionTable( table ),
00058 theOptionMap(),
00059 theProgramName( basename( Tstring( theOriginalArguments[ 0 ] ) ) ),
00060 theDirectoryName( dirname( Tstring( theOriginalArguments[ 0 ] ) ) ),
00061 theElements(),
00062 theNumberOfElements( 0 ),
00063 theUsage( usage )
00064 {
00065 if ( usage.empty() || usage.size() == 0 ) {
00066 Tostrstream strbuf( _buf, _buflen );
00067 strbuf << "Usage: " + theProgramName << " [OPTION]... [FILE]..." << Tendl;
00068 strbuf << Tendl;
00069 strbuf << "Mandatory arguments to long options are mandatory for short options too.";
00070 theUsage = strbuf.str();
00071 }
00072
00073 Analyse();
00074
00075 if ( theOptionMap.HasOption( "help" ) || theOptionMap.HasOption( "h" ) ) {
00076 ShowHelp();
00077 }
00078 if ( theOptionMap.HasOption( "version" ) || theOptionMap.HasOption( "v" ) ) {
00079 ShowVersion();
00080 }
00081 }
00082
00083 TArgument::~TArgument()
00084 {;}
00085
00086 Tvoid TArgument::Analyse()
00087 {
00088 TOptionList oplist = theOptionTable.GetOptionList();
00089 Tsize_t listlen = oplist.size();
00090 struct option* longop = new option[ listlen + 1 ];
00091 Tstring shortop;
00092
00093
00094 for ( Tsize_t i = 0; i < listlen; i ++ ) {
00095 Tstring longbuf = oplist[ i ].GetLongOption();
00096 Tstring shortbuf = oplist[ i ].GetShortOption();
00097 Tint argstyle = oplist[ i ].GetArgumentStyle();
00098 struct option buf = { longbuf.c_str(), argstyle, 0, 0 };
00099 longop[ i ] = buf;
00100
00101 shortop += shortbuf;
00102 if ( argstyle == TOption::tNeed ) {
00103 shortop += ":";
00104 } else if ( argstyle == TOption::tEither ) {
00105 shortop += "::";
00106 }
00107 }
00108 struct option delim = { 0, 0, 0, 0 };
00109 longop[ listlen ] = delim;
00110
00111
00112 Tint retval = 0;
00113 Tint optionindex = 0;
00114 while ( ( retval = getopt_long( theNumberOfOriginalArguments, theOriginalArguments, shortop.c_str(), longop, &optionindex ) ) != -1 ) {
00115
00116
00117 if ( retval == 0 ) {
00118 Tstring optionname = longop[ optionindex ].name;
00119 Tstring parameter = "";
00120 if ( optarg ) {
00121 parameter = optarg;
00122 }
00123 for ( Tsize_t i = 0; i < listlen; i ++ ) {
00124 if ( oplist[ i ] == optionname ) {
00125 theOptionMap.AddOption( oplist[ i ], parameter );
00126 break;
00127 }
00128 }
00129 } else if ( retval == '?' ) {
00130 Tcout << Tendl;
00131 ShowUsage();
00132 break;
00133 } else if ( isalnum( retval ) ) {
00134 Tstring optionname( 1, (Tchar)retval );
00135 Tstring parameter = "";
00136 if ( optarg ) {
00137 parameter = optarg;
00138 }
00139 for ( Tsize_t i = 0; i < listlen; i ++ ) {
00140 if ( oplist[ i ] == optionname ) {
00141 theOptionMap.AddOption( oplist[ i ], parameter );
00142 break;
00143 }
00144 }
00145 } else {
00146 Tcout << Tendl;
00147 ShowUsage();
00148 break;
00149 }
00150 }
00151
00152 if ( optind < theNumberOfOriginalArguments ) {
00153 while ( optind < theNumberOfOriginalArguments ) {
00154 theElements.push_back( theOriginalArguments[ optind ++ ] );
00155 }
00156 }
00157 theNumberOfElements = (Tint)theElements.size();
00158
00159 delete [] longop;
00160
00161 return;
00162 }
00163
00164 Tostream& operator<<( Tostream& tos, const TArgument& right )
00165 {
00166 tos << "Orginal:" << Tendl;
00167 for ( Tint i = 0; i < right.theNumberOfOriginalArguments; i ++ ) {
00168 tos << Twquote << right.theOriginalArguments[ i ] << Twquote;
00169 if ( i != right.theNumberOfOriginalArguments - 1 ) {
00170 tos << ", ";
00171 }
00172 }
00173 tos << Tendl;
00174
00175
00176 tos << "Directory and Program:" << Tendl;
00177 tos << Twquote << right.theDirectoryName << Twquote << ", ";
00178 tos << Twquote << right.theProgramName << Twquote << Tendl;
00179
00180 if ( right.theNumberOfElements > 0 ) {
00181 tos << "Element:" << Tendl;
00182 for ( Tint i = 0; i < right.theNumberOfElements; i ++ ) {
00183 tos << Twquote << right.theElements[ i ] << Twquote;
00184 if ( i != right.theNumberOfElements - 1 ) {
00185 tos << ", ";
00186 }
00187 }
00188 tos << Tendl;
00189 }
00190
00191 if ( !right.theOptionTable.GetOptionList().empty() ) {
00192 tos << "Option Table:" << Tendl;
00193 tos << right.theOptionTable << Tendl;
00194 }
00195
00196 if ( !right.theOptionMap.GetOptionList().empty() ) {
00197 tos << "Given Options:" << Tendl;
00198 tos << right.theOptionMap << Tendl;
00199 }
00200
00201 return tos;
00202 }
00203
00204 Tvoid TArgument::ShowUsage() const
00205 {
00206 Tcout << theUsage << Tendl;
00207 Tcout << theOptionTable << Tendl;
00208 exit( 0 );
00209 }
00210
00211 Tvoid TArgument::ShowHelp() const
00212 {
00213 Tcout << theUsage << Tendl;
00214 Tcout << theOptionTable << Tendl;
00215 exit( 0 );
00216 }
00217
00218 Tvoid TArgument::ShowVersion() const
00219 {
00220 Tcout << theProgramName << " (" << Tproject << ") " << Tversion << Tendl;
00221 Tcout << "$Id: TArgument.cc,v 1.4 2004/03/07 10:30:34 goiwai Exp $" << Tendl;
00222 exit( 0 );
00223 }
00224
00225 #ifdef __CLDAQ_ROOT_DLL
00226 ClassImp(TArgument)
00227 #endif