- #include <boost/program_options.hpp>
- #include <iostream>
- #include <vector>
- using namespace std;
- using namespace boost::program_options;
- int main(int argc, char* argv[])
- {
- string one ;
- vector<string> mult;
- boost::program_options::options_description opts("test options");
- opts.add_options()
- ("help,h","help info")
- ("test1,t",value<string>(),"test aaa ")
- ("one,o",value<string>(&one)->default_value("one"),"test one default")
- ("mult,m",value<vector<string> >(&mult)->multitoken(),"mult test");
-
- variables_map vm;
- try
- {
- store(parse_command_line(argc,argv,opts),vm);
- }
- catch(boost::program_options::error_with_no_option_name &ex)
- {
- cout<<ex.what()<<endl;
- }
-
- notify(vm);
- if (vm.count("help"))
- {
- cout<<opts<<endl;
- return -1;
- }
- if(vm.count("test1"))
- {
- cout<<vm["test1"].as<string>()<<endl;
- }
-
- cout<<one<<endl;
- cout<<mult.size()<<endl;
-
- getchar();
-
- return 0;
- }
[root@localhost test4]# g++ main.cpp -l boost_program_options
[root@localhost test4]# ./a.out -h
test options:
-h [ --help ] help info
-t [ --test1 ] arg test aaa
-o [ --one ] arg (=one) test one default
-m [ --mult ] arg mult test
[root@localhost test4]# ./a.out -m f2 f3 f4 --test1 testbbbb
testbbbb
one
3
boost 分析命令行参数
原文:http://www.cnblogs.com/lidabo/p/3953791.html