联系方式

  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-23:00
  • 微信:codinghelp

您当前位置:首页 >> C/C++编程C/C++编程

日期:2019-01-06 10:14

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/4 Working with getopt()

https://cis.technikum-wien.at/documents/bel/1/prg/semesterplan/tasks/content/getopt.html 2/3

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <limits.h>

#include <stdbool.h>

#include <unistd.h>

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);

C

2019/1/4 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


版权所有:留学生编程辅导网 2020 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。 站长地图

python代写
微信客服:codinghelp