Class cv::CommandLineParser#
Designed for command line parsing. View details
#include <opencv2/core/utility.hpp>Collaboration diagram for cv::CommandLineParser:
Detailed Description#
-
class CommandLineParser#
Designed for command line parsing.
The sample below demonstrates how to use CommandLineParser:
CommandLineParser parser(argc, argv, keys); parser.about("Application name v1.0.0"); if (parser.has("help")) { parser.printMessage(); return 0; } int N = parser.get<int>("N"); double fps = parser.get<double>("fps"); String path = parser.get<String>("path"); use_time_stamp = parser.has("timestamp"); String img1 = parser.get<String>(0); String img2 = parser.get<String>(1); int repeat = parser.get<int>(2); if (!parser.check()) { parser.printErrors(); return 0; }Keys syntax#
The keys parameter is a string containing several blocks, each one is enclosed in curly braces and describes one argument. Each argument contains three parts separated by the
|symbol:argument names is a list of option synonyms separated by standard space characters ‘ ‘ (to mark argument as positional, prefix it with the
@symbol)default value will be used if the argument was not provided (can be empty)
help message (can be empty)
For example:
const String keys = "{help h usage ? | | print this message }" "{@image1 | | image1 for compare }" "{@image2 |<none>| image2 for compare }" "{@repeat |1 | number }" "{path |. | path to file }" "{fps | -1.0 | fps for output video }" "{N count |100 | count of objects }" "{ts timestamp | | use time stamp }" ; }
Note that there are no default values for
helpandtimestampso we can check their presence using thehas()method. Arguments with default values are considered to be always present. Use theget()method in these cases to check their actual value instead. Note that whitespace characters other than standard spaces are considered part of the string. Additionally, leading and trailing standard spaces around the help messages are ignored.String keys like
get<String>("@image1")return the empty string""by default - even with an empty default value. Use the special<none>default value to enforce that the returned string must not be empty. (like inget<String>("@image2"))Usage#
For the described keys:
# Good call (3 positional parameters: image1, image2 and repeat; N is 200, ts is true) $ ./app -N=200 1.png 2.jpg 19 -ts # Bad call $ ./app -fps=aaa ERRORS: Parameter 'fps': can not convert: [aaa] to [double]
- Examples
- samples/facedetect.cpp, samples/hog_tapi.cpp, samples/peopledetect.cpp, samples/cpp/connected_components.cpp, samples/cpp/cout_mat.cpp, samples/cpp/demhist.cpp, samples/cpp/floodfill.cpp, samples/cpp/geometry.cpp, samples/cpp/grabcut.cpp, samples/cpp/image_alignment.cpp, samples/cpp/lkdemo.cpp, samples/cpp/pca.cpp, samples/cpp/tutorial_code/Histograms_Matching/MatchTemplate_Demo.cpp, samples/cpp/tutorial_code/ImgProc/Morphology_1.cpp, samples/cpp/tutorial_code/ImgProc/Morphology_2.cpp, samples/cpp/tutorial_code/ImgTrans/Sobel_Demo.cpp, samples/cpp/tutorial_code/features/Homography/decompose_homography.cpp, samples/cpp/tutorial_code/features/Homography/homography_from_camera_displacement.cpp, samples/cpp/tutorial_code/features/Homography/pose_from_homography.cpp, samples/cpp/tutorial_code/ml/introduction_to_pca/introduction_to_pca.cpp, samples/cpp/tutorial_code/photo/non_photorealistic_rendering/npr_demo.cpp, samples/cpp/videowriter.cpp, samples/dnn/classification.cpp, samples/dnn/colorization.cpp, samples/dnn/object_detection.cpp, samples/dnn/openpose.cpp, samples/dnn/segmentation.cpp, and samples/dnn/text_detection.cpp.
Constructor & Destructor Documentation#
CommandLineParser()#
cv::CommandLineParser::CommandLineParser(const CommandLineParser & parser)
Copy constructor.
CommandLineParser()#
cv::CommandLineParser::CommandLineParser(
int argc,
const char *const[] argv,
const String & keys )
Constructor.
Initializes command line parser object
Parameters
argc— number of command line arguments (from main())argv— array of command line arguments (from main())keys— string describing acceptable command line parameters (see class description for syntax)
~CommandLineParser()#
cv::CommandLineParser::~CommandLineParser()
Destructor.
Member Function Documentation#
about()#
void cv::CommandLineParser::about(const String & message)
Set the about message.
The about message will be shown when printMessage is called, right before arguments table.
check()#
bool cv::CommandLineParser::check()
Check for parsing errors.
Returns false if error occurred while accessing the parameters (bad conversion, missing arguments, etc.). Call printErrors to print error messages list.
get()#
template<typename T>
T cv::CommandLineParser::get(
const String & name,
bool space_delete = true )
Access arguments by name.
Returns argument converted to selected type. If the argument is not known or can not be converted to selected type, the error flag is set (can be checked with check).
For example, define:
String keys = "{N count||}";
Call:
$ ./my-app -N=20
## or
$ ./my-app --count=20
Access:
int N = parser.get<int>("N");
Parameters
name— name of the argumentspace_delete— remove spaces from the left and right of the stringT— the argument will be converted to this type if possible
get()#
template<typename T>
T cv::CommandLineParser::get(
int index,
bool space_delete = true )
Access positional arguments by index.
Returns argument converted to selected type. Indexes are counted from zero.
For example, define:
String keys = "{@arg1||}{@arg2||}"
Call:
./my-app abc qwe
Access arguments:
Parameters
index— index of the argumentspace_delete— remove spaces from the left and right of the stringT— the argument will be converted to this type if possible
getPathToApplication()#
String cv::CommandLineParser::getPathToApplication()
Returns application path.
This method returns the path to the executable from the command line (argv[0]).
For example, if the application has been started with such a command:
$ ./bin/my-executable
this method will return ./bin.
has()#
bool cv::CommandLineParser::has(const String & name)
Check if field was provided in the command line.
Parameters
name— argument name to check
operator=()#
CommandLineParser & cv::CommandLineParser::operator=(const CommandLineParser & parser)
Assignment operator.
printErrors()#
void cv::CommandLineParser::printErrors()
Print list of errors occurred.
See also
printMessage()#
void cv::CommandLineParser::printMessage()
Print help message.
This method will print standard help message containing the about message and arguments description.
See also
getByIndex()#
void cv::CommandLineParser::getByIndex(
int index,
bool space_delete,
Param type,
void * dst )
getByName()#
void cv::CommandLineParser::getByName(
const String & name,
bool space_delete,
Param type,
void * dst )
Member Data Documentation#
impl#
Impl * cv::CommandLineParser::impl
Source file#
The documentation for this class was generated from the following file:
opencv2/core/utility.hpp