从命令行获取参数的问题

Problem with getting arguments from command line

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,probes)。我这样做了:

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.

另请参阅: