GNU CommonC++
|
00001 // Copyright (C) 2001-2010 Gianni Mariani 00002 // 00003 // This program is free software; you can redistribute it and/or modify 00004 // it under the terms of the GNU General Public License as published by 00005 // the Free Software Foundation; either version 2 of the License, or 00006 // (at your option) any later version. 00007 // 00008 // This program is distributed in the hope that it will be useful, 00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00011 // GNU General Public License for more details. 00012 // 00013 // You should have received a copy of the GNU General Public License 00014 // along with this program; if not, write to the Free Software 00015 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00016 // 00017 // As a special exception, you may use this file as part of a free software 00018 // library without restriction. Specifically, if other files instantiate 00019 // templates or use macros or inline functions from this file, or you compile 00020 // this file and link it with other files to produce an executable, this 00021 // file does not by itself cause the resulting executable to be covered by 00022 // the GNU General Public License. This exception does not however 00023 // invalidate any other reasons why the executable file might be covered by 00024 // the GNU General Public License. 00025 // 00026 // This exception applies only to the code released under the name GNU 00027 // Common C++. If you copy code from other releases into a copy of GNU 00028 // Common C++, as the General Public License permits, the exception does 00029 // not apply to the code that you add in this way. To avoid misleading 00030 // anyone as to the status of such modified files, you must delete 00031 // this exception notice from them. 00032 // 00033 // If you write modifications of your own for GNU Common C++, it is your choice 00034 // whether to permit this exception to apply to your modifications. 00035 // If you do not wish that, delete this exception notice. 00036 // 00037 00043 #ifndef CCXX_CMDOPTNS_H_ 00044 #define CCXX_CMDOPTNS_H_ 00045 00046 #ifndef CCXX_STRING_H_ 00047 #include <cc++/string.h> 00048 #endif 00049 00050 #ifdef CCXX_NAMESPACES 00051 namespace ost { 00052 #endif 00053 00054 class CommandOption; 00055 class CommandOptionParse; 00056 00064 extern __EXPORT CommandOption * defaultCommandOptionList; 00065 00076 class __EXPORT CommandOption { 00077 public: 00078 00083 const char * optionName; 00084 00089 const char * optionLetter; 00090 00096 const char * description; 00097 00103 enum OptionType { 00107 hasArg, 00111 noArg, 00115 trailing, 00119 collect 00120 }; 00121 00125 OptionType optionType; // HasArg, NoArg or Trailing 00126 00131 bool required; // Option is required - fail without it 00132 00137 CommandOption * next; 00138 00142 virtual ~CommandOption(); 00143 00155 CommandOption( 00156 const char * inOptionName, 00157 const char * inOptionLetter, 00158 const char * inDescription, 00159 OptionType inOptionType, 00160 bool inRequired = false, 00161 CommandOption ** ppNext = & defaultCommandOptionList 00162 ); 00163 00171 virtual void foundOption( CommandOptionParse * cop, const char * value = 0 ); 00172 00181 virtual void foundOption( CommandOptionParse * cop, const char ** value, int num ); 00182 00189 virtual void parseDone( CommandOptionParse * cop ); 00190 00198 virtual void performTask( CommandOptionParse * cop ); 00199 00206 virtual bool hasValue(); 00207 00208 }; 00209 00214 class __EXPORT CommandOptionWithArg : public CommandOption { 00215 public: 00216 00220 const char ** values; 00221 00225 int numValue; 00226 00238 CommandOptionWithArg( 00239 const char * inOptionName, 00240 const char * inOptionLetter, 00241 const char * inDescription, 00242 OptionType inOptionType, 00243 bool inRequired = false, 00244 CommandOption ** ppNext = & defaultCommandOptionList 00245 ); 00246 00247 virtual ~CommandOptionWithArg(); 00248 00249 virtual void foundOption( CommandOptionParse * cop, const char * value = 0 ); 00250 virtual void foundOption( CommandOptionParse * cop, const char ** value, int num ); 00251 virtual bool hasValue(); 00252 }; 00253 00257 class __EXPORT CommandOptionArg : public CommandOptionWithArg { 00258 public: 00259 00270 CommandOptionArg( 00271 const char * inOptionName, 00272 const char * inOptionLetter, 00273 const char * inDescription, 00274 bool inRequired = false, 00275 CommandOption ** ppNext = & defaultCommandOptionList 00276 ); 00277 00278 virtual ~CommandOptionArg(); 00279 00280 00281 }; 00282 00292 class __EXPORT CommandOptionRest : public CommandOptionWithArg { 00293 public: 00294 00305 CommandOptionRest( 00306 const char * inOptionName, 00307 const char * inOptionLetter, 00308 const char * inDescription, 00309 bool inRequired = false, 00310 CommandOption ** ppNext = & defaultCommandOptionList 00311 ); 00312 00313 }; 00314 00322 class __EXPORT CommandOptionCollect : public CommandOptionWithArg { 00323 public: 00324 00335 CommandOptionCollect( 00336 const char * inOptionName, 00337 const char * inOptionLetter, 00338 const char * inDescription, 00339 bool inRequired = false, 00340 CommandOption ** ppNext = & defaultCommandOptionList 00341 ); 00342 00343 }; 00344 00348 class __EXPORT CommandOptionNoArg : public CommandOption { 00349 public: 00350 00354 int numSet; // The number of times this argument is set 00355 00366 CommandOptionNoArg( 00367 const char * inOptionName, 00368 const char * inOptionLetter, 00369 const char * inDescription, 00370 bool inRequired = false, 00371 CommandOption ** ppNext = & defaultCommandOptionList 00372 ); 00373 00377 virtual void foundOption( CommandOptionParse * cop, const char * value = 0 ); 00378 00379 }; 00380 00390 class __EXPORT CommandOptionParse { 00391 public: 00392 00396 virtual ~CommandOptionParse() = 0; 00397 00401 virtual bool argsHaveError() = 0; 00402 00406 virtual const char * printErrors() = 0; 00407 00411 virtual const char * printUsage() = 0; 00412 00417 virtual void registerError( const char * errMsg ) = 0; 00418 00423 virtual void performTask() = 0; 00424 00425 }; 00426 00435 __EXPORT CommandOptionParse * makeCommandOptionParse( 00436 int argc, 00437 char ** argv, 00438 const char * comment, 00439 CommandOption * options = defaultCommandOptionList 00440 ); 00441 00442 #ifdef CCXX_NAMESPACES 00443 } 00444 #endif 00445 00446 #endif 00447