学习boost program_options(2--简单示例)

在VS2012环境下的一个简单示例:

1、示例代码:

#include <string>
#include <iostream>
#include <boost\program_options.hpp>


using namespace std;


int main(int ac, char* av[])
{
boost::program_options::options_description options("command line options");
options.add_options() ("help,h","Use -h or --help to list all arguments") 
 ("file",boost::program_options::value<string>(), "Provide input file name");
boost::program_options::variables_map vmap;
boost::program_options::store(boost::program_options::parse_command_line(ac, av, options), vmap);
boost::program_options::notify(vmap);


if(vmap.count("help"))
{
cout<< options <<endl;
}
if(vmap.count("file"))
{
cout<< "Your input file: "<<vmap["file"].as<string>() <<"\n";
}
return 0;
}

2、编译运行后在工程目录下找到对应的.exe文件,然后打开电脑-->开始-->附件-->命令提示符,在命令行窗口中输入如下命令

(1)利用cd 命令切换到.exe文件目录

(2)输入.exe文件名 和对应的命令与参数,该简单示例中有两条命令参数,第一个为--help;第二个为--file arg, 示例如下

命令行输入:

对应输出结果:

        命令行输入:

对应输出结果:




你可能感兴趣的:(boost,program_options)