- Timestamp:
- Mar 26, 2006, 2:27:16 PM (19 years ago)
- Location:
- branches/preferences/src
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/preferences/src/lib/parser/cmdline_parser/cmdline_parser.cc
r7246 r7250 16 16 #include "cmdline_parser.h" 17 17 18 #include "src/lib/util/substring.h" 19 18 20 using namespace std; 19 21 … … 35 37 36 38 37 bool CmdLineParser::add( int id, const std::string & longOption, char shortOption, int numArgs, bool back )39 bool CmdLineParser::add( int id, const std::string & longOption, char shortOption, int numArgs, const std::string & argNames, const std::string& help, bool back ) 38 40 { 39 41 ArgTableEntry entry; … … 43 45 entry.shortOption = shortOption; 44 46 entry.numArgs = numArgs; 47 entry.argNames = argNames; 48 entry.help = help; 45 49 46 50 if ( back ) … … 53 57 bool CmdLineParser::parse( ArgParserCallback cb, void * data, int argc, char ** argv ) 54 58 { 59 this->exeName = argv[0]; 60 55 61 //put all args in vector 56 62 std::vector<std::string> args; … … 166 172 } 167 173 } 174 175 void 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 21 21 char shortOption; 22 22 int numArgs; 23 std::string argNames; 24 std::string help; 23 25 }; 24 26 … … 35 37 virtual ~CmdLineParser(); 36 38 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 ); 38 40 39 41 bool parse( ArgParserCallback cb, void* data, int argc, char** argv ); 40 42 43 void showHelp(); 44 41 45 private: 42 46 ArgTable argTable; 47 std::string exeName; 43 48 44 49 inline bool matches( ArgTableEntry entry, std::string arg, bool & finish ); -
branches/preferences/src/lib/parser/preferences/cmd_line_prefs_reader.cc
r7243 r7250 40 40 bool CmdLinePrefsReader::callBack( ArgTableEntry entry, void * data, const std::string & arg, const std::vector<MultiType> & argArgs ) 41 41 { 42 CallbackData * cbd = (CallbackData *)data; 43 42 44 switch ( entry.id ) 43 45 { … … 57 59 } 58 60 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(); 60 66 break; 61 67 } 68 case ID_HELP: 69 cbd->parser->showHelp(); 70 break; 62 71 default: 63 72 assert(false); … … 67 76 } 68 77 78 69 79 bool CmdLinePrefsReader::parse( int argc, char ** argv ) 70 80 { 71 81 CmdLineParser parser; 72 82 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"); 74 85 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 ); 76 93 } 77 94 -
branches/preferences/src/lib/parser/preferences/cmd_line_prefs_reader.h
r7243 r7250 12 12 #include "src/lib/parser/cmdline_parser/cmdline_parser.h" 13 13 14 struct IniEntry 15 { 16 std::string section; 17 std::string key; 18 std::string value; 19 }; 20 21 struct CallbackData 22 { 23 std::list<IniEntry> iniEntries; 24 CmdLineParser * parser; 25 }; 26 14 27 enum 15 28 { 16 ID_SET_INI = 1 29 ID_SET_INI = 1, 30 ID_HELP 17 31 }; 18 32 -
branches/preferences/src/orxonox.h
r7248 r7250 11 11 class WorldEntity; 12 12 class GameLoader; 13 class IniParser;14 13 15 14 //! orxonox core singleton class
Note: See TracChangeset
for help on using the changeset viewer.