Sign in to follow this  

Command Line Arguments

This topic is 4729 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster

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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
It seems his implementation would run under Linux as well...(well, you'll have to throw out those MessageBoxes, of course =) )

Share this post


Link to post
Share on other sites
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!
}



Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 arguments
if( argc != 2 )
{
// We should be recieving only 1 command line argument
// Close application
return 0;
}




which now reads:



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

StartGame();
}


Share this post


Link to post
Share on other sites

This topic is 4729 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this