在C中使用getopt()和switch语句

在C中使用getopt()和switch语句,c,switch-statement,arguments,getopt,C,Switch Statement,Arguments,Getopt,我是C语言的新手,尝试将getopt与switch语句结合使用。我想我的问题只是语法问题,但我似乎无法解决。我需要像这样运行我的程序: $/webserver-p8080-r/my/root/dir。 所以我需要识别-p,得到8080,识别-r,得到/my/root/dir。目前,我的代码先找到-p,然后找到-r,然后检测到未知选项并退出。这是代码。我有put()用于测试 #define USAGE_STRING "Usage: webserver -p <port> -r

我是C语言的新手,尝试将getopt与switch语句结合使用。我想我的问题只是语法问题,但我似乎无法解决。我需要像这样运行我的程序:
$/webserver-p8080-r/my/root/dir
。 所以我需要识别-p,得到8080,识别-r,得到/my/root/dir。目前,我的代码先找到-p,然后找到-r,然后检测到未知选项并退出。这是代码。我有
put()
用于测试

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


}
#定义用法\u字符串“用法:webserver-p-r\n”
字符*端口;
char*rootdir;
//处理命令行参数。使用GNU getopt()函数。
void processargs(int argc,char**argv){
int next_选项;
做{
next_option=getopt(argc,argv,“pr:”);
如果(下一个选项!=-1){
开关(下一个选项)
{
案例'p':/*-p--端口选项*/
看跌期权(“p”);
端口=optarg;
printf(“%s”,端口);
案例'r'://-r--根目录选项
看跌期权(“r”);
rootdir=optarg;
printf(“%s”,rootdir);
违约:
看跌期权(“未知”);
/*检测到未知选项。将其打印到标准
输出,退出时退出代码为零(正常终止)*/
fprintf(标准字符,用法字符串);
出口(1);
}
}
}while(下一个_选项!=-1);
printf(“%s”,端口);
如果(端口==NULL){
fprintf(标准字符,用法字符串);
出口(1);
}
if(rootdir==NULL){
看跌期权(“未知”);
fprintf(标准字符,用法字符串);
出口(1);
}
}
当前,当我运行上述命令时,我得到了这个输出(使用puts进行测试)

p
R
未知的
用法:webserver-p-r

谢谢你的帮助

我相信您希望在每个案例的结尾使用
break
,否则执行将进入下一个案例块。

C switch语句需要在每个案例的结尾使用
break
,否则将执行以下案例中的代码。因此:

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);
}
必须更改为:

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

首先,你应该使用
getopt_long()
我相信
getopt()
已经贬值了。switch语句中也缺少
break
。如果没有破发,该案件下的每一个案件都将被执行。虽然可以使用do-While循环,但使用While循环更简单

下面是一个简单的命令行解析器,您可以在此基础上进行构建

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

您的
switch
语句缺少每个
案例末尾的
break
。此外,您的选项字符串应该是
“p:r:
,而不是
”pr:“
@irrrgg我包括了中断,并将选项字符串更改为
p:r
,但它仍然无法识别
rootdir
。我遗漏了什么吗?这会为结构初始化中的每个{抛出此错误。警告:
初始化从整数生成指针,但没有强制转换[默认情况下启用]{“端口”,0,必需的参数,'p'},
^抱歉,我试图从内存写入它,并犯了一些愚蠢的错误,但请立即尝试。
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;
}