boost::program_options 解析命令行参数

提供强大的命令行参数处理功能,不仅能够分析命令行,也能够从配置文件和环境变量中获取参数,实现了分出完善的程序配置选项处理功能。

program_options 库的解析程序选项功能由三个基本组件构成:

  • 选项描述器:定义选项和选项的值
  • 分析器:依据选项描述器的定义解析命令行参数或者数据文件
  • 存储器:把分析器的结果保存起来共程序使用

boost::options 库的使用实例:

#include <boost/program_options.hpp>
using namespace boost;
//using namespace boost::program_options;

#include <string>
using std::string;

int main(int argc,char **argv)
{
    program_options::options_description opts("demo options");  //核心类
    opts.add_options()  //增加程序选项
        ("help", "just a help info")
        ("filename", program_options::value<std::string>(), "to find a file");
    program_options::variables_map vm;  //选项存储map容器
    program_options::store(program_options::parse_command_line(argc, argv, opts), vm);  //解析存储

    if (vm.count("help")){
        cout << opts << endl;
        //return 0;
    }

    if (vm.count("filename")){
        cout << "find" << vm["filename"].as<string>() << endl;
    }

    if (vm.size() == 0){
        cout << "no options" << endl;
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值