boost::program_options

boost::program_options 解析命令行,包括多参数命令行解析示例如下:

示例代码:

 1 #include "stdafx.h"
 2 
 3 #include <iostream>
 4 #include "boost/program_options.hpp"
 5 
 6 namespace bpo = boost::program_options;
 7 
 8 std::string showstring(std::string str){
 9     std::cout << str << std::endl;
10     return std::string(str + ";");
11 }
12 
13 int _tmain(int argc, _TCHAR* argv[])
14 {
15     bpo::options_description app_options("APP options");
16     app_options.add_options()
17         ("help,h", "Print this help message and exit.")
18         ("num", bpo::value<uint32_t>(), "int")
19         ("word", bpo::value<std::vector<std::string>>(),"string")
20         ;
21 
22     bpo::variables_map options;
23     bpo::store(bpo::parse_command_line(argc, argv, app_options), options);
24 
25     if (options.count("num"))
26     {
27         std::cout << "num " << options["num"].as<uint32_t>() << std::endl;
28     }
29     if (options.count("word"))
30     {
31         std::vector<std::string> words = options["word"].as<std::vector<std::string>>();
32         std::transform(words.begin(), words.end(), words.begin(), showstring);
33 
34         for (const auto& itr : words){
35             std::cout << "===> " << itr << std::endl;
36         }
37     }
38 
39     getchar();
40     return 0;
41 }

命令行参数输入:

输出结果:

posted @ 2018-08-07 12:27  漆天初晓  阅读(1040)  评论(0编辑  收藏  举报