在程序选项中添加键值对
add key value pairs in program options
我需要在我的程序中加载一个地图,其中包含一个整数键和一个字符串值,如下所示:
1, oil
2, car
5, house
我想使用 boost::program_options
加载它们。我可以在 tutorial 中看到我可以使用如下语法从选项中加载向量:
int opt;
po::options_description desc("Allowed options");
desc.add_options()
("help", "produce help message")
("optimization", po::value<int>(&opt)->default_value(10), "optimization level")
("include-path,I", po::value< vector<string> >(), "include path")
("input-file", po::value< vector<string> >(), "input file");
然后我可以使用 program --input-file file1, --input-file file2
来创建一个包含 file1
和 file2
的向量。从程序选项实现地图的最佳方式是什么?
我可以使用例如一个字符串,然后拆分字符串以获得值,例如 program --pair "1, oil" --pair "2, car" --pair "5, house"
,或者我可以使用 program --key 1 --value oil --key 2 --value car --key 5 --value car
.
将它们分开
我想使用可以从命令行和配置文件轻松编写的东西。哪种方法最好?
I can use for example a string and then splitting string in order to obtain values, like program --pair "1, oil" --pair "2, car" --pair "5, house"
...
我会坚持使用第一个选项并读取 std::vector<std::string>
字符串向量。之后你通过本机解析的向量,拆分字符串并填充你的地图。
... or I can separate them using program --key 1 --value oil --key 2 --value car --key 5 --value car
我不确定这种方式是否会保留您需要的强顺序。
我需要在我的程序中加载一个地图,其中包含一个整数键和一个字符串值,如下所示:
1, oil
2, car
5, house
我想使用 boost::program_options
加载它们。我可以在 tutorial 中看到我可以使用如下语法从选项中加载向量:
int opt;
po::options_description desc("Allowed options");
desc.add_options()
("help", "produce help message")
("optimization", po::value<int>(&opt)->default_value(10), "optimization level")
("include-path,I", po::value< vector<string> >(), "include path")
("input-file", po::value< vector<string> >(), "input file");
然后我可以使用 program --input-file file1, --input-file file2
来创建一个包含 file1
和 file2
的向量。从程序选项实现地图的最佳方式是什么?
我可以使用例如一个字符串,然后拆分字符串以获得值,例如 program --pair "1, oil" --pair "2, car" --pair "5, house"
,或者我可以使用 program --key 1 --value oil --key 2 --value car --key 5 --value car
.
我想使用可以从命令行和配置文件轻松编写的东西。哪种方法最好?
I can use for example a string and then splitting string in order to obtain values, like
program --pair "1, oil" --pair "2, car" --pair "5, house"
...
我会坚持使用第一个选项并读取 std::vector<std::string>
字符串向量。之后你通过本机解析的向量,拆分字符串并填充你的地图。
... or I can separate them using program --key 1 --value oil --key 2 --value car --key 5 --value car
我不确定这种方式是否会保留您需要的强顺序。