[comp.lang.c] command line parsing

randy@umn-cs.cs.umn.edu (Randy Orrison) (04/06/88)

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@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.