CommandLineParser命令行参数解析的使用

#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

程序输出:

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值