是否可以使用getopt_long来解析类似于C程序中的命令行参数的字符串数组?

时间:2015-07-23 14:46:22

标签: c getopt getopt-long

我知道应该使用getopt来解析命令行参数,而不是字符串。

然而,我感到困惑的是,如果我传递一个“看起来像”argv变量的字符串数组,getopt_long似乎可以工作,但只是我第一次调用它 。对于第二个以及随后的所有时间,我称之为忽略任何参数并返回默认参数。

我这样做的原因是我正在转变一个应用程序,该应用程序曾经在它的“交互”版本中接受命令行参数,要求用户提供一些参数,然后根据这些参数执行一些操作,然后请求更多的论据等等。

以下代码是重现错误的最小示例

#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>

struct input_parameters{
    char * d; // data set
};

int solver_help(int argc, char* const argv[], struct input_parameters * p)
{
  int c;

  p->d = "default name";

  while (1)
  {
      static struct option long_options[] =
      {
          {"data",  required_argument,     0, 'd'},
          {0, 0, 0, 0}
      };
      /* getopt_long stores the option index here. */
      int option_index = 0;

      c = getopt_long (argc, argv, "d:",
                       long_options, &option_index);

      /* Detect the end of the options. */
      if (c == -1)
        break;

      switch (c)
      {
          case 'd': p->d = optarg;
              break;
          default:
              printf("wrong option specification\n");
              exit(-1);
      }
  }

  return 0 ;


}    


int main () {

    int argc_first = 3;
    char * const argv_first[] = { "getopt_test", "--data", "first" };
    struct input_parameters first_input;
    solver_help(argc_first, argv_first, &first_input);

    printf("I think the data is: %s\n", first_input.d);

    int argc_second = 3;
    char * const argv_second[] = { "getopt_test","--data", "second" };
    struct input_parameters second_input;
    solver_help(argc_second, argv_second, &second_input);

    printf("I think the data is: %s\n", second_input.d);

    return 0;
}

此代码的输出是

I think the data is: first
I think the data is: default name

1 个答案:

答案 0 :(得分:3)

来自getopt的手册页:

  

为了使用getopt()来评估多组参数,或者   多次评估一组参数,即变量   {2}之前必须将optreset设置为1   一组对getopt()的调用,变量optind必须是   重新初始化。

稍微修改代码会为您提供所需的结果:

#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>

struct input_parameters {
    char * d; // data set
};

int solver_help(int argc, char* const argv[], struct input_parameters * p)
{
    p->d = "default name";

    static struct option long_options[] =
    {
        {"data",  required_argument,     0, 'd'},
        {0, 0, 0, 0}
    };

    int option_index = 0;

    optreset = 1;     /*  ADD THIS  */
    optind = 1;       /*  ADD THIS  */

    int c = getopt_long(argc, argv, "d:",
                        long_options, &option_index);

    switch ( c )
    {
        case -1:
            break;

        case 'd':
            p->d = optarg;
            break;

        default:
            printf("in default case...\n");
            printf("wrong option specification\n");
            exit(EXIT_FAILURE);
    }

    return 0;
}    

int main(void) {

    int argc_first = 3;
    char * const argv_first[] = { "getopt_test", "--data", "first" };
    struct input_parameters first_input;
    solver_help(argc_first, argv_first, &first_input);

    printf("I think the data is: %s\n", first_input.d);

    int argc_second = 3;
    char * const argv_second[] = { "getopt_test","--data", "second" };
    struct input_parameters second_input;
    solver_help(argc_second, argv_second, &second_input);

    printf("I think the data is: %s\n", second_input.d);

    return 0;
}

带输出:

paul@horus:~/src/sandbox$ ./go
I think the data is: first
I think the data is: second
paul@horus:~/src/sandbox$