Changeset 7319 in orxonox.OLD for trunk/src/lib/parser/cmdline_parser
- Timestamp:
- Apr 17, 2006, 1:00:09 PM (19 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/lib/parser/cmdline_parser/cmdline_parser.cc
r7260 r7319 11 11 ### File Specific: 12 12 main-programmer: Christoph Renner 13 co-programmer: 13 co-programmer: 14 14 */ 15 15 … … 40 40 { 41 41 ArgTableEntry entry; 42 42 43 43 entry.id = id; 44 44 entry.longOption = longOption; … … 47 47 entry.argNames = argNames; 48 48 entry.help = help; 49 49 50 50 if ( back ) 51 51 argTable.push_back( entry ); … … 58 58 { 59 59 this->exeName = argv[0]; 60 60 61 61 //put all args in vector 62 62 std::vector<std::string> args; 63 63 64 64 for ( int i = 1; i<argc; i++ ) 65 65 { 66 66 std::string s = argv[i]; 67 67 68 68 if ( s.find( "=" ) == std::string::npos ) 69 69 { … … 88 88 op.erase( op.find("=") ); 89 89 ar.erase( 0, ar.find("=")+1); 90 90 91 91 //PRINTF(0)("'%s' '%s'\n", op.c_str(), ar.c_str()); 92 92 args.push_back( op ); … … 94 94 } 95 95 } 96 96 97 97 int i = 0; 98 98 99 99 ArgTable::iterator it; 100 100 bool finish; 101 101 bool found; 102 102 103 103 while ( i < args.size() ) 104 104 { … … 109 109 { 110 110 found = true; 111 111 112 112 int posArgs = 1; 113 113 114 114 while ( i + posArgs < args.size() ) 115 115 { … … 119 119 posArgs++; 120 120 } 121 121 122 122 posArgs--; 123 123 124 124 if ( it->numArgs > posArgs ) 125 125 { … … 127 127 return false; 128 128 } 129 129 130 130 std::vector<MultiType> argArgs; 131 131 132 132 for ( int j = 1; j <= it->numArgs; j++ ) 133 133 argArgs.push_back( args[i+j] ); 134 134 135 135 if ( !cb( *it, data, args[i], argArgs ) ) 136 136 return false; 137 137 138 138 i += it->numArgs; 139 139 140 140 if ( finish ) 141 141 { … … 143 143 break; 144 144 } 145 else 146 { 145 else 146 { 147 147 assert( it->numArgs == 0 ); 148 148 } 149 149 } 150 150 } 151 151 152 152 if ( !found ) 153 153 { … … 156 156 } 157 157 } 158 158 159 159 return true; 160 160 } … … 163 163 { 164 164 finish = true; 165 165 166 166 if ( arg.length() < 2 ) 167 167 return false; 168 168 169 169 if ( arg[0] == '-' ) 170 170 { … … 172 172 { 173 173 arg.erase( 0, 2 ); 174 174 175 175 if ( entry.longOption.find('%') != std::string::npos ) 176 176 { … … 208 208 printf("Usage: %s [options]\n", exeName.c_str()); 209 209 printf("\n"); 210 210 211 211 std::list<std::vector<std::string> > output; 212 212 213 213 for ( ArgTable::iterator it = argTable.begin(); it != argTable.end(); it++ ) 214 214 { 215 215 output.push_back( std::vector<std::string>() ); 216 216 217 217 SubString substr( it->argNames ); 218 218 std::string args; 219 assert( it->numArgs == substr. getCount() );220 219 assert( it->numArgs == substr.size() ); 220 221 221 for ( int i = 0; i<it->numArgs; i++ ) 222 222 { 223 223 args += " [" + substr[i] + "]"; 224 224 } 225 225 226 226 if ( it->shortOption != '\0' ) 227 227 { … … 231 231 else 232 232 output.back().push_back( "" ); 233 233 234 234 if ( it->longOption != "" ) 235 235 { 236 236 output.back().push_back( "--" + it->longOption ); 237 237 238 238 output.back().back() += args; 239 239 } 240 240 else 241 241 output.back().push_back( "" ); 242 242 243 243 output.back().push_back( it->help ); 244 244 } 245 245 246 246 output.push_back( std::vector<std::string>() ); 247 247 output.back().push_back( "Option" ); 248 248 output.back().push_back( "Long option" ); 249 249 output.back().push_back( "Description" ); 250 250 251 251 output.reverse(); 252 252 253 253 int maxShort = 0; 254 254 int maxLong = 0; 255 255 256 256 std::list<std::vector<std::string> >::const_iterator it; 257 257 258 258 for ( it = output.begin(); it != output.end(); it++ ) 259 259 { 260 260 if ( (*it)[0].length() > maxShort ) 261 261 maxShort = (*it)[0].length(); 262 262 263 263 if ( (*it)[1].length() > maxLong ) 264 264 maxLong = (*it)[1].length(); 265 265 } 266 266 267 267 for ( it = output.begin(); it != output.end(); it++ ) 268 268 { 269 269 printf("%s ", (*it)[0].c_str()); 270 270 271 271 for ( int i = 0; i<maxShort-(*it)[0].length(); i++ ) 272 272 printf(" "); 273 273 274 274 printf("%s ", (*it)[1].c_str()); 275 275 276 276 for ( int i = 0; i<maxLong-(*it)[1].length(); i++ ) 277 277 printf(" "); 278 278 279 279 printf("%s\n", (*it)[2].c_str()); 280 280 } 281 281 282 282 exit(0); 283 283 }
Note: See TracChangeset
for help on using the changeset viewer.