Skip to main content
GameDev.net gamedev.net
🔒 Locked

c command line arguments

Started by jwc1138 Feb 14, 2003 at 12:12 PM 12 replies 550+ views
Original Post
jwc1138
jwc1138
This should be simple, but i can''t figure it... I want to have a optional command line argument passed to my program. I want to check if there are NO arguments and do one thing in that case and if there are arguments I want to make sure that it is "-u" if it is not I want to print a error... here is the code! what I''m I doing wrong? It works for no arguments but if i put -u it still thinks there is an error!
  
	if(argv[1] != '''')
	{
	   if(argv[1] == "-u")
	   {
		//..do one thing

	   }
	   else
           {
	       //print error

	   }
	}
	else
	{
	timeData = localtime(&curTime);
	}	
  
whatever.
Chip
Chip
Try this:

Instead of if(argv[1] == "-u") make it if(argv[1] == "u").

Been a long time since I''ve done command line programming but I''m pretty sure that''s the way you check it.

Hope that helps and if I''m wrong I apologize.
Chip LambertPresident/Lead Programmer LambertSoft
Hyren
Hyren
C doesn''t have simple string comparisons (ie. argv[1] == "-u"), you have to use the strcmp (or variations thereof) for string checking. Make sure to include the string.h header file and then you could check that using the line:

  
if (strcmp(arv[1], "-u") == 0)
{
...
}

Check around the net for more info on how to do other, more specific types of string comparisons if you need them.



"Back to the code mines... ka-chink... ka-chink..."
Tachyon Digital
"Back to the code mines... ka-chink... ka-chink..."Tachyon Digital - Down for the summer, be back in the fall.
jwc1138
jwc1138
if( !strcmp(argv[1], "-u" ) ) WORKS!

Is there a way to do it without the libaray function?
whatever.
Miserable
Miserable
Yes, you could do a loop and compare character by character (not forgetting the null byte at the end). It''s better to use the library function, though.

I urge you to recall, moreover, that you don''t need to check if there is ''nothing'' in the second parameter: The number of parameters is stored in argc, assuming standard naming conventions (though the first one, as you already know, is the executable itself).
darookie
darookie
If you use C++ you can do like this:

if (argv[1] == string("-u")) {
// ...
}

[edited by - darookie on February 14, 2003 1:32:11 PM]
HG
HG
You could do your own comparison:


if((argv[1][0]==''-'') && (argv[1][1]==''u''))
{
// done
}
-Richard
Qw3r7yU10p!
Qw3r7yU10p!

  
if(argv[1][0] == ''-'' &&
argv[1][1] == ''u'' &&
argv[1][2] == 0)
jwc1138
jwc1138
You guys rock!
whatever.
The Bear
The Bear
petewood! I''m surprised!

if(argv[1][0] == ''-'' &&
argv[1][1] == ''u'' &&
argv[1][2] == 0)

This is very, very bad! You are assuming the length of the character string. Best to use ''strcmp''.

Also, are you checking for how many arguments you actually have? As Miserable said, ''argc'' holds the number of arguments. It''s best to loop on ''argc'' and set flags (and exit or break if necessary).

  
for ( int i = 1; i < argc; i++ )
{
if ( strcmp( argv[ i ], "-u" ) == 0 )
{
// Set "u" flag

}
else if ( strcmp( argv[ i ], "-t" ) == 0 )
{
// Set "t" flag

}
else
{
// Unknown flag

PrintHelp();
exit( 0 );
}
}
Dean Harding
Dean Harding
quote:
Original post by PropellerBoy
petewood! I''m surprised!

if(argv[1][0] == ''-'' &&
argv[1][1] == ''u'' &&
argv[1][2] == 0)

This is very, very bad! You are assuming the length of the character string.



Actually it''s OK since the string will be NULL terminated, and if''s are lazily evaluated (i.e. it''ll jump out of the comparisons as soon as it finds one that''s false) you''ll be OK.

Still it''s not a very good way of doing it, because it''s pretty hard to read, and everybody knows what strcmp is for, it''s a far more readable way of doing it.

If I had my way, I''d have all of you shot!


codeka.com - Just click it.

Zipster
Zipster
You might also want to consider stricmp if both "-u" and "-U" mean the same thing, ie. case-insensitive.
Qw3r7yU10p!
Qw3r7yU10p!
quote:
Original post by PropellerBoy: petewood! I'm surprised!
...
This is very, very bad! You are assuming the length of the character string. Best to use 'strcmp'.


Thank you for being surprised!

I was merely giving the poster a solution which worked and was safe (As Dean Harding pointed out the short ciruit evaluation stops you over-running the array).

Also the original poster had further said they didn't want to use the library function.

Personally I'd use std::string.

[edited by - petewood on April 29, 2003 6:40:25 AM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.