Friday, May 14, 2010

Creating command line arguments to control in c++?

i'm trying to write it so that in main i can take optional command-line arguments that control whether my program does something or not


for example


when i ./a.out -help


./a.out being the program


-help being the command


i need to print out a list of options when i put in the -help





how do i program this into main in C++? i know i'm supposed to use argv[] or argc but i'm not sure which or howCreating command line arguments to control in c++?
create your main to use command line arguments:





int main(int agrc, char **argv)


{


return 0;


}





Now when a user types:


./a.out -help -answer=42





The variable argc will contain 3.


The variable argv will contain


argv[0] = ';./a.out';


argv[1] = ';-help';


argv[2] = ';-answer=42';





That's the easy part. The harder part is interpreting what the user passed in. If the user is passing integers or real numbers, look into atoi() and atof(). Otherwise, you can just keep the arguments as strings.





For more advanced handling, look into a standard library called getopt. getopt contains functions that do the parsing for you as well as specify which options are required and which are optional.Creating command line arguments to control in c++?
SWF to Video Scout - Command Line a


H - run program in automated mode


/S - set output video width and height in form of WIDTHxHEIGHT, for example: /H320x240


/F - set output video FPS. For example: /F15


/N - do not capture sound

No comments:

Post a Comment