c++ - 从命令行获取参数的问题

标签 c++ unistd.h

<分区>

strong text我正在尝试从此命令获取参数:

./cube -d ../../data/input_small_id -q ../../data/query_small_id -k 5 -M 15 -p 6 -o out

我在获取 k、M 和 p 时遇到了问题。具体来说,我希望 k、M 和 p 是 optional_requirements,我的程序只获得我给它们的默认数字。 所有的数字都是整数(k,M,探针)。我已经这样做了:

void CubeUtils::getArgs(int argc,char **argv) {

    int c ;
    while (1) {
        static struct option long_options[] =
        {
            //These options don’t set a flag.
            //We distinguish them by their indices. 
            {"d"         , required_argument , 0 , 'd'},
            {"q"        , required_argument , 0 , 'q'},
            {"k"    , optional_argument , 0 , 'k'},
            {"M"           , optional_argument , 0 , 'M'},
            {"probes"           , optional_argument , 0 , 'p'},
            {"o"          , required_argument , 0 , 'o'},
            {0, 0, 0, 0}
        };

        // getopt_long stores the option index here. 
        int option_index = 0;

        c = getopt_long (argc, argv, "d:q:k::M::p::o:a:h",
        long_options, &option_index);

        // Detect the end of the options. 
        if (c == -1)
            break;
        switch (c) {
            case 'd':
                params.input_file.insert(0,optarg);
                break;
            case 'q':
                params.query_file.insert(0,optarg);
                break;
            case 'k':
                if (optarg) {
                    params.K = atoi(optarg);
                }
                else
                    params.K = 3 ;
                break;
            case 'M':
                if (optarg) {
                    params.M = atoi(optarg);
                }
                else
                    params.M = 10 ;
                break;
            case 'p':
                if (optarg) {
                    params.probes = atoi(optarg);
                }
                else
                    params.probes = 2 ;
                break;
            case 'o':
                params.output_file.insert(0,optarg);
                break;
            case '?':
            // getopt_long already printed an error message. 
                break;
            default:
                abort ();
        }
    }
}

Params 是包含参数的结构:

struct ParametersCube
{
    string input_file;
    string query_file;
    int K;
    int M;
    int probes ;
    string output_file;
    float radius;

};

当它执行时,我只采用默认数字,而不是我通过命令行提供的数字。

最佳答案

这似乎是带有可选参数的预期行为。

引用 the optarg manpage :

optstring is a string containing the legitimate option characters. If such a character is followed by a colon, the option requires an argument, so getopt() places a pointer to the following text in the same argv-element, or the text of the following argv-element, in optarg. Two colons mean an option takes an optional arg; if there is text in the current argv-element (i.e., in the same word as the option name itself, for example, "-oarg"), then it is returned in optarg, otherwise optarg is set to zero.

…和the getopt manpage :

A simple short option is a '-' followed by a short option character. If the option has a required argument, it may be written directly after the option character or as the next parameter (ie. separated by whitespace on the command line). If the option has an optional argument, it must be written directly after the option character if present.

我个人觉得这很奇怪,但我可以确认,如果您将这些参数更改为:

-k5 -M15 -p6

then it works .

另见:

关于c++ - 从命令行获取参数的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58593810/

相关文章:

c - 从 stderr 而不是 stdin 读取

C : Create a function to diplay number just with "write" function

Linux 中的 C 程序读取作为终端参数传递的文件描述符

c++ - string.find() 不返回 -1

c++ - 模板参数的潜在范围是什么?

c - 为什么我的 fork 进程的父进程 pid 与父进程自己的 pid 不同?

c - 从c中的文件获取输入时出错

c++ - 在检查 array[i] == i 时返回 bool 的递归算法(必须是 O(log n))

c++ - 无法在 Visual Studio 2012 中打开头文件

c++ - 交换(int &a, int &b) 和交换(int *a, int *b)。有什么区别?