accessing a list of integer associated with a command line parameter

2.2k views Asked by At

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

There are 1 answers

1
Maxim Akristiniy On BEST ANSWER

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.