【问题标题】:Problem with getting arguments from command line [duplicate]从命令行获取参数的问题[重复]
【发布时间】:2020-02-23 21:20:39
【问题描述】:

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;

};

当它执行时,我只取默认数字,而不是我通过命令行给出的数字。

【问题讨论】:

  • Runnable testcase(下次请自己做,省点时间)
  • 哦,发现了一个骗子。还是留下我的答案。

标签: c++ unistd.h


【解决方案1】:

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

引用the optarg manpage:

optstring 是一个包含合法选项字符的字符串。如果这样的字符后跟一个冒号,则该选项需要一个参数,因此 getopt() 将指向同一 argv 元素中的以下文本的指针,或 optarg 中以下 argv 元素的文本。 两个冒号表示一个选项带有一个可选参数;如果当前 argv 元素中有文本(即与选项名称本身在同一个单词中,例如“-oarg”),则在 optarg 中返回,否则将 optarg 设置为零。

…和the getopt manpage:

一个简单的短选项是一个'-'后跟一个短选项字符。如果选项有一个必需的参数,它可以直接写在选项字符之后或作为下一个参数(即,在命令行上用空格分隔)。 如果选项有可选参数,则必须直接写在选项字符之后。

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

-k5 -M15 -p6

then it works.

另见:

【讨论】:

  • Double :: 我认为有,所以您可以声明这是一个可选参数。我不确定,我从 gnu 获得了代码并对其进行了一些更改,以使其适合我的情况。我可以做些什么来改变它吗?
  • @ΣτεφανοςΠαμποριδης 单击“另请参阅”(这也是此 Q 的欺骗)以获得可能的解决方法。但除此之外,这就是它的工作原理
猜你喜欢
  • 2020-07-11
  • 2014-02-15
  • 2014-01-03
  • 2016-03-21
  • 2013-02-18
  • 1970-01-01
  • 2015-10-17
相关资源
最近更新 更多