accessing a list of integer associated with a command line parameter

谁都会走 提交于 2019-12-11 06:02:46

问题


I'm using gflags in c++ for parsing of command line parameters. I would like to have a command line flag that accepts a list of parameters. For example param in the example below.

./myprog --param 0 1 2 3

How can I access a list of integers associated with this parameter?


回答1:


gflags not supported array output, its just skipping unknown data, so you can choose:
Choice 1, parse args manually before gFlags,but add param to gflags - for no error parsing, for example:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <vector>
    #include <gflags/gflags.h>

   DEFINE_string(param, "string", "string"); 

   int main (int argc, char **argv) 
   {
        vector<int> param;
        for (int i = 0; i < argc; i++) 
        {
            if (!strcmp(argv[i], "--param")) 
            {
                for (++i; i < argc; i++) 
                {
                    if (!isdigit(argv[i][0]))
                        break;
                    param.push_back(atoi(argv[i]));
                }
            }
        }
        gflags::ParseCommandLineFlags(&argc, &argv, false); 
        return 0;
    }     


Choice 2:
Modify your input command line for example to : --param 0,1,2,3
and receive param as string in gFlags, split string by ',' and convert to array of integer.



来源:https://stackoverflow.com/questions/27125887/accessing-a-list-of-integer-associated-with-a-command-line-parameter

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!