C++ boost::program_options的使用

c++ 解析参数选项的方法,使用boost::program_options库
/*
compile command at linux:
g++ test9_program_options.cpp  -o t9 -lboost_program_options
*/

#include <boost/program_options.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/regex.hpp>
#include <boost/foreach.hpp>
#include "myHfile.h"
#include <vector>

using namespace std;

int main(int argc, char** argv )
{
    string configFilename;
    std::vector<int>    values;
    bool flag;
    int pos1;
    std::vector<int> pos2;
    std::vector<std::string> inputs;
    namespace po = boost::program_options;
    po::options_description desc( "test program options" "\n"
            "usage: ./t9 -c hello -v 1 2 4 6 7 8 9 \n"
            "options");

    desc.add_options()
        ("help", "produce help messages/haha")
        ("config,c", po::value<std::string>( &configFilename), "config file")
        ("value,v", po::value( &values), "int values, -v 1 -v 2 -v 3")
        ("flag,f", po::bool_switch( &flag ), "swith option")
        ("pos1,p", po::value( &pos1 ), "positional option 1")
        ("input,i", po::value( &inputs)->multitoken(), "multitoken inputs")
        ;

    // this option will be hidden at "help" show    
    po::options_description hiddenDesc( "hidden options");
    hiddenDesc.add_options()
        ("pos2,q", po::value( &pos2 ), "positional option 2")
        ;

    // the second arg of add, species that up to 'max_count' next positional options should be given the 'name'. The value of '-1' means 'unlimited'.
    po::positional_options_description pos;
    pos.add( "pos1", 1 ).add( "pos2", -1 );


    po::variables_map vm;
    try{
        po::store( po::command_line_parser( argc, argv ).options(
            po::options_description().add( desc ).add( hiddenDesc ) ).positional( pos ).run(), vm );
    }
    catch(...){
        std::cout << "unknown options!" << std::endl;
    }

    po::notify( vm );

    if( vm.count("help") ){
        std::cout << desc << std::endl;
    }
	if( vm.count( "cofing") ) { //not work
        std::cout << "config file is:" << vm["config"].as<std::string>() << std::endl;
        std::cout << "config file is:" << configFilename << std::endl;
    }
    std::cout << "config file is:" << vm["config"].as<std::string>() << std::endl;
    std::cout << "config file is:" << configFilename << std::endl;
    if( vm.empty() ) {
        std::cout << "no options found" << std::endl;
    }
    if( !values.empty() ) {
        std::cout << "values: ";
        BOOST_FOREACH( int var, values ) {
            std::cout << var << ",";
        }
        std::cout << std::endl;
    }

    std::cout << "positional options, pos1: " << pos1 << std::endl;

    if( !pos2.empty() ) {
        std::cout << "positional options, pos2: ";
        BOOST_FOREACH( int var, pos2 ) {
            std::cout << var << ",";
        }
        std::cout << std::endl;
    }

    std::cout << "orig args:" << std::endl;
    for( int idx=0; idx<argc; idx++)
        cout << "agrv[" << idx << "] = " << argv[idx] <<endl;

    return 0;
}
  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值