Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 7250 in orxonox.OLD


Ignore:
Timestamp:
Mar 26, 2006, 2:27:16 PM (18 years ago)
Author:
rennerc
Message:

implemented -h/—help argument

Location:
branches/preferences/src
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • branches/preferences/src/lib/parser/cmdline_parser/cmdline_parser.cc

    r7246 r7250  
    1616#include "cmdline_parser.h"
    1717
     18#include "src/lib/util/substring.h"
     19
    1820using namespace std;
    1921
     
    3537
    3638
    37 bool CmdLineParser::add( int id, const std::string & longOption, char shortOption, int numArgs, bool back )
     39bool CmdLineParser::add( int id, const std::string & longOption, char shortOption, int numArgs, const std::string & argNames, const std::string& help, bool back )
    3840{
    3941  ArgTableEntry entry;
     
    4345  entry.shortOption = shortOption;
    4446  entry.numArgs = numArgs;
     47  entry.argNames = argNames;
     48  entry.help = help;
    4549 
    4650  if ( back )
     
    5357bool CmdLineParser::parse( ArgParserCallback cb, void * data, int argc, char ** argv )
    5458{
     59  this->exeName = argv[0];
     60 
    5561  //put all args in vector
    5662  std::vector<std::string> args;
     
    166172  }
    167173}
     174
     175void CmdLineParser::showHelp()
     176{
     177  printf("Usage: %s [options]\n", exeName.c_str());
     178  printf("\n");
     179 
     180  std::list<std::vector<std::string> > output;
     181 
     182  for ( ArgTable::iterator it = argTable.begin(); it != argTable.end(); it++ )
     183  {
     184    output.push_back( std::vector<std::string>() );
     185   
     186    SubString substr( it->argNames );
     187    std::string args;
     188    assert( it->numArgs == substr.getCount() );
     189     
     190    for ( int i = 0; i<it->numArgs; i++ )
     191    {
     192      args += " [" + substr[i] + "]";
     193    }
     194   
     195    if ( it->shortOption != '\0' )
     196    {
     197      output.back().push_back( " -" + std::string((char*)&it->shortOption, 1) );
     198      output.back().back() += args;
     199    }
     200    else
     201      output.back().push_back( "" );
     202   
     203    if ( it->longOption != "" )
     204    {
     205      output.back().push_back( "--" + it->longOption );
     206     
     207      output.back().back() += args;
     208    }
     209    else
     210      output.back().push_back( "" );
     211   
     212    output.back().push_back( it->help );
     213  }
     214 
     215  output.push_back( std::vector<std::string>() );
     216  output.back().push_back( "Option" );
     217  output.back().push_back( "Long option" );
     218  output.back().push_back( "Meaning" );
     219 
     220  output.reverse();
     221 
     222  int maxShort = 0;
     223  int maxLong = 0;
     224 
     225  std::list<std::vector<std::string> >::const_iterator it;
     226 
     227  for ( it = output.begin(); it != output.end(); it++ )
     228  {
     229    if ( (*it)[0].length() > maxShort )
     230      maxShort = (*it)[0].length();
     231   
     232    if ( (*it)[1].length() > maxLong )
     233      maxLong = (*it)[1].length();
     234  }
     235 
     236  for ( it = output.begin(); it != output.end(); it++ )
     237  {
     238    printf("%s ", (*it)[0].c_str());
     239   
     240    for ( int i = 0; i<maxShort-(*it)[0].length(); i++ )
     241      printf(" ");
     242   
     243    printf("%s ", (*it)[1].c_str());
     244   
     245    for ( int i = 0; i<maxLong-(*it)[1].length(); i++ )
     246      printf(" ");
     247   
     248    printf("%s\n", (*it)[2].c_str());
     249  }
     250 
     251  exit(0);
     252}
  • branches/preferences/src/lib/parser/cmdline_parser/cmdline_parser.h

    r7243 r7250  
    2121  char        shortOption;
    2222  int         numArgs;
     23  std::string argNames;
     24  std::string help;
    2325};
    2426
     
    3537    virtual ~CmdLineParser();
    3638   
    37     bool add( int id, const std::string& longOption, char shortOption, int numArgs, bool back=false );
     39    bool add( int id, const std::string& longOption, char shortOption, int numArgs, const std::string & argNames, const std::string& help, bool back=false );
    3840
    3941    bool parse( ArgParserCallback cb, void* data, int argc, char** argv );
    4042   
     43    void showHelp();
     44   
    4145  private:
    4246    ArgTable argTable;
     47    std::string exeName;
    4348   
    4449    inline bool matches( ArgTableEntry entry, std::string arg, bool & finish );
  • branches/preferences/src/lib/parser/preferences/cmd_line_prefs_reader.cc

    r7243 r7250  
    4040bool CmdLinePrefsReader::callBack( ArgTableEntry entry, void * data, const std::string & arg, const std::vector<MultiType> & argArgs )
    4141{
     42  CallbackData * cbd = (CallbackData *)data;
     43 
    4244  switch ( entry.id )
    4345  {
     
    5759      }
    5860     
    59       Preferences::getInstance()->setMultiType( section, key, argArgs[0], true );
     61      //Preferences::getInstance()->setMultiType( section, key, argArgs[0], true );
     62      cbd->iniEntries.push_back( IniEntry() );
     63      cbd->iniEntries.back().section = section;
     64      cbd->iniEntries.back().key = key;
     65      cbd->iniEntries.back().value = argArgs[0].getString();
    6066      break;
    6167    }
     68    case ID_HELP:
     69      cbd->parser->showHelp();
     70      break;
    6271    default:
    6372      assert(false);
     
    6776}
    6877
     78
    6979bool CmdLinePrefsReader::parse( int argc, char ** argv )
    7080{
    7181  CmdLineParser parser;
    7282 
    73   parser.add( ID_SET_INI, "set-%", '\0', 1 );
     83  parser.add( ID_HELP, "help", 'h', 0, "", "Show this help");
     84  parser.add( 99, "port", 'p', 0, "", "dont know");
    7485 
    75   parser.parse( &callBack, NULL, argc, argv );
     86  parser.add( ID_SET_INI, "set-%", '\0', 1, "value", "Override a configuration element." );
     87 
     88  CallbackData cbd;
     89 
     90  cbd.parser = &parser;
     91 
     92  parser.parse( &callBack, &cbd, argc, argv );
    7693}
    7794
  • branches/preferences/src/lib/parser/preferences/cmd_line_prefs_reader.h

    r7243 r7250  
    1212#include "src/lib/parser/cmdline_parser/cmdline_parser.h"
    1313
     14struct IniEntry
     15{
     16  std::string section;
     17  std::string key;
     18  std::string value;
     19};
     20
     21struct CallbackData
     22{
     23  std::list<IniEntry> iniEntries;
     24  CmdLineParser * parser;
     25};
     26
    1427enum
    1528{
    16   ID_SET_INI = 1
     29  ID_SET_INI = 1,
     30  ID_HELP
    1731};
    1832
  • branches/preferences/src/orxonox.h

    r7248 r7250  
    1111class WorldEntity;
    1212class GameLoader;
    13 class IniParser;
    1413
    1514//! orxonox core singleton class
Note: See TracChangeset for help on using the changeset viewer.