我的getopt.h代码在不同的情况下跳入默认情况下

发布于 2025-02-07 14:10:13 字数 2931 浏览 1 评论 0原文

我想将参数添加到我编写的程序中,因此我制作了一些新项目来学习和了解getopt.h库。

但是由于某种原因,如果我运行程序(./ parameter_test2 -n 21./ parameter_test2 -num 21),则使用正确的选项,然后跳入默认值案件。 我不知道为什么我的代码这样做。从我阅读的内容来看,默认案例仅在有未知返回代码时才会被调用。

也许你们中的一个可以向我解释发生了什么。

代码:

#include <getopt.h> /* getopt */
#include <stdlib.h> /* exit   */
#include <stdio.h>  /* printf */

void print_usage(char **argv)
{
  printf("+-----------------------------------------------------+\n");
  printf("Usage: \n\t%s [arg] [arg] [arg] ...\n",argv[0]);
  printf("\n");
  printf("\tProgramname --sender --Path /home/pi/Datei.txt\n");
  printf("\tTransceiver –s home/pi1/datei.txt\n");
  printf("\tTransceiver –r home/pi2/\n");
  printf("\n");
  printf("\t\tTransceiver [-s] SOURCE-PATH\n");

  printf("\t\tTransceiver [-r] [DESTINATION-PATH]\n");
  printf("+-----------------------------------------------------+\n");
  exit(EXIT_FAILURE);
}

int main(int argc, char **argv)
{
  
  // if no parameters are given
  if (argc == 1) {
    fprintf(stderr, "\nYou need to use options!\n\n");
    //printf("You need to use options!\n");
    
    print_usage(argv);
    }

  int next_option;

  const char *short_options ="-hn:";
    //  {"sender",        no_argument,       &f_sender,       1},
    //   {"receiver",      no_argument,       &f_receiver,     1},
    //   {"source",        required_argument, &f_source,       1},
    //   {"destination",   required_argument, NULL,  0},
    //   //{"verbose",

  const struct option long_options[] = {
    //{ name,   has_arg,            flag,  val}, 
      { "help", no_argument,        NULL, 'h' },
      { "num",  required_argument,  NULL, 'n' },
      { NULL,   0,                  NULL,  0  }
      };

  do {
      next_option = getopt_long(argc, argv, short_options, long_options, NULL);

      switch(next_option) {
          case '?':
              fprintf(stderr, "\nERROR: WRONG USAGE!\n\n");
              //print_usage(argv);
              //exit(2);
              break;

          case 'h':
              print_usage(argv);
              break;

          case 'n':
              printf("num %s\n", optarg);
              break;

          default:
              print_usage(argv);
              exit(EXIT_FAILURE);
      }   
  } while(next_option != -1);/* end of list */

  exit(EXIT_SUCCESS);
}

运行程序:

./parameter_test2 -n 21                                                                     
num 21
+-----------------------------------------------------+
Usage: 
        ./parameter_test2 [arg] [arg] [arg] ...

        Programname --sender --Path /home/pi/Datei.txt
        Transceiver –s home/pi1/datei.txt
        Transceiver –r home/pi2/

                Transceiver [-s] SOURCE-PATH
                Transceiver [-r] [DESTINATION-PATH]
+-----------------------------------------------------+

谢谢您的阅读!

I want to add arguments to a program I have written, so I have made a couple of new project to learn and understand getopt.h library.

But for some reason if I run the program (./parameter_test2 -n 21 or ./parameter_test2 --num 21) it uses the right option and then jumps into the default case.
I have no clue why my code does this. Going from what I have read of, the default-case gets only called if there is an unknown return code.

Maybe one of you can explain to me what is happening.

CODE:

#include <getopt.h> /* getopt */
#include <stdlib.h> /* exit   */
#include <stdio.h>  /* printf */

void print_usage(char **argv)
{
  printf("+-----------------------------------------------------+\n");
  printf("Usage: \n\t%s [arg] [arg] [arg] ...\n",argv[0]);
  printf("\n");
  printf("\tProgramname --sender --Path /home/pi/Datei.txt\n");
  printf("\tTransceiver –s home/pi1/datei.txt\n");
  printf("\tTransceiver –r home/pi2/\n");
  printf("\n");
  printf("\t\tTransceiver [-s] SOURCE-PATH\n");

  printf("\t\tTransceiver [-r] [DESTINATION-PATH]\n");
  printf("+-----------------------------------------------------+\n");
  exit(EXIT_FAILURE);
}

int main(int argc, char **argv)
{
  
  // if no parameters are given
  if (argc == 1) {
    fprintf(stderr, "\nYou need to use options!\n\n");
    //printf("You need to use options!\n");
    
    print_usage(argv);
    }

  int next_option;

  const char *short_options ="-hn:";
    //  {"sender",        no_argument,       &f_sender,       1},
    //   {"receiver",      no_argument,       &f_receiver,     1},
    //   {"source",        required_argument, &f_source,       1},
    //   {"destination",   required_argument, NULL,  0},
    //   //{"verbose",

  const struct option long_options[] = {
    //{ name,   has_arg,            flag,  val}, 
      { "help", no_argument,        NULL, 'h' },
      { "num",  required_argument,  NULL, 'n' },
      { NULL,   0,                  NULL,  0  }
      };

  do {
      next_option = getopt_long(argc, argv, short_options, long_options, NULL);

      switch(next_option) {
          case '?':
              fprintf(stderr, "\nERROR: WRONG USAGE!\n\n");
              //print_usage(argv);
              //exit(2);
              break;

          case 'h':
              print_usage(argv);
              break;

          case 'n':
              printf("num %s\n", optarg);
              break;

          default:
              print_usage(argv);
              exit(EXIT_FAILURE);
      }   
  } while(next_option != -1);/* end of list */

  exit(EXIT_SUCCESS);
}

Runing the Programm:

./parameter_test2 -n 21                                                                     
num 21
+-----------------------------------------------------+
Usage: 
        ./parameter_test2 [arg] [arg] [arg] ...

        Programname --sender --Path /home/pi/Datei.txt
        Transceiver –s home/pi1/datei.txt
        Transceiver –r home/pi2/

                Transceiver [-s] SOURCE-PATH
                Transceiver [-r] [DESTINATION-PATH]
+-----------------------------------------------------+

Thank you for reading!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嘿哥们儿 2025-02-14 14:10:13

我的错误是,我应该在环路头上检查next_option

我用:
while(1){if(next_option == -1)break; / *列表结束 */...代码...}
并删除我的做...循环。

这是我现在完全工作的代码:

#include <stdlib.h> /* exit   */
#include <stdio.h>  /* printf */

void print_usage(char **argv)
{
  printf("+-----------------------------------------------------+\n");
  printf("Usage: \n\t%s [arg] [arg] [arg] ...\n",argv[0]);
  printf("\n");
  printf("\tProgramname --sender --Path /home/pi/Datei.txt\n");
  printf("\tTransceiver –s home/pi1/datei.txt\n");
  printf("\tTransceiver –r home/pi2/\n");
  printf("\n");
  printf("\t\tTransceiver [-s] SOURCE-PATH\n");
  printf("\t\tTransceiver [-r] [DESTINATION-PATH]\n");
  printf("+-----------------------------------------------------+\n");
  exit(EXIT_FAILURE);
}

int main(int argc, char **argv)
{
  
  // if no parameters are given
  if (argc == 1) {
    fprintf(stderr, "\nYou need to use options!\n\n");
    print_usage(argv);
    }
  
  while(1)
  {
    const char *short_options ="-hn:";
    const struct option long_options[] = {
      //{ name,   has_arg,            flag,  val}, 
        { "help", no_argument,        NULL, 'h' },
        { "num",  required_argument,  NULL, 'n' },
        { NULL,   0,                  NULL,  0  }
        };

        int next_option = getopt_long(argc, argv, short_options, long_options, NULL);
        if(next_option == -1) {break;} /* end of list */

        switch(next_option) {
            case '?':
                fprintf(stderr, "\nERROR: WRONG USAGE!\n\n");
                break;

            case 'h':
                print_usage(argv);
                break;

            case 'n':
                printf("num %s\n", optarg);
                break;

            default:
                print_usage(argv);
                exit(EXIT_FAILURE);
        }
  }   
  exit(EXIT_SUCCESS);
}

谢谢大家都指出了我的错误。

My mistake was I should have checked next_option at the head of the loop.

I fixed it with:
while(1){if(next_option == -1) break; /* end of list */...Code...}
and by removing my do ... while loop.

Here is my now fully working Code:

#include <stdlib.h> /* exit   */
#include <stdio.h>  /* printf */

void print_usage(char **argv)
{
  printf("+-----------------------------------------------------+\n");
  printf("Usage: \n\t%s [arg] [arg] [arg] ...\n",argv[0]);
  printf("\n");
  printf("\tProgramname --sender --Path /home/pi/Datei.txt\n");
  printf("\tTransceiver –s home/pi1/datei.txt\n");
  printf("\tTransceiver –r home/pi2/\n");
  printf("\n");
  printf("\t\tTransceiver [-s] SOURCE-PATH\n");
  printf("\t\tTransceiver [-r] [DESTINATION-PATH]\n");
  printf("+-----------------------------------------------------+\n");
  exit(EXIT_FAILURE);
}

int main(int argc, char **argv)
{
  
  // if no parameters are given
  if (argc == 1) {
    fprintf(stderr, "\nYou need to use options!\n\n");
    print_usage(argv);
    }
  
  while(1)
  {
    const char *short_options ="-hn:";
    const struct option long_options[] = {
      //{ name,   has_arg,            flag,  val}, 
        { "help", no_argument,        NULL, 'h' },
        { "num",  required_argument,  NULL, 'n' },
        { NULL,   0,                  NULL,  0  }
        };

        int next_option = getopt_long(argc, argv, short_options, long_options, NULL);
        if(next_option == -1) {break;} /* end of list */

        switch(next_option) {
            case '?':
                fprintf(stderr, "\nERROR: WRONG USAGE!\n\n");
                break;

            case 'h':
                print_usage(argv);
                break;

            case 'n':
                printf("num %s\n", optarg);
                break;

            default:
                print_usage(argv);
                exit(EXIT_FAILURE);
        }
  }   
  exit(EXIT_SUCCESS);
}

Thank you all for pointing my to my mistake.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文