c++ - optarg 设置为空

标签 c++ getopt

我正在尝试在 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;
        }
    }
}

//更多内容//

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

最佳答案

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

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

您需要使用 strcmp()或等效的,像这样:

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

可能还有其他问题,但我会立即想到。


修补 optargoptarg++可能不是一个好主意。那是函数给你的信息;改变它是狡猾的。您可以获取指针的拷贝,然后递增该指针,逐步遍历它指向的字符串。当然不能保证 -k 的参数中会有空格。 , 这很可能是你麻烦的根源,同时滥用字符指针比较而不是使用 strcmp()或等效的。

您似乎缺少一个 breakcase 'k': 之后.

bool endOption循环体内很奇怪:它被设置为 false在每次迭代中;在它仍然存在时进行测试 false , 所以 break不被执行;它设置为 true什么时候getopt_long()返回 0 ;但该值在下一次迭代之前被重置。您可能应该在主 while ((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()共。顺便说一句,我不确定哪个 header 拉入了 <cstring> , 但其中一个做到了。


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

关于c++ - optarg 设置为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9255822/

相关文章:

command-line-arguments - 如何在 Dart 中访问 argv/命令行选项?

c - 那么 GNU getopt 只支持中间的选项吗?

c++ - 事件对象 - 从函数对象类创建线程

c++ - std::unique_ptr 可以用作参数吗?

c++ - Ncurses:在列之间创建填充空间

我可以使用 getopt 按特定顺序处理选项吗?

c - 退出时出现段错误(EXIT_FAILURE)但没有错误(EXIT_FAILURE,...)

linux - BASH getopt inside bash 函数

c++ - 为什么一个简单的程序不能立即启动

c++ - 为什么初始化构造函数列表参数会出现异常?