getopt/long_getopt 的正确使用方法
我知道这个话题已经被打死了,但我仍然找不到我要找的东西。 我需要解析 C++ 中的命令行参数。
我无法使用 Boost 并使用 long_getopt
问题在于转换,当我简单地打印参数时,它在循环中按预期工作,但分配给变量的值无法正常工作。
这是完整的、可编译的程序。
#include <iostream>
#include <getopt.h>
using namespace std;
int main(int argc, char *argv[])
{
int c;
int iterations = 0;
float decay = 0.0f;
int option_index = 0;
static struct option long_options[] =
{
{"decay", required_argument, 0, 'd'},
{"iteration_num", required_argument, 0, 'i'},
{0, 0, 0, 0}
};
while ((c = getopt_long (argc, argv, "d:i:",
long_options, &option_index) ) !=-1)
{
/* getopt_long stores the option index here. */
switch (c)
{
case 'i':
//I think issue is here, but how do I typecast properly?
// especially when my other argument will be a float
iterations = static_cast<int>(*optarg);
cout<<endl<<"option -i value "<< optarg;
break;
case 'd':
decay = static_cast<float>(*optarg);
cout<<endl<<"option -d with value "<<optarg;
break;
}
}//end while
cout << endl<<"Value from variables, which is different/not-expected";
cout << endl<< decay << endl << iterations << endl;
return(0);
}
正如我在评论中提到的 - 认为问题出在类型转换上,如何正确地做到这一点?如果还有其他更好的方法,请告诉我。
您可以将程序运行为 --- ./program-name -d .8 -i 100
谢谢您的帮助。我对 Unix 和 C++ 很陌生,但非常努力地学习它:)
I know this topic has been beaten to death but I still couldn't find what I am searching for.
I need to parse command line arguments in C++.
I cannot use Boost and using long_getopt
The issue is in casting, when I simply print the arguments , it works as expected in the loop but the value assigned to variables is not working somehow.
Here is the complete , compilable program.
#include <iostream>
#include <getopt.h>
using namespace std;
int main(int argc, char *argv[])
{
int c;
int iterations = 0;
float decay = 0.0f;
int option_index = 0;
static struct option long_options[] =
{
{"decay", required_argument, 0, 'd'},
{"iteration_num", required_argument, 0, 'i'},
{0, 0, 0, 0}
};
while ((c = getopt_long (argc, argv, "d:i:",
long_options, &option_index) ) !=-1)
{
/* getopt_long stores the option index here. */
switch (c)
{
case 'i':
//I think issue is here, but how do I typecast properly?
// especially when my other argument will be a float
iterations = static_cast<int>(*optarg);
cout<<endl<<"option -i value "<< optarg;
break;
case 'd':
decay = static_cast<float>(*optarg);
cout<<endl<<"option -d with value "<<optarg;
break;
}
}//end while
cout << endl<<"Value from variables, which is different/not-expected";
cout << endl<< decay << endl << iterations << endl;
return(0);
}
As I mentioned in the comment - think the problem is in typecasting, how to do it properly? If there is any other way which is better, please do let me know.
You can run the program as --- ./program-name -d .8 -i 100
Thank you for your help. I am new to Unix and C++ but trying very hard to learn it :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您将字符串 (char*) 值转换为整数值,这与解析它有很大不同。通过强制转换,您可以使用第一个字符的 ASCII 值作为数值,而通过解析字符串,您可以尝试将整个字符串解释为文本并将其转换为机器可读的值格式。
您需要使用解析函数,例如:
or
or (C 风格)
You are casting a string (char*) value to integer values, which is very different from parsing it. By casting, you use the ASCII value of the first character as a numerical value, while by parsing a string, you try to interpret the whole string as a text and convert it into a machine-readable value format.
You need to use a parsing function like:
or
or (C-style)
因为 optarg 是 char*。这是纯文本。因此,如果您给程序提供 .8 作为参数,则 optarg 是一个字符串“.8”,并且将其转换为 float 不起作用。例如,使用 atoi 和 atof 函数(在“stdlib.h”中声明)将字符串解析为 int 和 float。在你的代码中它将是:
Because optarg is char*. It's plain text. So if you give your program .8 as argument then optarg is a string ".8" and casting it to float doesn't work. Use for example atoi and atof funcions (declared in 'stdlib.h') to parse string as int and float. In your code it would be: