v20i005: getopt.h - simple interface to getopt, Part01/01

dm at bloom-beacon.mit.edu dm at bloom-beacon.mit.edu
Wed May 22 13:35:24 AEST 1991


Submitted-by: Dave Mankins <dm at think.com>
Posting-number: Volume 20, Issue 5
Archive-name: getopt.h/part01

Yet another getopt ...

Below is a macro package I wrote the second time I had to refer to the
getopt(3) man page to remind myself how getopt() is used.  I think this
simplifies the use of getopt tremendously.

Stick it into getopt.h in your personal toolkit.

david mankins (dm at think.com)
----
#! /bin/sh
# This is a shell archive.  Remove anything before this line, then feed it
# into a shell via "sh file" or similar.  To overwrite existing files,
# type "sh file -c".
# The tool that generated this appeared in the comp.sources.unix newsgroup;
# send mail to comp-sources-unix at uunet.uu.net if you want that tool.
# Contents:  getopt.h
# Wrapped by kent at sparky on Tue May 21 22:31:30 1991
PATH=/bin:/usr/bin:/usr/ucb ; export PATH
echo If this archive is complete, you will see the following message:
echo '          "shar: End of archive."'
if test -f 'getopt.h' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'getopt.h'\"
else
  echo shar: Extracting \"'getopt.h'\" \(1222 characters\)
  sed "s/^X//" >'getopt.h' <<'END_OF_FILE'
X#ifndef GETOPT
X/*****************************************************************************
X *
X *  GETOPT, USES_GETOPT
X *
X *	Usage:
X *	     GETOPT(c, str) {
X *	     case 'a':
X *		code for option a...
X *	     case 'b':
X *		code for option b...
X *	     case '?':
X *		code for unknown option...
X *	     }
X *	     GETOPT_LOOP_REST(strp) {
X *		loop over the remaining arguments (typically file names), that
X *		is, the ones at the end of the line that aren't prefixed with
X *		hyphens... 
X *	     }
X *
X *	requires that argc, and argv have their usual names (and are in the
X *	same scope as the use of GETOPT).
X *     
X *	Expects a USES_GETOPT within the same scope to declare the getopt
X *	variables.
X *	     
X *****************************************************************************/
X#define USES_GETOPT	extern char *optarg; extern int optind, opterr
X#define GETOPT(c, str)	while((c=getopt(argc, argv, str)) != -1) switch(c)
X
X/*
X * Loop over the REST of the arguments
X *
X * (this has got to be one of the more egregious uses of the ternary ?: ---
X * however, you only have to understand it once.)
X */
X#define GETOPT_LOOP_REST(argp) \
X	while((optind < argc)? (argp = argv[optind++]): (argp = NULL))
X
X#endif /* GETOPT */
END_OF_FILE
  if test 1222 -ne `wc -c <'getopt.h'`; then
    echo shar: \"'getopt.h'\" unpacked with wrong size!
  fi
  # end of 'getopt.h'
fi
echo shar: End of archive.
exit 0
exit 0 # Just in case...
-- 
Kent Landfield                   INTERNET: kent at sparky.IMD.Sterling.COM
Sterling Software, IMD           UUCP:     uunet!sparky!kent
Phone:    (402) 291-8300         FAX:      (402) 291-4362
Please send comp.sources.misc-related mail to kent at uunet.uu.net.



More information about the Comp.sources.misc mailing list