[英]Using getopt() and switch statements in C
I'm new to C and trying to use getopt combined with a switch statement. 我是C语言的新手,尝试将getopt与switch语句结合使用。 I think my problem is just a syntax one but I can't seem to figure it out.
我认为我的问题只是语法问题,但我似乎无法弄清楚。 I need to run my program like this :
$/webserver -p 8080 -r /my/root/dir
. 我需要这样运行程序:
$/webserver -p 8080 -r /my/root/dir
。 So I need to recognize the -p and get the 8080 and the recognize the -r and get the /my/root/dir. 因此,我需要识别-p并获取8080,并识别-r并获取/ my / root / dir。 Currently my code finds the -p and then finds the -r and then detects an unknown option and exits.
当前,我的代码找到-p,然后找到-r,然后检测到未知选项并退出。 Here is the code.
这是代码。 I have the
puts()
in there for testing. 我在那里
puts()
了puts()
进行测试。
#define USAGE_STRING "Usage: webserver -p <port> -r <rootdir>\n"
char *port;
char *rootdir;
// Process command line arguments. Uses GNU getopt() function.
void processargs(int argc, char **argv) {
int next_option;
do {
next_option = getopt(argc, argv, "pr:");
if (next_option != -1) {
switch (next_option)
{
case 'p': /* -p -- port option */
puts("p");
port = optarg;
printf("%s", port);
case 'r': // -r -- root directory option
puts("r");
rootdir = optarg;
printf("%s", rootdir);
default:
puts("unknown");
/* Unknown option detected. Print it to standard
output, and exit with exit code zero (normal termination). */
fprintf(stderr, USAGE_STRING);
exit(1);
}
}
} while (next_option != -1);
printf("%s", port);
if (port == NULL) {
fprintf(stderr, USAGE_STRING);
exit(1);
}
if (rootdir == NULL) {
puts("unknown");
fprintf(stderr, USAGE_STRING);
exit(1);
}
}
Currently when I run the above command I get this output (using the puts for testing). 当前,当我运行上面的命令时,我得到此输出(使用puts进行测试)。
p
r
unknown
Usage: webserver -p <port> -r <rootdir>
Thanks for any help! 谢谢你的帮助!
我相信您希望在每种情况下都使用break
,否则执行将进入下一个case块。
AC switch statement requires a break
at the end of each case, otherwise the code in the following case is executed. AC switch语句在每种情况下都需要
break
,否则将执行以下情况下的代码。 So this: 所以这:
switch (next_option)
{
case 'p': /* -p -- port option */
puts("p");
port = optarg;
printf("%s", port);
case 'r': // -r -- root directory option
puts("r");
rootdir = optarg;
printf("%s", rootdir);
default:
puts("unknown");
/* Unknown option detected. Print it to standard output, and exit with exit code zero
(normal termination). */
fprintf(stderr, USAGE_STRING);
exit(1);
}
Must be changed to this: 必须更改为此:
switch (next_option)
{
case 'p': /* -p -- port option */
puts("p");
port = optarg;
printf("%s", port);
break;
case 'r': // -r -- root directory option
puts("r");
rootdir = optarg;
printf("%s", rootdir);
break;
default:
puts("unknown");
/* Unknown option detected. Print it to standard output, and exit with exit code zero
(normal termination). */
fprintf(stderr, USAGE_STRING);
exit(1);
}
First you should be using getopt_long()
I believe getopt()
is depreciated. 首先,您应该使用
getopt_long()
我相信getopt()
已弃用。 You are also missing break
in your switch statement. 您还在switch语句中缺少
break
。 With out break each case beneath that one will be executed. 在不中断的情况下,将执行该案件以下的每个案件。 While you could use a do-while loop it's simpler to just use a while loop.
虽然可以使用do-while循环,但仅使用while循环会更简单。
Here's a simple command line parser you can build on. 这是可以构建的简单命令行解析器。
static const char *optstring = "p:r:";
static struct option longopts[] =
{
{ "port", required_argument, NULL, 'p' },
{ "root", required_argument, NULL, 'r' },
{ NULL, 0, NULL, 0 }
};
int parseInput(int argc, char * const argv[])
{
int ch;
/* Main Loop ie the Parser */
while ((ch = getopt_long(argc, argv, optstring, longopts, NULL)) != -1)
{
switch(ch)
{
case 'p':
printf("arg: %s\n", optarg);
break;
case 'r':
printf("arg: %s\n", optarg);
break;
default:
printf("Unsupported option\n");
return -1;
}
}
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.