00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef TCLAP_XORHANDLER_H
00024 #define TCLAP_XORHANDLER_H
00025
00026 #include <tclap/Arg.h>
00027 #include <tclap/PrintSensibly.h>
00028 #include <string>
00029 #include <vector>
00030 #include <algorithm>
00031 #include <iostream>
00032
00033 namespace TCLAP {
00034
00039 class XorHandler
00040 {
00041 protected:
00042
00046 std::vector< std::vector<Arg*> > _orList;
00047
00048 public:
00049
00053 XorHandler( ) {}
00054
00059 void add( std::vector<Arg*>& ors );
00060
00068 int check( const Arg* a );
00069
00073 std::string shortUsage();
00074
00079 void printLongUsage(std::ostream& os);
00080
00086 bool contains( const Arg* a );
00087
00088 };
00089
00090
00092
00094 inline void XorHandler::add( std::vector<Arg*>& ors )
00095 {
00096 _orList.push_back( ors );
00097 }
00098
00099 inline std::string XorHandler::shortUsage()
00100 {
00101 std::string out = "";
00102 for ( int i = 0; (unsigned int)i < _orList.size(); i++ )
00103 {
00104 out += " {";
00105 for ( ArgVectorIterator it = _orList[i].begin();
00106 it != _orList[i].end(); it++ )
00107 out += (*it)->shortID() + "|";
00108
00109 out[out.length()-1] = '}';
00110 }
00111
00112 return out;
00113 }
00114
00115 inline void XorHandler::printLongUsage( std::ostream& os )
00116 {
00117 for ( int i = 0; (unsigned int)i < _orList.size(); i++ )
00118 {
00119 for ( ArgVectorIterator it = _orList[i].begin();
00120 it != _orList[i].end();
00121 it++ )
00122 {
00123 spacePrint( os, (*it)->longID(), 75, 3, 3 );
00124 spacePrint( os, (*it)->getDescription(), 75, 5, 0 );
00125
00126 if ( it+1 != _orList[i].end() )
00127 spacePrint(os, "-- OR --", 75, 9);
00128 }
00129 os << std::endl << std::endl;
00130 }
00131 }
00132
00133 inline int XorHandler::check( const Arg* a )
00134 {
00135
00136 for ( int i = 0; (unsigned int)i < _orList.size(); i++ )
00137 {
00138
00139 if ( find( _orList[i].begin(), _orList[i].end(), a ) !=
00140 _orList[i].end() )
00141 {
00142
00143 for ( ArgVectorIterator it = _orList[i].begin();
00144 it != _orList[i].end();
00145 it++ )
00146 if ( a != (*it) )
00147 (*it)->xorSet();
00148
00149
00150 return (int)_orList[i].size();
00151 }
00152 }
00153
00154 if ( a->isRequired() )
00155 return 1;
00156 else
00157 return 0;
00158 }
00159
00160 inline bool XorHandler::contains( const Arg* a )
00161 {
00162 for ( int i = 0; (unsigned int)i < _orList.size(); i++ )
00163 for ( ArgVectorIterator it = _orList[i].begin();
00164 it != _orList[i].end();
00165 it++ )
00166 if ( a == (*it) )
00167 return true;
00168
00169 return false;
00170 }
00172
00174
00175 }
00176
00177 #endif