Warning! This site is under development.
This class will be recorded! Its use will be limited to members of the class. Please do not share with others.
Approximate overview
-wordflag with only one dash.What flags have you become accustomed to using / seeing?
-v or --verbose: Report information while the program is running
(frequently to help with tracing problems or errors).-o file or --output file: Name the/an output file-i file or --input file: Name the/an input file.
-a: “All”, depending on what all means in the particular context.--version: Give the version number. Whoops. Overlaps verbose.-h or --help: Give help or options.And some less common ones
-p or --pipe: Print to standard outputint
main (int argc, char *argv[])
{
} // main
argc is the number of command-line parameters (including the command)argv is the parameters themselves as stringsint
main (int argc, char *argv[])
{
for (int i = 0; i < argc; i++)
{
printf ("%2d: %s\n", i, argv[i]);
} // for i
// And we're done
return 0;
} // main
$ ./printargs -r hello world -f 23
How big is argc? 6!
$ ls H*
Hello world Hi
How big is argc? 3
What do you expect the output to be?
0: ./printargs
1: Hello world
2: Hi
Possibility
0: ./printargs
1: H*
We got
$ ./printargs H*
0: ./printargs
1: Hello world
2: Hi
Note: the H* is expanded by the shell and the different file names are passed to the printargs procedures as separate “arguments”.
Many C programmers write the test as (CONSTANT == EXP) rather than than
(EXP == CONSTANT) because history suggests that we sometimes forget an
equals sign, and, say, (x = 2) is a legal test. However, (2 = x) is
invalid and the compiler tells you that.
In C, && is processed left to right and does short-circuit evaluation, so we can safely write.
Many people write (! var) rather than (0 != var) or (NULL != var).
for (int j = 1; j < strlen (argument); j++) is inefficient, since it
requires us to call strlen multiple times.
See our sample code in examples/command-line/arguments.c.
It was not possible to record the design discussions.
getoptFrom the man page …
#include <unistd.h>
extern char *optarg;
extern int optind;
extern int optopt;
extern int opterr;
extern int optreset;
int
getopt(int argc, char * const argv[], const char *optstring);
If “The getopt() function returns the next known option character in
optstring”, why does getopt return an int rather than a char?
As Schemers, we believe that “the same function call with the same
arguments should produce the same result”. Clearly, that shouldn’t
happen with getopt. How do we control / understand state?
Sam believes we best understand libraries by writing programs that
use them. We’ll start with some simple programs that let us explore
getopt.
Sam assumes we’ll need a break about now. We may have to continue
the getopt example after break.
getopt-longNot as much of a standard
From the man page
#include <getopt.h>
extern char *optarg;
extern int optind;
extern int optopt;
extern int opterr;
extern int optreset;
int
getopt_long(int argc, char * const *argv, const char *optstring,
const struct option *longopts, int *longindex);
struct option {
char *name;
int has_arg;
int *flag;
int val;
};