Command Line Arguments

Started by
6 comments, last by Webbster 19 years, 3 months ago
Just been trying something I have'nt really expiremented with. Command Line Arguments, my main() function looks a little like this (i've stripped away most of the non-relevant code)


int main( int argc, char *argv[] )
{
	// Program begins by checking command line arguments
	if( argc != 1 ) 
	{
		// We should be recieving only 1 command line argument
		// Close application
		return 0;
	}
	else
	{
		if( argv[1] == 0 )
		{
			MessageBox(NULL, "Game App", NULL, NULL);
		}
		
		if( argv[1] == 1 )
		{
			MessageBox(NULL, "Editor App", NULL, NULL);
		}
	}

	return 0; // Final closure!
}


Now, I get the strange feeling I'm going about this completely wrong but I think you can probably tell what I'm trying to do. I want completely different sections of the application to run dependant on the value of a command line argument. If the arguments value is 0 the game class will be created, if however a 1 is passed into the command line argument an instance of CEditApp will be created allowing the user to use the games editor. Can anyone answer this puzzle as it's starting to frustrate me i'd figured this would be very simple. :) Thanks
Advertisement

int main( int argc, char *argv[] )
{
// Program begins by checking command line arguments
if( argc != 2 )
{
// We should be recieving only 1 command line argument
// Close application
return 0;
}
else
{
if( !strcmp(argv[1], "0") )
{
MessageBox(NULL, "Game App", NULL, NULL);
}

if( !strcmp(argv[1], "1") )
{
MessageBox(NULL, "Editor App", NULL, NULL);
}
}

return 0; // Final closure!
}

note that always at least 1 argument is passed i.e. the exe name iself.

I would also suggest to use a commandline switch like
thexe.exe /editor
if no /editor switch is supplied then start the game. zero and one are not the most verbose switches.
Thanks for that i'll probably go for the /editor /game switches as opposed to using 0 and 1.

The implementation you provided, how flexible is that? I'm using SDL in the hope that my game will run on Linux as well as Windows, that won't pose a problem will it?

Thanks Again
It seems his implementation would run under Linux as well...(well, you'll have to throw out those MessageBoxes, of course =) )
hehe, the win32 msg boxes are only temporary till i get my html logging class finished.

Just wanted to get my code double-checked does this all look okay now:

int main( int argc, char *argv[] ){	// Program begins by checking command line arguments	if( argc != 2 ) 	{		// We should be recieving only 1 command line argument		// Close application		return 0;	}	else	{		if( !strcmp( argv[1], "/game" ) )		{			// Run the game client			CGameApp * pGame = new CGameApp;			if( !pGame->CreateWnd( 800,600, false ) )			{				return 0;			}			pGame->GameLoop();		}				if( !strcmp( argv[1], "/editor" ) )		{			// Run game editor client			CEditApp * pEditor = new CEditApp;			if( !pEditor->CreateWnd( 800,600, false ) )			{				return 0;			}			pEditor->MsgLoop();		}	}	return 0; // Final closure!}

instead of closing the program if a command isnt passed in you should start the game or display an error message
Closing may be OK depending on context, but if that's what makes the most sense, you should probably (a) write an error message in addition to closing; and (b) return a non-zero value. (a) is so that the user knows what happened, and how to fix it. (b) is so someone else who uses your program in their own program (or more likely, script) will be able to detect the failure.
Okay i've added:

pLog->Print("Program recieved no Command Line Arguments");
pLog->Print("Game Application executing by default");

In this sections:

// Program begins by checking command line argumentsif( argc != 2 ) {	// We should be recieving only 1 command line argument	// Close application	return 0;}


which now reads:

// Program begins by checking command line argumentsif( argc != 2 ) {	pLog->Print("Program recieved no Command Line Arguments");	pLog->Print("Game Application executing by default");		StartGame();}

This topic is closed to new replies.

Advertisement