getopt/long_getopt 的正确使用方法

发布于 2024-12-05 03:05:10 字数 1576 浏览 0 评论 0原文

我知道这个话题已经被打死了,但我仍然找不到我要找的东西。 我需要解析 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 技术交流群。

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

发布评论

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

评论(2

相对绾红妆 2024-12-12 03:05:10

您将字符串 (char*) 值转换为整数值,这与解析它有很大不同。通过强制转换,您可以使用第一个字符的 ASCII 值作为数值,而通过解析字符串,您可以尝试将整个字符串解释为文本并将其转换为机器可读的值格式。

您需要使用解析函数,例如:

std::stringstream argument(optarg);
argument >> iterations;

or

boost::lexical_cast<int>(optarg);

or (C 风格)

atoi(optarg)

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:

std::stringstream argument(optarg);
argument >> iterations;

or

boost::lexical_cast<int>(optarg);

or (C-style)

atoi(optarg)
李不 2024-12-12 03:05:10

因为 optarg 是 char*。这是纯文本。因此,如果您给程序提供 .8 作为参数,则 optarg 是一个字符串“.8”,并且将其转换为 float 不起作用。例如,使用 atoi 和 atof 函数(在“stdlib.h”中声明)将字符串解析为 int 和 float。在你的代码中它将是:

iterations = atoi(optarg);
decay = atof(optarg);

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:

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