Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 7319 in orxonox.OLD for trunk/src/lib/parser/cmdline_parser


Ignore:
Timestamp:
Apr 17, 2006, 1:00:09 PM (18 years ago)
Author:
bensch
Message:

orxonox/trunk: MUCH more comprehensive SubString

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/lib/parser/cmdline_parser/cmdline_parser.cc

    r7260 r7319  
    1111   ### File Specific:
    1212   main-programmer: Christoph Renner
    13    co-programmer: 
     13   co-programmer:
    1414*/
    1515
     
    4040{
    4141  ArgTableEntry entry;
    42  
     42
    4343  entry.id = id;
    4444  entry.longOption = longOption;
     
    4747  entry.argNames = argNames;
    4848  entry.help = help;
    49  
     49
    5050  if ( back )
    5151    argTable.push_back( entry );
     
    5858{
    5959  this->exeName = argv[0];
    60  
     60
    6161  //put all args in vector
    6262  std::vector<std::string> args;
    63  
     63
    6464  for ( int i = 1; i<argc; i++ )
    6565  {
    6666    std::string s = argv[i];
    67    
     67
    6868    if ( s.find( "=" ) == std::string::npos )
    6969    {
     
    8888      op.erase( op.find("=") );
    8989      ar.erase( 0, ar.find("=")+1);
    90      
     90
    9191      //PRINTF(0)("'%s' '%s'\n", op.c_str(), ar.c_str());
    9292      args.push_back( op );
     
    9494    }
    9595  }
    96  
     96
    9797  int i = 0;
    98  
     98
    9999  ArgTable::iterator it;
    100100  bool finish;
    101101  bool found;
    102  
     102
    103103  while ( i < args.size() )
    104104  {
     
    109109      {
    110110        found = true;
    111        
     111
    112112        int posArgs = 1;
    113        
     113
    114114        while ( i + posArgs < args.size() )
    115115        {
     
    119119            posArgs++;
    120120        }
    121        
     121
    122122        posArgs--;
    123        
     123
    124124        if ( it->numArgs > posArgs )
    125125        {
     
    127127          return false;
    128128        }
    129        
     129
    130130        std::vector<MultiType> argArgs;
    131        
     131
    132132        for ( int j = 1; j <= it->numArgs; j++ )
    133133          argArgs.push_back( args[i+j] );
    134        
     134
    135135        if ( !cb( *it, data, args[i], argArgs ) )
    136136          return false;
    137        
     137
    138138        i += it->numArgs;
    139        
     139
    140140        if ( finish )
    141141        {
     
    143143          break;
    144144        }
    145         else 
    146         { 
     145        else
     146        {
    147147          assert( it->numArgs == 0 );
    148148        }
    149149      }
    150150    }
    151    
     151
    152152    if ( !found )
    153153    {
     
    156156    }
    157157  }
    158  
     158
    159159  return true;
    160160}
     
    163163{
    164164  finish = true;
    165  
     165
    166166  if ( arg.length() < 2 )
    167167    return false;
    168  
     168
    169169  if ( arg[0] == '-' )
    170170  {
     
    172172    {
    173173      arg.erase( 0, 2 );
    174      
     174
    175175      if ( entry.longOption.find('%') != std::string::npos )
    176176      {
     
    208208  printf("Usage: %s [options]\n", exeName.c_str());
    209209  printf("\n");
    210  
     210
    211211  std::list<std::vector<std::string> > output;
    212  
     212
    213213  for ( ArgTable::iterator it = argTable.begin(); it != argTable.end(); it++ )
    214214  {
    215215    output.push_back( std::vector<std::string>() );
    216    
     216
    217217    SubString substr( it->argNames );
    218218    std::string args;
    219     assert( it->numArgs == substr.getCount() );
    220      
     219    assert( it->numArgs == substr.size() );
     220
    221221    for ( int i = 0; i<it->numArgs; i++ )
    222222    {
    223223      args += " [" + substr[i] + "]";
    224224    }
    225    
     225
    226226    if ( it->shortOption != '\0' )
    227227    {
     
    231231    else
    232232      output.back().push_back( "" );
    233    
     233
    234234    if ( it->longOption != "" )
    235235    {
    236236      output.back().push_back( "--" + it->longOption );
    237      
     237
    238238      output.back().back() += args;
    239239    }
    240240    else
    241241      output.back().push_back( "" );
    242    
     242
    243243    output.back().push_back( it->help );
    244244  }
    245  
     245
    246246  output.push_back( std::vector<std::string>() );
    247247  output.back().push_back( "Option" );
    248248  output.back().push_back( "Long option" );
    249249  output.back().push_back( "Description" );
    250  
     250
    251251  output.reverse();
    252  
     252
    253253  int maxShort = 0;
    254254  int maxLong = 0;
    255  
     255
    256256  std::list<std::vector<std::string> >::const_iterator it;
    257  
     257
    258258  for ( it = output.begin(); it != output.end(); it++ )
    259259  {
    260260    if ( (*it)[0].length() > maxShort )
    261261      maxShort = (*it)[0].length();
    262    
     262
    263263    if ( (*it)[1].length() > maxLong )
    264264      maxLong = (*it)[1].length();
    265265  }
    266  
     266
    267267  for ( it = output.begin(); it != output.end(); it++ )
    268268  {
    269269    printf("%s ", (*it)[0].c_str());
    270    
     270
    271271    for ( int i = 0; i<maxShort-(*it)[0].length(); i++ )
    272272      printf(" ");
    273    
     273
    274274    printf("%s ", (*it)[1].c_str());
    275    
     275
    276276    for ( int i = 0; i<maxLong-(*it)[1].length(); i++ )
    277277      printf(" ");
    278    
     278
    279279    printf("%s\n", (*it)[2].c_str());
    280280  }
    281  
     281
    282282  exit(0);
    283283}
Note: See TracChangeset for help on using the changeset viewer.