3
*************************************************************************
5
ArmageTron -- Just another Tron Lightcycle Game in 3D.
6
Copyright (C) 2000 Manuel Moos (manuel@moosnet.de)
7
Copyright (C) 2004 Armagetron Advanced Team (http://sourceforge.net/projects/armagetronad/)
9
**************************************************************************
11
This program is free software; you can redistribute it and/or
12
modify it under the terms of the GNU General Public License
13
as published by the Free Software Foundation; either version 2
14
of the License, or (at your option) any later version.
16
This program is distributed in the hope that it will be useful,
17
but WITHOUT ANY WARRANTY; without even the implied warranty of
18
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
GNU General Public License for more details.
21
You should have received a copy of the GNU General Public License
22
along with this program; if not, write to the Free Software
23
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25
***************************************************************************
30
#ifndef TCOMMANDLINE_H_INCLUDED
31
#include "tCommandLine.h"
35
#include "tConfiguration.h"
36
#include "tException.h"
46
static tCommandLineAnalyzer * s_commandLineAnalyzerAnchor;
48
static void quitWithMessagePrepare( const char* message )
57
int result = MessageBox (NULL, message , "Message", MB_OK);
65
static void quitWithMessage( const char* message )
68
quitWithMessagePrepare( message );
71
// tGenericException( message, "Command Line Parsing Error" );
75
//#define QUIT(x) { std::ostringstream s; s << x; quitWithMessage(s.str().c_str()); name_.Clear(); } exit(0)
76
//#define QUIT(x) { std::ostringstream s; s << x; quitWithMessage(s.str().c_str()); name_.Clear(); } return false
77
#define QUIT(x) { std::ostringstream s; s << x; quitWithMessage(s.str().c_str()); name_.Clear();}
79
bool tCommandLineData::Analyse(int argc,char **argv)
83
#define getcwd _getcwd
86
char * cwd = getcwd(0,0);
87
tERR_MESSAGE( "Executable: " << argv[0] << ", CWD: " << cwd );
91
tCommandLineParser parser( argc, argv );
93
tASSERT( programVersion_ );
95
char const * run = parser.Current();
98
if (*run == '\\' || *run == '/')
103
if ( name_.Len() <= 3 )
105
name_ = "Armagetron";
110
// initialize third party analyzers
112
tCommandLineAnalyzer * commandLineAnalyzer = s_commandLineAnalyzerAnchor;
113
while ( commandLineAnalyzer )
115
commandLineAnalyzer->Initialize( parser );
116
commandLineAnalyzer = commandLineAnalyzer->Next();
122
//std::cout << "config loaded\n";
131
while ( !parser.End() )
133
if ( parser.GetSwitch( "--help", "-h" ) )
136
std::ostringstream s;
137
s << "\n\nUsage: " << name_ << " [Arguments]\n\n"
138
<< "Possible arguments:\n\n";
139
s << "-h, --help : print this message\n";
141
s << "--doc : print documentation for all console commands\n";
143
s << "-v, --version : print version number\n\n";
145
// ask third party analyzers
146
tCommandLineAnalyzer * commandLineAnalyzer = s_commandLineAnalyzerAnchor;
147
while ( commandLineAnalyzer )
149
commandLineAnalyzer->Help( s );
150
commandLineAnalyzer = commandLineAnalyzer->Next();
157
quitWithMessagePrepare( s.str().c_str() );
164
else if ( parser.GetSwitch( "--doc") )
169
else if ( parser.GetSwitch( "--version", "-v") )
171
QUIT( "This is " << name_ << " version " << *programVersion_ << ".\n" );
175
// let the registered command line anelyzers have a go
176
tCommandLineAnalyzer * commandLineAnalyzer = s_commandLineAnalyzerAnchor;
177
bool success = false;
178
while ( commandLineAnalyzer )
180
if ( success = commandLineAnalyzer->Analyze( parser ) )
182
commandLineAnalyzer = commandLineAnalyzer->Next();
187
QUIT( "\n\nUnknown command line option " << parser.Current() << ". Type " << name_ << " -h to get a list of possible options.\n\n" );
194
bool tCommandLineData::Execute()
200
std::cout << "Available console commands/config file settings:\n\n";
201
tConfItemBase::DocAll( std::cout );
210
// *******************************************************************************************
214
// *******************************************************************************************
216
//! @param option long version of the switch
217
//! @param option_short short version of the switch
218
//! @return true if the switch was detected
220
// *******************************************************************************************
222
bool tCommandLineParser::GetSwitch( char const * option, char * option_short )
227
char * argument = argv[index];
229
if ( !strcmp(argument,option) || ( option_short && !strcmp(argument,option_short ) ) )
238
// *******************************************************************************************
242
// *******************************************************************************************
244
//! @param target string to store option to
245
//! @param option long version of the option
246
//! @param option_short short version of the option
247
//! @return true if the option was detected
249
// *******************************************************************************************
251
bool tCommandLineParser::GetOption( tString & target, char const * option, char * option_short )
256
char * argument = argv[index];
258
if ( GetSwitch( option, option_short ) )
262
target = argv[index];
270
QUIT( " " << argument << " needs another argument.\n" );
277
// *******************************************************************************************
281
// *******************************************************************************************
283
//! @return true if the options have been parsed to the end
285
// *******************************************************************************************
287
bool tCommandLineParser::End( void ) const
289
return ( index >= argc );
292
// *******************************************************************************************
294
// * tCommandLineParser
296
// *******************************************************************************************
298
//! @param a_argc number of arguments
299
//! @param a_argv arguments
301
// *******************************************************************************************
303
tCommandLineParser::tCommandLineParser( int a_argc, char * * a_argv )
304
: argc( a_argc ), argv( a_argv ), index( 0 )
308
// *******************************************************************************************
310
// * tCommandLineParser
312
// *******************************************************************************************
315
// *******************************************************************************************
318
tCommandLineParser::tCommandLineParser( void )
323
// *******************************************************************************************
327
// *******************************************************************************************
329
//! @return the full path of the executable
331
// *******************************************************************************************
333
const char * tCommandLineParser::Executable( void ) const
338
// *******************************************************************************************
342
// *******************************************************************************************
344
//! @return the current command line option
346
// *******************************************************************************************
348
const char * tCommandLineParser::Current( void ) const
350
return argv[ index ];
353
// *******************************************************************************************
357
// *******************************************************************************************
360
// *******************************************************************************************
362
void tCommandLineParser::Advance( void )
369
// *******************************************************************************************
371
// * tCommandLineAnalyzer
373
// *******************************************************************************************
376
// *******************************************************************************************
378
tCommandLineAnalyzer::tCommandLineAnalyzer( void )
379
: tListItem< tCommandLineAnalyzer >( s_commandLineAnalyzerAnchor )
383
// *******************************************************************************************
385
// * ~tCommandLineAnalyzer
387
// *******************************************************************************************
390
// *******************************************************************************************
392
tCommandLineAnalyzer::~tCommandLineAnalyzer( void )
396
// *******************************************************************************************
400
// *******************************************************************************************
402
//! @param parser parser to analyze
404
// *******************************************************************************************
406
void tCommandLineAnalyzer::DoInitialize( tCommandLineParser & parser )
410
// *******************************************************************************************
414
// *******************************************************************************************
416
//! @param parser parser to analyze
417
//! @return true if anaysis was succesful
419
// *******************************************************************************************
421
bool tCommandLineAnalyzer::DoAnalyze( tCommandLineParser & parser )
426
// *******************************************************************************************
430
// *******************************************************************************************
432
//! @param s string to write help to
434
// *******************************************************************************************
436
void tCommandLineAnalyzer::DoHelp( std::ostream & s )