#include <iostream>
#include <unistd.h>
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
extern char *optarg;
extern int optind, opterr, optopt;
void test01(int argc, char *argv[])
{
int opt = -1;
while (opt = getopt(argc, argv, "abc:d::") != -1)
{
cout << "opt: " << opt << endl;
cout << "optarg:" << optarg << endl;
cout << "optopt:" << optopt << endl;
cout << "optind:" << optind << endl;
}
cout << "opterr:" << opterr << endl;
}
void test02(int argc, char *argv[])
{
int verbose = 0;
int n = 0;
char opt;
while ((opt = getopt(argc, argv, "vn:")) != -1)
{
switch (opt)
{
case 'v':
verbose = 1;
break;
case 'n':
n = atoi(optarg);
cout << "n : " << n << endl;
break;
default:
fprintf(stderr, "usage: error");
exit(-1);
}
}
int i = 0;
for (i = 0; i < n; ++i)
{
if (verbose)
cout << i << endl;
}
cout << "Done counting to " << n << endl;
}
int main1(int argc, char *argv[])
{
if (argc < 2)
return -1;
test02(argc, argv);
return 0;
}
int main(int argc, char *argv[])
{
int c;
int digit_optind = 0;
while (1)
{
int this_option_optind = optind ? optind : 1;
int option_index = 0;
static struct option long_options[] = {
{"add", required_argument, 0, 0},
{"append", no_argument, 0, 0},
{"delete", required_argument, 0, 0},
{"verbose", no_argument, 0, 0},
{"create", required_argument, 0, 'c'},
{"file", required_argument, 0, 0},
{0, 0, 0, 0}
};
c = getopt_long(argc, argv, "abc:d:012", long_options, &option_index);
if (c == -1)
break;
switch (c)
{
case 0:
if (strcmp(long_options[option_index].name, "add") == 0)
fprintf(stdout, "long option \"add\" value: %s\n", optarg);
else if (strcmp(long_options[option_index].name, "append") == 0)
fprintf(stdout, "long option \"append\"\n");
else if (strcmp(long_options[option_index].name, "delete") == 0)
fprintf(stdout, "long option \"delete\" value: %s\n", optarg);
else if (strcmp(long_options[option_index].name, "create") == 0)
fprintf(stdout, "long option \"create\" value: %s\n", optarg);
else if (strcmp(long_options[option_index].name, "verbose") == 0)
fprintf(stdout, "long option \"verbose\"\n");
else if (strcmp(long_options[option_index].name, "file") == 0)
fprintf(stdout, "long option \"file\" value: %s\n", optarg);
break;
case '0':
case '1':
case '2':
if (digit_optind != 0 && digit_optind != this_option_optind)
fprintf(stdout, "digits occur in two different argv elements.\n");
digit_optind = this_option_optind;
fprintf(stdout, "option %c\n", c);
break;
case 'a':
fprintf(stdout, "option a\n");
break;
case 'b':
fprintf(stdout, "option b\n");
break;
case 'c':
fprintf(stdout, "option c with value '%s'\n", optarg);
break;
case 'd':
fprintf(stdout, "option d with value '%s'\n", optarg);
break;
case '?':
break;
default:
fprintf(stdout, "?? getopt returned character code 0%o ??\n", c);
}
}
if (optind < argc)
{
fprintf(stdout, "non-option argv elements: ");
while (optind < argc)
fprintf(stdout, "%s ", argv[optind++]);
fprintf(stdout, "\n");
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)