首页 > > 详细

辅导command-line、讲解C/C++编程、讲解arbitrary program、c++辅导 讲解R语言程序|解析C/C++编程

2019/1/29 Working with getopt()
https://cis.technikum-wien.at/documents/bel/1/prg/semesterplan/tasks/content/getopt.html 1/3
Working with getopt()
The function getopt() allows to implement programs with command-line arguments in a
more flexible manner. In particular, it releaves the user of the program from remebering the
correct order of the arguments and whether arguments are optional or mandatory. The
command-line arguments need to be pre-fixed with an additional - argument, e.g., -i
argument .
The following usage example shows an arbitrary program:
The first example prints a help message - we use only -h without additional argument.
$ prog.exe -h
Next, we use the program with four arguments - three with an additional arguments following
the -i , -o , and -p , respectively. The fourth argument -r doesn’t request an additional
argument.
$ prog.exe -i infile.txt -o ofile.txt -p 10 -r
The third example is the same as the one before, however, with a different order of the
arguments.
$ prog.exe -p 10 -o ofile.txt -r -i infile.txt
Using getopt()
The following listing shows how to use getopt():2019/1/29 Working with getopt()
https://cis.technikum-wien.at/documents/bel/1/prg/semesterplan/tasks/content/getopt.html 2/3
#include
#include
#include
#include
#include
#include
void print_help (void);
int main (int argc, char *argv[])
{
int opt;
int parg = INT_MIN;
_Bool rarg = false;
char *iarg = "";
char *oarg = "";
// no arguments given
if (argc == 1) {
fprintf (stderr, "This program needs arguments....\n\n");
print_help();
}
// use getopt to evaluate the parameters
while ( (opt = getopt (argc, argv, "i:o:p:rh")) != -1) {
switch (opt) {
case 'i':
iarg = optarg;
break;
case 'o':
oarg = optarg;
break;
case 'p':
sscanf (optarg, "%d", &parg);
break;
case 'r':
rarg = true;
break;
case 'h':
print_help();
break;
case ':':
fprintf (stderr, "Option requires an argument.\n");
print_help ();
break;
case '?':
fprintf (stderr, "Unknown option character %c.\n", optopt);
print_help ();
}
}
for (; optind < argc; optind++)
printf ("Positional argument %d: %s\n", optind, argv[optind]);
// now we print the parameters we have got
if (strcmp (iarg, "") != 0)
printf ("The program %s was invoked with -i %s\n", argv[0], iarg);
C2019/1/29 Working with getopt()
https://cis.technikum-wien.at/documents/bel/1/prg/semesterplan/tasks/content/getopt.html 3/3
The getopt() function gets the arguments listed as a string. Every "-" argument is listed by
the respective letter. When one of these "-" arguments requests an additional argument a
colon is added after the respective letter; in the above example the arguments -i, -o, and -p
request an additional argument. Note - only one additional argument can be requested.
For every argument add a respective case statement. Often it is advisible to copy the
"additional argument" to a variable for post processing after the switch statement.
if (strcmp (oarg, "") != 0)
printf ("The program %s was invoked with -o %s\n", argv[0], oarg);
if (parg != INT_MIN)
printf ("The program %s was invoked with -p %d\n", argv[0], parg);
if (rarg != false)
printf ("The program %s was invoked with -r\n", argv[0]);
return 0;
}
void
print_help (void)
{
printf ("\nSyntax: prog.exe [-i infile] [-o outfile] [-p value] [-r] [-h]\n");
exit (EXIT_FAILURE);
}
Last updated 2018-08-22 14:44:10 CEST

联系我们
  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp
热点标签

联系我们 - QQ: 99515681 微信:codinghelp
程序辅导网!