Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get cmdline arguments with getopt? 1

Status
Not open for further replies.

frag

Programmer
Dec 7, 2000
321
GB
Hi!

I need to grep some cmdline arguments for a c++ program.
I know how to handle this argc and argv - stuff...
but I heard of a thing called 'getopt', which should be much more comfortable.

How does this work? Can someone please drop me some lines of code?

Thanx!

frag patrick.metz@epost.de
 
This is from one of my c progs:


main(int argc, char *argv[])
{
int opt;

while ((opt = getopt(argc, argv, "df:l:qr")) != -1) {
switch (opt) {

case 'd' :
g_wbDebugFlag = 1;
break;

case 'f' :
filename = optarg;
break;

case 'l' :
logfile = optarg;
break;

case 'q' :
g_wbQuietFlag = 1;
break;

case 'r' :
g_wbRejectFlag = 1;
break;

default:
errcount++;
break;

}
}

This is how it's used:

usage: ./wbin [-d] [-f filename] [-l logfile] [-q] [-r]
-d Enable debugging information.
-f Input filename; default is WPR110D1.dat.
-l Set logfile name.
-q Enable quiet logging; do not dump logging to the screen.
-r Dump rejected records to filename.rej.

See the man page for getopt for more info->
shell%man getopt As always, I hope that helped!

Disclaimer:
Beware: Studies have shown that research causes cancer in lab rats.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top