我的getopt.h代码在不同的情况下跳入默认情况下
我想将参数添加到我编写的程序中,因此我制作了一些新项目来学习和了解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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的错误是,我应该在环路头上检查
next_option
。我用:
while(1){if(next_option == -1)break; / *列表结束 */...代码...}
并删除我的
做...
循环。这是我现在完全工作的代码:
谢谢大家都指出了我的错误。
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:
Thank you all for pointing my to my mistake.