• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            zhujian

            揣著夢想騎驢找馬但不虐待驢
            posts - 10, comments - 7, trackbacks - 0, articles - 0
              C++博客 :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

            Boost program_options(續(xù))

            Posted on 2011-01-13 13:17 逐漸 閱讀(548) 評論(0)  編輯 收藏 引用
            下面介紹如何從配置文件中讀參數(shù),配置文件中采用name = value的形式,#行表示注釋.
             1 #include <boost/program_options.hpp>
             2 
             3 #include <vector>
             4 #include <iostream>
             5 #include <string>
             6 #include <algorithm>
             7 #include <iterator>
             8 #include <fstream>
             9 using std::copy;
            10 using std::vector;
            11 using std::string;
            12 using std::cout;
            13 using std::cerr;
            14 using std::endl;
            15 using std::exception;
            16 using std::ostream;
            17 using std::ifstream;
            18 using std::ostream_iterator;
            19 
            20 namespace po=boost::program_options;
            21 
            22 // output vector.
            23 template <typename T>
            24 ostream& operator<<(ostream& os, const vector<T>& v)
            25 {
            26     copy(v.begin(), v.end(), ostream_iterator<T>(os, " "));
            27     return os;
            28 }
            29 
            30 int main(int argc, char*argv[])
            31 {
            32     try
            33     {
            34         string conf_file;
            35         po::options_description desc("general descriptions.");
            36         desc.add_options()
            37             ("help""generate help information")
            38             ("config,c", po::value<string>(&conf_file)->default_value("compiler.conf"), "compiler configure file")
            39             ("input-file", po::value<vector<string> >(), "input files")
            40             ("link-file,l", po::value<vector<string> >()->composing(), "link file");
            41 
            42         po::positional_options_description p;
            43         p.add("input-file"-1);
            44 
            45         po::variables_map vm;
            46         //po::store(po::parse_command_line(argc, argv, desc), vm);
            47         po::store(po::command_line_parser(argc, argv).options(desc).positional(p).run(), vm);
            48         po::notify(vm);
            49 
            50       
            51         if(vm.count("help"))
            52         {
            53             cout<<desc<<endl;
            54             return 1;
            55         }
            56 
            57         // add following lines
            58         ifstream i_conf(conf_file.c_str());
            59         if(!i_conf)
            60         {
            61             cerr<<"Configure file not exit.\n";
            62             return -1;
            63         }
            64         else
            65         {
            66             po::store(po::parse_config_file(i_conf, desc), vm);
            67             notify(vm);
            68         }
            69         
            70         if(vm.count("input-file"))
            71         {
            72             cout<<"Input files: "<<vm["input-file"].as<vector<string> >()
            73                 <<"\n";
            74         }
            75 
            76         if(vm.count("link-file"))
            77         {
            78             cout<<"Link file: "<<vm["link-file"].as<vector<string> >()
            79                 <<"\n";
            80         }
            81     }
            82     catch(exception& e)
            83     {
            84         cout<<e.what()<<endl;
            85         return -1;
            86     }
            87 
            88     return 0;
            89 }
            90 

            第38行添加了config參數(shù)命令,接受一個string類型值,并將默認(rèn)值設(shè)為compiler.conf.
            第40行添加了composing()方法,這表示程序?qū)牟煌臄?shù)據(jù)源中獲得數(shù)據(jù)并組合起來.
            第66行解析配置文件并存儲至vm.
            接下來代碼便是比對vm中選項值,簡單吧:)

            boost文檔里介紹了隱藏選項和存放多姐選項的方法,http://www.boost.org/doc/libs/1_45_0/doc/html/program_options/tutorial.html#id2073299





            久久久久久国产精品无码下载| 97久久精品人人澡人人爽| 久久午夜综合久久| 综合久久精品色| 国产精品美女久久久m| 久久本道综合久久伊人| 久久久久久久波多野结衣高潮| 97久久精品无码一区二区| 少妇久久久久久被弄到高潮| 亚洲va久久久噜噜噜久久男同 | 国内精品久久久久久久影视麻豆 | 国产91色综合久久免费| 精品乱码久久久久久夜夜嗨| 中文字幕人妻色偷偷久久| 91精品日韩人妻无码久久不卡| 久久妇女高潮几次MBA| 国产精品99久久久久久董美香 | 91精品观看91久久久久久| 久久中文字幕人妻丝袜| 丁香五月综合久久激情| 久久亚洲AV成人出白浆无码国产| 久久国产精品免费| 99久久免费国产特黄| 亚洲精品国产美女久久久| 亚洲精品tv久久久久久久久久| 欧美伊香蕉久久综合类网站| 亚洲人成精品久久久久| 免费精品久久天干天干| 久久久无码精品午夜| 久久精品国产色蜜蜜麻豆| 久久精品国产一区| 久久久久久久综合日本亚洲| 久久99国产综合精品| 国产精品久久久久久吹潮| 午夜精品久久久久久中宇| 久久婷婷五月综合成人D啪| 欧美亚洲国产精品久久高清| 久久久久亚洲国产| 亚洲欧洲日产国码无码久久99| 久久婷婷五月综合色奶水99啪| 国产精品久久久久久久久久影院 |