织梦CMS - 轻松建站从此开始!

罗索

boost::program_options 使用

jackyhwei 发布于 2011-09-14 22:25 点击:次 
从命令行提取程序运行时选项的方法有很多。你可以自己编写相对应的完整的解析函数,或许你有丰富的C语言编程经验,熟知getopt()函数的用法,又或许使用Python的你已经在使用optparse库来简化这一工作。
TAG:

从命令行提取程序运行时选项的方法有很多。你可以自己编写相对应的完整的解析函数,或许你有丰富的C语言编程经验,熟知getopt()函数的用法,又或许使用Python的你已经在使用optparse库来简化这一工作。

program_options 提供程序员一种方便的命令行和配置文件进行程序选项设置的方法。使用program_options库而不是你自己动手写相关的解析代码,因为它更简单,声明程序选项的语法简洁,并且库自身也非常小。将选项值转换为适合的类型值的工作也都能自动完成。库有着完备的错误检查机制,如果自己手写解析代码时,就可能会错过对一些出错情况的检查了。最后,选项值不仅能从命令行获取,也能从配置文件,甚至于环境变量中提取,而这些选择不会增加明显的工作量。

1. 首先看一个简单的程序吧:

  1. #include <string> 
  2. #include <iostream> 
  3. #include <boost/program_options.hpp> 
  4. using namespace std; 
  5.  
  6. int main (int ac, char* av[]) 
  7.     boost::program_options::options_description options("command line options"); 
  8.     options.add_options() ("help""Use -h or --help to list all arguments"
  9.         ("file", boost::program_options::value<string>(), 
  10.          "Provide input file name"); 
  11.     boost::program_options::variables_map vmap; 
  12.     boost::program_options::store( 
  13. boost::program_options::parse_command_line(ac, av, options), vmap); 
  14.     boost::program_options::notify(vmap); 
  15.  
  16.     if (vmap.count("help")) { 
  17.         cout << options << endl; 
  18.     } 
  19.  
  20.     if (vmap.count("file")){ 
  21.         cout <<"Your input file: " << vmap["file"].as<string>() << "\n"
  22.     } 
  23.  
  24.     return 0; 
  25.  
  26. //compile: g++ boost_test.cpp -o boost_test -lboost_program_options 

程序的工作方式如下:
    options_description 类声明所有的有效命令行选项。
    使用方法 add_options,您可以注册命令和跟在命令后面的参数类型。在此例中,help 选项不需要任何参数,但是 file 选项需要一个字符串参数。
    variables_map 类在运行时存储命令行选项及其参数。
    Boost 的 parse_command_line 函数解析 argc 和 argv 参数。store 和 notify 方法帮助存储 vmap 对象中的数据。
    vmap.count()用于检测输入的是哪个命令行参数,并采取适当的动作

2. 提供多个参数和缩写的命令选项

命令行处理通常同时需要同一个命令选项的短名称和长名称。此外,您通常必须多次使用某个选项,以便收集该选项的所有参数。例如,您可能希望使用 -h 和 --help 来打印可用的命令:

  1. #include <string> 
  2. #include <iostream> 
  3. #include <boost/program_options.hpp> 
  4. using namespace std; 
  5.  
  6. int main (int ac, char* av[]) 
  7.   boost::program_options::options_description options("command line options"); 
  8.   options.add_options() ("help,h""Use -h or --help to list all arguments"
  9.                     ("file", boost::program_options::value<vector<string> >( ), 
  10.                          "Provide input file name"); 
  11.   boost::program_options::variables_map vmap; 
  12.   boost::program_options::store( 
  13.       boost::program_options::parse_command_line(ac, av, options), vmap); 
  14.   boost::program_options::notify(vmap); 
  15.  
  16.   if (vmap.count("help")) { 
  17.       cout << options << endl; 
  18.   } 
  19.  
  20.   if (vmap.count("file")) { 
  21.       vector<string> ifiles(vmap["file"].as< vector<string> > ()); 
  22.       vector<string>::iterator vI; 
  23.       cout << "Number of input files: " << ifiles.size() << endl; 
  24.       cout << "Input file list: " << endl; 
  25.       for(vI = ifiles.begin(); vI != ifiles.end(); ++vI) 
  26.           cout << "\t" << *vI << endl; 
  27.   } else { 
  28.       cout << "No file specified\n"
  29.   } 
  30.  
  31.   return 0; 

在使用 add_options 来添加命令选项时,较长和较短的选项之间使用逗号进行分隔。请注意,较长的选项 (help) 必须在较短的选项 (h) 之前,代码才能正常工作。与使用单个字符串不同,file 选项现在是使用一个字符串向量来定义的。如果指定了 --file 选项多次,则会将所有收集到的file命令选项参数存储在关联的vector<string>中。下面是使用不同的参数来多次指定 --h 和 --file 所获得的输出:

 ./a.out -h
command line options:
  -h [ --help ]         Use -h or --help to list all arguments
  --file arg            Provide input file name

No file specified

./a.out --file abc --file pqr
Number of input files: 2
Input file list:
        abc
        pqr

3. 解析位置选项

下面的程序,第一个参数转换为 --file=<first parameter>,第二个参数转换为 --do-file=<second parameter>。

  1. #include <string> 
  2. #include <iostream> 
  3. #include <boost/program_options.hpp> 
  4. using namespace std; 
  5.  
  6. int main (int ac, char* av[]) 
  7.   boost::program_options::options_description options("command line options"); 
  8.   options.add_options() ("help,h""Use -h or --help to list all arguments"
  9.                         ("file", boost::program_options::value<string>(), 
  10.                          "Provide input file name"
  11.                         ("do-file", boost::program_options::value<string>(), 
  12.                          "Specify commands file"); 
  13.  
  14.   boost::program_options::variables_map vmap; 
  15.   boost::program_options::positional_options_description poptd; 
  16.   poptd.add("file", 1); 
  17.   poptd.add("do-file", 2); 
  18.  
  19.   boost::program_options::store( 
  20.       boost::program_options::command_line_parser(ac, av). 
  21.       options(options).positional(poptd).run(), vmap); 
  22.   boost::program_options::notify(vmap); 
  23.  
  24.   if (vmap.count("file")) { 
  25.      cout << "file: " << vmap["file"].as<string> ( ) << endl; 
  26.   } 
  27.  
  28.   if (vmap.count("do-file")) { 
  29.      cout << "do-file: " << vmap["do-file"].as<string> ( ) << endl; 
  30.   } 
  31.  
  32.   return 0; 

下面是输出内容:

./a.out file1 dofile1
file: file1
do-file: dofile1

程序引入了新的类 positional_options_description。该类的 add 方法(add("command option", N))将位置 N 处的输入参数与命令行选项 "command option" 相关联。因此,./a.out file1 在内部解析为 ./a.out  --file=file1。另一个区别在于调用 program_options::store 方法的方式。与使用 parse_command_line 例程不同,Boost 库要求您将 command_line_parser 例程与 store 方法结合在一起使用。

请注意,仍然可以使用 --file 和 --do-file 选项来调用该程序。最后,若要将所有的输入参数与同一个命令行选项相关联,您需要使用值-1 将该命令行选项添加到 positional_options_description 对象。下面是代码:


boost::program_options::positional_options_description poptd;
poptd.add("file", -1);
...

(itlanger)
本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [http://www.rosoo.net/a/201109/14993.html]
本文出处:itlanger.blog.chinaunix.net 作者:itlanger
顶一下
(7)
100%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
将本文分享到微信
织梦二维码生成器
推荐内容