Skip to content

Commit a2769e1

Browse files
committed
CVV - Replace getopt with CommandLineParser
1 parent bccbec7 commit a2769e1

File tree

1 file changed

+18
-23
lines changed

1 file changed

+18
-23
lines changed

modules/cvv/samples/cvv_demo.cpp

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// system includes
2-
#include <getopt.h>
32
#include <iostream>
43

54
// library includes
@@ -16,6 +15,7 @@
1615
#include <opencv2/cvv/dmatch.hpp>
1716
#include <opencv2/cvv/final_show.hpp>
1817

18+
using namespace std;
1919
using namespace cv;
2020

2121
template<class T> std::string toString(const T& p_arg)
@@ -42,28 +42,22 @@ main(int argc, char** argv)
4242
{
4343
cv::Size* resolution = nullptr;
4444

45-
// parse options
46-
const char* optstring = "hr:";
47-
int opt;
48-
while ((opt = getopt(argc, argv, optstring)) != -1) {
49-
switch (opt) {
50-
case 'h':
51-
usage();
52-
return 0;
53-
break;
54-
case 'r':
55-
{
56-
char dummych;
57-
resolution = new cv::Size();
58-
if (sscanf(optarg, "%d%c%d", &resolution->width, &dummych, &resolution->height) != 3) {
59-
printf("%s not a valid resolution\n", optarg);
60-
return 1;
61-
}
62-
}
63-
break;
64-
default: /* '?' */
65-
usage();
66-
return 2;
45+
// parser keys
46+
const char *keys =
47+
"{ help h usage ? | | show this message }"
48+
"{ resolution r |0x0| resolution to width and height in the format WxH }";
49+
CommandLineParser parser(argc, argv, keys);
50+
string res(parser.get<string>("resolution"));
51+
if (parser.has("help")) {
52+
usage();
53+
return 0;
54+
}
55+
if (res != "0x0") {
56+
char dummych;
57+
resolution = new cv::Size();
58+
if (sscanf(res.c_str(), "%d%c%d", &resolution->width, &dummych, &resolution->height) != 3) {
59+
cout << res << " not a valid resolution" << endl;
60+
return 1;
6761
}
6862
}
6963

@@ -78,6 +72,7 @@ main(int argc, char** argv)
7872
printf("Setting resolution to %dx%d\n", resolution->width, resolution->height);
7973
capture.set(CV_CAP_PROP_FRAME_WIDTH, resolution->width);
8074
capture.set(CV_CAP_PROP_FRAME_HEIGHT, resolution->height);
75+
delete resolution;
8176
}
8277

8378

0 commit comments

Comments
 (0)