getopt 无法识别 c 中的多个命令行标志
getopt not recognizing multiple command line flags in c
我正在学习 C,我正在尝试使用 getopt() 来获取命令行标志。我的问题是它只会将第一个命令标志识别为标志,并将其他任何命令视为常规命令行参数。这是我的代码:
#include <stdio.h>
#include <unistd.h>
#include <string.h>
int main(int argc, char *argv[]) {
char *delivery = "";
int thick = 0;
int count = 0;
char ch;
while ((ch = getopt(argc, argv, "d:t")) != -1) {
switch (ch) {
case 'd':
delivery = optarg;
break;
case 't':
thick = 1;
break;
default:
fprintf(stderr, "Unknown option: '%s'\n", optarg);
return 1;
}
argc -= optind;
argv += optind;
}
if(thick) {
puts("Thick crust.");
}
if (delivery[0]) {
printf("To be delivered %s.\n", delivery);
}
puts("Ingredients:");
for(count = 0; count < argc; count++) {
if (!strstr(argv[count], "./")) {
puts(argv[count]);
}
}
return 0;
}
当我做一个标志或不做任何标志时,它完全可以正常工作:
$ ./order_pizza Anchovies
Ingredients:
Anchovies
$ ./order_pizza Anchovies Pineapple
Ingredients:
Anchovies
Pineapple
$ ./order_pizza -d now Anchovies Pineapple
To be delivered now.
Ingredients:
Anchovies
Pineapple
$ ./order_pizza -t Anchovies Pineapple
Thick crust.
Ingredients:
Anchovies
Pineapple
但是,当我做不止一个标志时:
$ ./order_pizza -d now -t Anchovies Pineapple
To be delivered now.
Ingredients:
-t
Anchovies
Pineapple
$ ./order_pizza -t -d now Anchovies Pineapple
Thick crust.
Ingredients:
-d
now
Anchovies
Pineapple
我似乎无法弄清楚我做错了什么,因为从我的搜索来看,似乎没有人遇到同样的问题。我在 Windows 7 上使用 cygwin 并使用以下行进行编译:
$ gcc order_pizza.c -o order_pizza
有人有什么想法吗?
不要在调用 getopt
的 while
循环中修改 argc
和 argv
。它使用这些变量来发挥它的魔力,所以改变它们会把它搞砸。
所以不是这个:
while ((ch = getopt(argc, argv, "d:t")) != -1) {
switch (ch) {
...
}
argc -= optind;
argv += optind;
}
这样做:
while ((ch = getopt(argc, argv, "d:t")) != -1) {
switch (ch) {
...
}
}
argc -= optind;
argv += optind;
我正在学习 C,我正在尝试使用 getopt() 来获取命令行标志。我的问题是它只会将第一个命令标志识别为标志,并将其他任何命令视为常规命令行参数。这是我的代码:
#include <stdio.h>
#include <unistd.h>
#include <string.h>
int main(int argc, char *argv[]) {
char *delivery = "";
int thick = 0;
int count = 0;
char ch;
while ((ch = getopt(argc, argv, "d:t")) != -1) {
switch (ch) {
case 'd':
delivery = optarg;
break;
case 't':
thick = 1;
break;
default:
fprintf(stderr, "Unknown option: '%s'\n", optarg);
return 1;
}
argc -= optind;
argv += optind;
}
if(thick) {
puts("Thick crust.");
}
if (delivery[0]) {
printf("To be delivered %s.\n", delivery);
}
puts("Ingredients:");
for(count = 0; count < argc; count++) {
if (!strstr(argv[count], "./")) {
puts(argv[count]);
}
}
return 0;
}
当我做一个标志或不做任何标志时,它完全可以正常工作:
$ ./order_pizza Anchovies
Ingredients:
Anchovies
$ ./order_pizza Anchovies Pineapple
Ingredients:
Anchovies
Pineapple
$ ./order_pizza -d now Anchovies Pineapple
To be delivered now.
Ingredients:
Anchovies
Pineapple
$ ./order_pizza -t Anchovies Pineapple
Thick crust.
Ingredients:
Anchovies
Pineapple
但是,当我做不止一个标志时:
$ ./order_pizza -d now -t Anchovies Pineapple
To be delivered now.
Ingredients:
-t
Anchovies
Pineapple
$ ./order_pizza -t -d now Anchovies Pineapple
Thick crust.
Ingredients:
-d
now
Anchovies
Pineapple
我似乎无法弄清楚我做错了什么,因为从我的搜索来看,似乎没有人遇到同样的问题。我在 Windows 7 上使用 cygwin 并使用以下行进行编译:
$ gcc order_pizza.c -o order_pizza
有人有什么想法吗?
不要在调用 getopt
的 while
循环中修改 argc
和 argv
。它使用这些变量来发挥它的魔力,所以改变它们会把它搞砸。
所以不是这个:
while ((ch = getopt(argc, argv, "d:t")) != -1) {
switch (ch) {
...
}
argc -= optind;
argv += optind;
}
这样做:
while ((ch = getopt(argc, argv, "d:t")) != -1) {
switch (ch) {
...
}
}
argc -= optind;
argv += optind;