Warning! This site is under development.
Approximate overview
argc
and argv in C) andgetopt is the primary library and getopt_long is an extended
version of it.Here’s the declaration of getopt
int getopt(int argc, char * const argv[],
const char *optstring);
extern char *optarg;
extern int optind, opterr, optopt;
What’s going on here?
argc is the number of options (the length of argv)argv is an array of strings which represent what was typed on
the command line (spaces generally separate the strings)optstring is a list of legal options.
int and not char.const in the declaration of argv?
const in the declaration of optstring?
optarg? Is the argument for flags that have arguments or
optional arguments.optopt? A way to report errors back to the caller. (It’s
C, since we only get to return one value, we “return” other values in
globals.)
getopt always returns ‘?’optind.opterr? How you tell getopt whether or not you want it to
print errors. By default, it’s a number that means “print errors”.
If you set it to 0, it doesn’t print errors.optind? The index last used (or next used); the state of
getopt.
getopt programStolen from the getopt man page.
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
int
main (int argc, char *argv[])
{
int flags, opt;
int nsecs, tfnd;
nsecs = 0;
tfnd = 0;
flags = 0;
while ((opt = getopt (argc, argv, "nt:")) != -1)
{
switch (opt)
{
case 'n':
flags = 1;
break;
case 't':
nsecs = atoi (optarg);
tfnd = 1;
break;
default: /* '?' */
fprintf (stderr, "Usage: %s [-t nsecs] [-n] name\n",
argv[0]);
exit (EXIT_FAILURE);
} // switch
} // while
printf ("flags=%d; tfnd=%d; nsecs=%d; optind=%d\n",
flags, tfnd, nsecs, optind);
if (optind >= argc)
{
fprintf (stderr, "Expected argument after options\n");
exit (EXIT_FAILURE);
}
exit (EXIT_SUCCESS);
} // main
Our goal: Understand what getopt does in a variety of situations.
We’ll print out the status after each call to getopt and not much more.
int opt = 0;
while ((opt = getopt (argc, argv, "abc:d:ef?")) != -1)
{
fprintf (port, "Status\n");
fprintf (port, " opt: '%c' (%d)\n", opt, opt);
fprintf (port, " optopt: '%c' (%d)\n", optopt, optopt);
fprintf (port, " optind: %d\n", optind);
fprintf (port, " optarg: \"%s\"\n", optarg);
fprintf (port, " argv: ");
fprintstrings (port, argv, argc);
fprintf (port, "\n\n");
}
TPS: What experiments would you like to run?
./getopt-experiment -a - see what happens with just a flag./getopt-experiment foo -a - see what happens if the flag isn’t first./getopt-experiment -abd yeah - Combine arguments together./getopt-experiment -c foo bar baz - Give multiple arguments./getopt-experiment foo bar baz -a - Flag after the arguments./getopt-experiment -f - no optional argument./getopt-experiment -f foo - optional argument [NOT WORKING AS EXPECTED]./getopt-experiment -c foo -c bar - repeat an argument./getopt-experiment -cfilename - argument attached./getopt-experiment -abcfilename - argument attached./getopt-experiment foo -b bar -a baz -a - Flag mixed into arguments./getopt-experiment foo -q bar -r baz -a - Illegal flags./getopt-experiment foo bar baz - Flag after the arguments./getopt-experiment -d - missing required argument./getopt-experiment - nothing at all./getopt-experiment -- -a -b -c cat - special dashdash flag./getopt-experiment - -a -b -c dog - special dashdash flagEvaluation forms may be found at https://grinnell.smartevals.com.
End-of-course ratings enable you to give responsible feedback for your professors, and the information you provide enters into future contract reviews. The agree/disagree responses will be tallied to produce frequency reports. The instructor will be able to review your unidentified comments within the electronic course evaluation system. Please note that the scale starts with “Strongly Disagree” at the top. Be careful not to inadvertently reverse your responses. Please provide comments but do not write your name in the comment boxes. Instructors receive the unidentified, completed forms only after grades have been submitted to the registrar.
Note that Sam’s form has a few extra questions.
No, you don’t have a final.