在C ++中解析命令行参数有什么有效的方法? [英] What's an effective way to parse command line parameters in C++?
问题描述
有没有一个真正有效的方式来处理C ++中的命令行参数?
我在下面做什么感觉完全业余,我不能想象这是在专业软件中真正处理命令行参数(atoi,硬编码argc检查)。
//命令行用法:sum num1 num2
int main(int argc,char * argv [])
{
if(argc< 3)
& {
cout<< 用法:< argv [0]<< num1 num2 \\\
;
exit(1);
}
int a = atoi(argv [1]);
int b = atoi(argv [2]);
int sum = a + b;
cout< Sum:< sum< \\\
;
return 0;
}
解决方案您可能想使用外部库。有很多可供选择。
Boost具有非常丰富的功能(像往常一样)库加入计划选项。
我最后的个人最爱几年来一直是 TCLAP - 纯粹模板化,因此没有库或链接,自动化帮助代和其他好东西。请参阅文档中的最简单的示例。
Is there a really effective way of dealing with command line parameters in C++?
What I'm doing below feels completely amateurish, and I can't imagine this is how command line parameters are really handled (atoi, hard-coded argc checks) in professional software.
// Command line usage: sum num1 num2 int main(int argc, char *argv[]) { if (argc < 3) { cout << "Usage: " << argv[0] << " num1 num2\n"; exit(1); }
int a = atoi(argv[1]); int b = atoi(argv[2]); int sum = a + b;
cout << "Sum: " << sum << "\n";
return 0; }
解决方案You probably want to use an external library for that. There are many to chose from.
Boost has a very feature-rich (as usual) library Boost Program Options.
My personal favorite for the last few years has been TCLAP -- purely templated, hence no library or linking, automated '--help' generation and other goodies. See the simplest example from the docs.
这篇关于在C ++中解析命令行参数有什么有效的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!