1

我正在尝试在 optarg 上使用 atoi,但它也可能是任何东西。我一直在试图弄清楚为什么我的 getopt_long 不起作用。当我输入我的 switch 语句 optarg 设置为 null 并保持这种状态。我检查了我的冒号,它们是正确的。这是我的代码。

static struct option long_options[] = 
    {
        {"algorithm", required_argument, 0, 'a'},
        {"reverse", no_argument, 0, 'r'},
        {"key", required_argument, 0, 'k'},
        {"output", required_argument, 0, 'o'},
        {"help", no_argument, 0, 'h'},
        {"version", no_argument, 0, 'V'},
        {0, 0, 0, 0}
    };

int option_index = 0;
int c;
//Getopt to get the correct options from the command line.
while ((c = getopt_long(argc, argv, "a:rk:o:hV", long_options, 
        &option_index)) != -1)
{
    bool endOption = false;

    if (endOption) break;

    switch (c) 
    {
        case 0:
        {
            endOption = true;
            break;
        }
        case 'a':
        {
            if (optarg == "insertion") algorithm = 0;
            break;
        }

        case 'r':
        {
            reverseFlag = true;
            break;
        }
        case 'k':
        {
            while (optarg != " ")
            {
                if (optarg == ",")
                {
                    optarg++;
                }
                else 
                {
                    sortOrder.push_back(atoi(optarg)); //error here
                    optarg++;
                }
            }
        }
        case 'o':
        {
            fileFlag = true;
            break;
        }
        case 'h':
        case 'V':
        default:
        {
            cerr<<"You have entered an incorrect flag, do it better"<<endl;
            break;
        }
    }
}

//更多东西//

我试过使用双冒号和其他所有东西。

4

1 回答 1

2

你不能像这样比较字符串:

 if (optarg == "insertion") algorithm = 0;

您需要使用strcmp()或等效,如下所示:

if (strcmp(optarg, "insertion") == 0) algorithm = 0;

可能还有其他问题,但这会立即浮现在脑海中。


修修补补optarg可能optarg++不是一个好主意。那是功能给您的信息;改变它是狡猾的。您可以获取指针的副本,然后将其递增,逐步遍历它指向的字符串。当然不能保证在参数中会有空格 for -k,这可能是您的麻烦的根源,以及滥用字符指针比较而不是 usingstrcmp()或等效项。

你似乎缺少一个breakafter case 'k':

bool endOption循环体的内部很奇怪:它在false每次迭代时都设置为;它在静止时被测试false,所以break不被执行;返回true时设置为;但该值在下一次迭代之前被重置。您可能应该在主循环之外声明此变量。然后它会表现得明智(尽管如果在再次调用之前设置了循环,您可能应该终止循环 - 所以它的条件可能应该在主循环的底部。getopt_long()0while ((c = getopt_long(...)) != -1)getopt_long()while


-k将处理中的循环测试更改为while (strcmp(optarg, "") != 0)(关键区别是两个双引号之间缺少空格),然后我会从中得到理智的行为。请注意我是如何检测这些选项的,以便我可以看到正在执行的内容。当我与包含空格的字符串进行比较时,我在循环终止之前得到了我的环境的精美转储。这就是导致核心转储的事情。

#include <getopt.h>
#include <vector>
#include <iostream>
#include <cstdlib>
using namespace std;

static struct option long_options[] = 
{
    {"algorithm", required_argument, 0, 'a'},
    {"reverse", no_argument, 0, 'r'},
    {"key", required_argument, 0, 'k'},
    {"output", required_argument, 0, 'o'},
    {"help", no_argument, 0, 'h'},
    {"version", no_argument, 0, 'V'},
    {0, 0, 0, 0}
};

int main(int argc, char **argv)
{
    vector<int> sortOrder;
    int option_index = 0;
    int c;
    bool reverseFlag = false;
    int algorithm = 1;
    bool fileFlag = false;

    //Getopt to get the correct options from the command line.
    while ((c = getopt_long(argc, argv, "a:rk:o:hV", long_options, &option_index)) != -1)
    {
        bool endOption = false;

        if (endOption) break;

        switch (c) 
        {
            case 0:
                    cerr << "Found 0 value\n";
                    endOption = true;
                    break;
            case 'a':
                    cerr << "Found -a option: " << optarg << endl;
                    if (strcmp(optarg, "insertion") == 0)
                        algorithm = 0;
                    break;
            case 'r':
                    cerr << "Found -r option\n";
                    reverseFlag = true;
                    break;
            case 'k':
                    cerr << "Found -k option: " << optarg << endl;
                    while (strcmp(optarg, "") != 0)
                    {
                        if (strcmp(optarg, ",") == 0)
                            optarg++;
                        else 
                        {
                            cerr << "pushback <<" << optarg << ">>\n";
                            sortOrder.push_back(atoi(optarg)); //error here
                            optarg++;
                        }
                    }
                    break;
            case 'o':
                    cerr << "Found -o option: " << optarg << endl;
                    fileFlag = true;
                    break;
            case 'h':
            case 'V':
            default:
                    cerr << "You have entered an incorrect flag, do it better" << endl;
                    break;
        }
    }
}

当它运行为:

./getopt -k 2,1

我得到了输出:

Found -k option: 2,1
pushback <<2,1>>
pushback <<,1>>
pushback <<1>>

您可能希望将比较从以下位置更改:

if (strcmp(optarg, ",") == 0)

至:

if (*optarg == ',')

您甚至可以合理地将循环条件更改为:

while (*optarg != '\0')

因此完全避免strcmp()。顺便说一句,我不确定哪个标头拉入了<cstring>,但其中一个标头确实拉入了。


作为记录,我正在运行 MacOS X 10.7.2 (Lion) 并使用提供的 G++ 编译器的 Mac Mini 上进行测试,i686-apple-darwin11-llvm-g++-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00).

于 2012-02-13T05:51:28.023 回答