[Courses] [C] getopt() and getopt_long()

Anand R anand.r at cybertech.co.in
Mon Aug 12 10:26:12 EST 2002

Thanks Akkana. Another interesting thing that I was looking at. I was
talking to Fyodor on use of fakeargs in his program NMAP and this is what he
has to say about the rationale behind using fake_args:

-----Original Message-----
From: Fyodor [mailto:fyodor at insecure.org]
Sent: Monday, August 12, 2002 8:54 AM
To: Anand R
Subject: Re: Use of fake args

On Mon, Aug 12, 2002 at 08:50:44AM +0530, Anand R wrote:
> Fyodor,
> 	We were discussing command line args in a C tutorial mailing list
> and saw nmap using myargs and fakeargs. The rationale behind using
> would be really useful.

On some systems, you can clobber argv[] and show bogus results in 'ps'
and 'w' listings.  However, that usage is deprecated in favor of
nmap's --interactive option.


-----Original Message-----
From: Akkana [mailto:akkana at shallowsky.com]
Sent: Saturday, August 10, 2002 10:51 PM
To: 'courses at linuxchix.org'
Subject: Re: [Courses] [C] getopt() and getopt_long()

Anand R writes:
> 	Could we have a discussion on using parsing command line arguments.
> Typically, we have programs today, that use getopt() and getopt_long() for
> parsing command line arguments (they have made life a lot more easier for
> us). A small discussion on best practices, cute hacks and
> guidelines/pitfalls/best practices would be really useful and would
> benefit newbies. 

I usually "cheat" with getopt -- I can never remember the syntax (and
it seems to vary between versions, and of course between languages),
so I go find a program that uses it, grab a few lines and stuff them
into my program, then edit appropriately.

Lately I've been poking around the argument parsing of some of the
pilot-link programs (pilot-link.org, used for syncing Palm Pilots)
since the latest version has reworked a lot of the command arguments
to be much more consistent, but a few programs still need cleaning up.
The getopt use in these programs generally looks like this (from
pilot-xfer.c, the main program in pilot-link) and it's pretty easy
to read this code or adapt it to other programs:

struct option options[] = {
	{"help",        no_argument,       NULL, 'h'},
	{"version",     no_argument,       NULL, 'v'},
	{"port",        required_argument, NULL, 'p'},
[ ... ]
	{NULL,          0,                 NULL, 0}

static const char *optstring = "-hvp:b:u:s:Sr:i:m:f:d:e:PlLa:x:FOIqc";

[ ... ]

int main(int argc, char *argv[])
[ ... ]
	while ((c = getopt_long(argc, argv, optstring, options, NULL)) !=
-1) {
		switch (c) {

		/* This means a field is unknown, could be multiple arg * */
		case 1:
			if (last_c=='i') {
			/* else { Unknown param, let it go by quietly } * */
		case 'h':
			return 0;
		case 'v':
			return 0;
		case 'p':
			port = optarg;
[ ... ]

and so forth.  It's mostly pretty straightforward (except the "case 1"),
but for some reason I have a hard time remembering the syntax so I find
it much easier to stea^H^H^H^Hcopy.

I do use the _long version, both in C and perl, because it's not
that much harder to use, and it seems more civilized to support both
long and short format arguments.

The "case 1" there looks lke a nifty trick to handle multiple arguments
(which getopt otherwise doesn't deal with gracefully.  I never noticed
it before, but I'm going to use it myself now that I know about it. :-)


More information about the Courses mailing list