访问与命令行参数关联的整数列表 [英] accessing a list of integer associated with a command line parameter
问题描述
我在c ++中使用gflags解析命令行参数.我想有一个接受参数列表的命令行标志.例如下面的示例中的param
.
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?
推荐答案
gflags不支持数组输出,它只是跳过未知数据,因此您可以选择:
选择1,在gFlags之前手动解析args,但将参数添加到gflags-无错误解析,例如:
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;
}
选择2:
修改输入命令行,例如:---param 0,1,2,3
并以gFlags中的字符串形式接收参数,将字符串除以',',然后转换为整数数组.
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.
这篇关于访问与命令行参数关联的整数列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!