gpt4 book ai didi

c++ - 在 C++ 中使用 getopt 时打印默认参数

转载 作者:行者123 更新时间:2023-11-30 05:10:59 28 4
gpt4 key购买 nike

static struct option long_options[] =
{
{"r", required_argument, 0, 'r'},
{"help", no_argument, 0, 'h'},
{0, 0, 0, 0}
};


int option_index = 0;
char c;
while((c = getopt_long(argc, argv, "r:h", long_options, &option_index)) != -1)
{
switch(c)
{
case 'r':
break;
case 'h':
return EXIT_SUCCESS;
}
}

我如何使 h 成为默认参数,这样如果这个程序在没有任何参数的情况下运行,它就像是使用 -h 运行的一样?

最佳答案

也许尝试这样的事情:

static struct option long_options[] =
{
{"r", required_argument, 0, 'r'},
{"help", no_argument, 0, 'h'},
{0, 0, 0, 0}
};

int option_index = 0;
char c = getopt_long(argc, argv, "r:h", long_options, &option_index);
if (c == -1)
{
// display help...
return EXIT_SUCCESS;
}

do
{
switch(c)
{
case 'r':
break;

case 'h':
{
// display help...
return EXIT_SUCCESS;
}
}

c = getopt_long(argc, argv, "r:h", long_options, &option_index);
}
while (c != -1);

或者这个:

static struct option long_options[] =
{
{"r", required_argument, 0, 'r'},
{"help", no_argument, 0, 'h'},
{0, 0, 0, 0}
};

int option_index = 0;
char c = getopt_long(argc, argv, "r:h", long_options, &option_index);
if (c == -1)
c = 'h';

do
{
switch(c)
{
case 'r':
break;

case 'h':
{
// display help...
return EXIT_SUCCESS;
}
}

c = getopt_long(argc, argv, "r:h", long_options, &option_index);
}
while (c != -1);

关于c++ - 在 C++ 中使用 getopt 时打印默认参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45359873/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com