#include "opencv2/objdetect.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/videoio.hpp"
#include <iostream>
using namespace std;
using namespace cv;
int main( int argc, const char** argv )
{
/******************
"{参数名|默认参数|参数说明}"
参数名:可以设置多个同义词标识,用空格分开。可以使用@符号标记参数位置,然后通过get(index)获取参数。
默认参数:当命令行未设置该参数时,将使用默认参数,默认参数可以为空。
参数说明:对参数的用途进行说明。
*******************/
CommandLineParser parser(argc, argv,
"{help h usage ?||}"
"{path|./source_path|destination folder.}"
"{fps|30|fps for output video.}"
"{N count|50|count of objects.}"
"{@path_index|<none>|to mark argument as positional, prefix it with the @ symbol}"
"{@fps_index||to mark argument as positional, prefix it with the @ symbol}"
"{@count_index||to mark argument as positional, prefix it with the @ symbol}");
//检测命令行中是否包含该参数,可以使用同义词,如命令行使用-h,has("help")依然返回ture.
if(parser.has("help"))
{
cout<<"user guide!"<<endl;
return -1;
}
//检查参数.
if(!parser.check())
{
parser.printErrors();//参数转换出错、或缺少必要参数,打印错误提示信息!
return -1;
}
//返回当前程序的路径
String APP_path = parser.getPathToApplication();
cout<<"APP_path:"<<APP_path<<endl;
parser.about( "\nThis print message.\n");
parser.printMessage();
//1.根据参数名称获取参数(命令行输入:-path=./newpath -fps=66 -N=100)
String path = parser.get<String>("path");
int fps = parser.get<int>("fps");
int count = parser.get<int>("N");
cout<<"path:"<<path<<endl;
cout<<"fps:"<<fps<<endl;
cout<<"count:"<<count<<endl;
//2.根据顺序索引获取参数(命令行输入:-path=./newpath -fps=66 -N=100 newpathindex 88 6.66)
String path_index;
int fps_index;
double count_index;
/*当使用<none>作为默认参数后,在命令行必需输入此参数,否则将报错,不会返回空字符串*/
path_index = parser.get<String>(0);//从前往后,从0开始将@标记的参数名称建立索引,此处将提取@path_index参数。
cout<<"path_index:"<<path_index<<endl;
path_index = parser.get<String>("@path_index");//使用@标记后,依然可以使用参数名称获取参数,必需要带上@符号
//path_index = parser.get<String>("path_index");//没有@会报错
cout<<"@path_index:"<<path_index<<endl;
fps_index = parser.get<int>(1);
cout<<"fps_index:"<<fps_index<<endl;
count_index = parser.get<double>(2);
cout<<"count_index:"<<count_index<<endl;
return 0;
}
输入命令行参数:
-path=./newpath -fps=66 -N=100 newpathindex 88 6.66
程序输出: