command line parsing

Randy Orrison randy at umn-cs.cs.umn.edu
Wed Apr 6 13:31:13 AEST 1988


This was posted to comp.unix.questions (?) because someone sent the followup
there.  (why?  the discussion is here...)

I have an idea, though I have not coded nor tested it.

	int getopt (char ***argv, int *argc, char *format, ...);

(you may not want to call it getopt for obvious reasons).

The routine, when called, would pull one argument out of the command line
and return an error/status code.  For example:

	if (getopt (&argv, &argc, "-o %s", OutFile)) {
		process error return
	}

	if (getopt (&argv, &argc, "=O", &OptFlag)) {
		process error return
	}

	if (getopt (&argv, &argc, "=pascal", &PascalFlag))

	if (getopt (&argv, &argc, "-origin %d %d", &x, &y))

perhaps even
	while (!getopt (&argv, &argc, "%s.c", CFile[i++]))
		;

The routine would be stateless, could be written to handle any sort of
arguments you wanted, and would require the information to be present in
only one place.

I don't really have time to code this, but if someone would like me to write
up a simplified version to demonstrate, I could maybe do that.

	-randy
-- 
Randy Orrison, Control Data, Arden Hills, MN		randy at ux.acss.umn.edu
(Anyone got a Unix I can borrow?)   {ihnp4, seismo!rutgers, sun}!umn-cs!randy
Electrical Engineers do it with less resistance.



More information about the Comp.lang.c mailing list