command line options

Peter J Desnoyers peter at athena.mit.edu
Tue Apr 5 11:37:19 AEST 1988


I finally remembered the operation of the argument parser I mentioned
in a previous posting. It was used as follows: [pretend 1-column text]

#define OPT    0	optarray[OPT]    = { "O", "Optimize", BOOLEAN};
#define MAXERR 1	optarray[MAXERR] = { "m", "maxerr",   DECIMAL};
#define LIST   2	optarray[LIST]   = { "l", "list",     STRING};

This was usually done in the .h file for the main module. Then you
would call get_args( argc, argv [,optarray?]) and it would shove
everything into a global, but hidden, array. Finally, you would
reference the resulting values with macros, sort of like:

if (SET_P( LIST))			/* works for -l=list.out    	*/
  list_file = fopen( VALUE( LIST),"w"); /* and       -list=list.out 	*/
compile( ARG( 1));			/* handles args mixed with opts	*/
if (++errnum > VALUE( MAXERR))		/* parsed decimal value		*/
  go_up_in_flames();			/*   -maxerr=10, -m=10		*/
if (SET_P( OPT))			/* simple flag '-O'		*/
  go_off_and_optimize();

and suchlike. The global arrays were a kludge, but you need some type
of sort-of-global flag for something like -O, and it will probably be
a kludge, anyway. The only problem was that it wasn't re-entrant, and
it supported a weird argument-passing style. (sort of like VMS but
with '-' instead of '/'.)


				Peter Desnoyers
				peter at athena.mit.edu



More information about the Comp.unix.wizards mailing list