• <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++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

            Boost program_options

            Posted on 2011-01-13 11:17 逐漸 閱讀(1876) 評論(0)  編輯 收藏 引用
            boost program_options庫可以幫助我們解析程序參數,支持命令行形式和配置文件形式,獲得(name, value)對.下面我們以一個模擬編譯器例子介紹program_options庫的應用,在下一節繼續介紹program_options整個庫.

             1 #include <boost/program_options.hpp>
             2 
             3 #include <vector>
             4 #include <iostream>
             5 #include <string>
             6 #include <algorithm>
             7 #include <iterator>
             8 using std::copy;
             9 using std::vector;
            10 using std::string;
            11 using std::cout;
            12 using std::endl;
            13 using std::exception;
            14 using std::ostream;
            15 using std::ostream_iterator;
            16 
            17 namespace po=boost::program_options;
            18 
            19 // output vector.
            20 template <typename T>
            21 ostream& operator<<(ostream& os, const vector<T>& v)
            22 {
            23     copy(v.begin(), v.end(), ostream_iterator<T>(os, " "));
            24     return os;
            25 }
            26 
            27 int main(int argc, char*argv[])
            28 {
            29     try
            30     {
            31         po::options_description desc("general descriptions.");
            32         desc.add_options()
            33             ("help""generate help information")
            34             ("input-file", po::value<vector<string> >(), "input files")
            35             ("link-file,l", po::value<vector<string> >(), "link file");
            36 
            37         po::variables_map vm;
            38         po::store(po::parse_command_line(argc, argv, desc), vm);
            39         po::notify(vm);
            40 
            41         if(vm.count("help"))
            42         {
            43             cout<<desc<<endl;
            44             return 1;
            45         }
            46 
            47         if(vm.count("input-file"))
            48         {
            49             cout<<"Input files: "<<vm["input-file"].as<vector<string> >()
            50                 <<"\n";
            51         }
            52 
            53         if(vm.count("link-file"))
            54         {
            55             cout<<"Link file: "<<vm["link-file"].as<vector<string> >()
            56                 <<"\n";
            57         }
            58     }
            59     catch(exception& e)
            60     {
            61         cout<<e.what()<<endl;
            62         return -1;
            63     }
            64 
            65     return 0;
            66 }
            67 

            程序第20行重載了<<運算符,用于輸出vector數組.
            第31行定義一個選項描述組件,然后添加允許的選項,add_options()方法返回一個特定對象,該對象重載了()運算.link-file選項指定了短名l,這樣--link-file與-l一個意思.
            第37行定義一個存儲器組件對象vm.
            第38行分析器parse_command_line將選項描述存儲至vm,這里用到的分析器很簡單,后面會介紹更復雜的應用.

            接下來的代碼就是比對vm中存放的選項了,簡單吧,很好理解.下面是運行截圖,編譯需要添加boost program_options庫,即-lboost_program_option

            對于input-file選項,每次都要輸出--input-file真的很麻煩,能不能用compiler main.cpp呢,當然可以.這種選項叫做positional option, 在第36行處加上如下代碼:
            1         po::positional_options_description p;
            2         p.add("input-file"-1);
            3 

            修改第38行,我們要用到功能更強大的command_line_parse,改成如下:
            1  po::store(po::command_line_parser(argc, argv).options(desc).positional(p).run(), vm);
            編譯運行:看下結果吧

            先到這里吧,接下來再看從文件中讀選項:)


            热久久最新网站获取| 国产成人精品免费久久久久| 久久久无码精品午夜| 狠狠色伊人久久精品综合网| 伊人久久大香线蕉综合网站| 久久精品亚洲中文字幕无码麻豆| 国产高潮国产高潮久久久91 | 99久久久精品免费观看国产| 久久精品嫩草影院| 超级97碰碰碰碰久久久久最新| 精品免费久久久久久久| 久久99精品国产麻豆蜜芽| 中文精品久久久久人妻不卡| 日韩精品久久久久久| 性高湖久久久久久久久| 九九热久久免费视频| 久久国产精品成人片免费| 色综合久久久久综合99| 69久久精品无码一区二区| 性做久久久久久免费观看| 久久91亚洲人成电影网站| 亚洲中文字幕无码久久综合网| 国产成人精品久久亚洲| 久久久精品2019免费观看| 久久综合偷偷噜噜噜色| 国产精品gz久久久| 青青青青久久精品国产h| 丰满少妇高潮惨叫久久久| 97精品依人久久久大香线蕉97| 久久青青草原精品国产不卡| 91久久精品电影| 99国产精品久久| 久久99国产亚洲高清观看首页 | 欧美精品福利视频一区二区三区久久久精品| 99精品国产免费久久久久久下载| 国产精品VIDEOSSEX久久发布| 97久久精品无码一区二区天美| 久久精品人人做人人爽电影蜜月| 无码专区久久综合久中文字幕| 伊人久久精品影院| 国内精品久久久久久久久电影网|