- #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 ; // 外部变量 存储 参数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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
2013-09-03 C/C++源代码的Include依赖关系图
2013-09-03 新手必看:如何快速看懂VC++项目
2012-09-03 50个c/c++源代码网站
2012-09-03 C++资源之不完全导引