cv::CommandLineParser Class Reference 核心功能 » 实用程序、系统函数及宏


Designed for command line parsing. 更多...

#include <opencv2/core/utility.hpp>

Public Member Functions

  CommandLineParser (int argc, const char *const argv[], const 字符串 &keys)
  构造函数。 更多...
 
  CommandLineParser (const CommandLineParser &parser)
  Copy constructor. 更多...
 
  ~CommandLineParser ()
  析构函数。 更多...
 
void  about (const 字符串 &message)
  Set the about message. 更多...
 
bool  check () const
  Check for parsing errors. 更多...
 
template<typename T >
get (const 字符串 &name, bool space_delete=true) const
  Access arguments by name. 更多...
 
template<typename T >
get (int index, bool space_delete=true) const
  Access positional arguments by index. 更多...
 
字符串   getPathToApplication () const
  Returns application path. 更多...
 
bool  has (const 字符串 &name) const
  Check if field was provided in the command line. 更多...
 
CommandLineParser operator= (const CommandLineParser &parser)
  Assignment operator. 更多...
 
void  printErrors () const
  Print list of errors occurred. 更多...
 
void  printMessage () const
  Print help message. 更多...
 

Protected Member Functions

void  getByIndex (int index, bool space_delete, Param type, void *dst) const
 
void  getByName (const 字符串 &name, bool space_delete, Param type, void *dst) const
 

Protected Attributes

Impl *  impl
 

详细描述

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" );
字符串 path = parser.get< 字符串 >( "path" );
use_time_stamp = parser.has( "timestamp" );
字符串 img1 = parser.get< 字符串 >(0);
字符串 img2 = parser.get< 字符串 >(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:

  1. argument names is a space-separated list of option synonyms (to mark argument as positional, prefix it with the @ symbol)
  2. default value will be used if the argument was not provided (can be empty)
  3. help message (can be empty)

例如:

const 字符串 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 help and timestamp so we can check their presence using the has() method. Arguments with default values are considered to be always present. Use the get() method in these cases to check their actual value instead.

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 in get<String>("@image2") )

用法

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]
范例:
fld_lines.cpp , modules/shape/samples/shape_example.cpp , samples/cpp/camshiftdemo.cpp , samples/cpp/connected_components.cpp , samples/cpp/contours2.cpp , samples/cpp/convexhull.cpp , samples/cpp/cout_mat.cpp , samples/cpp/create_mask.cpp , samples/cpp/demhist.cpp , samples/cpp/distrans.cpp , samples/cpp/edge.cpp , samples/cpp/facedetect.cpp , samples/cpp/ffilldemo.cpp , samples/cpp/filestorage.cpp , samples/cpp/fitellipse.cpp , samples/cpp/grabcut.cpp , samples/cpp/image_alignment.cpp , samples/cpp/laplace.cpp , samples/cpp/lkdemo.cpp , samples/cpp/pca.cpp , samples/cpp/peopledetect.cpp , samples/cpp/polar_transforms.cpp , samples/cpp/segment_objects.cpp , samples/cpp/train_HOG.cpp , samples/cpp/tutorial_code/features2D/Homography/decompose_homography.cpp , samples/cpp/tutorial_code/features2D/Homography/homography_from_camera_displacement.cpp , samples/cpp/tutorial_code/features2D/Homography/pose_from_homography.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/ml/introduction_to_pca/introduction_to_pca.cpp , samples/cpp/tutorial_code/photo/non_photorealistic_rendering/npr_demo.cpp , samples/cpp/warpPerspective_demo.cpp , samples/cpp/watershed.cpp , samples/dnn/classification.cpp , samples/dnn/colorization.cpp , samples/dnn/object_detection.cpp , samples/dnn/openpose.cpp , samples/dnn/segmentation.cpp , samples/dnn/text_detection.cpp , samples/tapi/hog.cpp ,和 samples/tapi/squares.cpp .

Constructor & Destructor Documentation

◆  CommandLineParser() [1/2]

cv::CommandLineParser::CommandLineParser ( int  argc ,
const char *const  argv [],
const 字符串 keys  
)

构造函数。

Initializes command line parser object

参数
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() [2/2]

cv::CommandLineParser::CommandLineParser ( const CommandLineParser parser )

Copy constructor.

◆  ~CommandLineParser()

cv::CommandLineParser::~CommandLineParser ( )

析构函数。

成员函数文档编制

◆  about()

◆  check()

bool cv::CommandLineParser::check ( ) const

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.

范例:
modules/shape/samples/shape_example.cpp , samples/cpp/facedetect.cpp , samples/cpp/laplace.cpp , samples/cpp/peopledetect.cpp ,和 samples/dnn/classification.cpp .

◆  get() [1/2]

template<typename T >
T cv::CommandLineParser::get ( const 字符串 name ,
bool  space_delete = true  
) const
inline

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:

字符串 keys = "{N count||}" ;

Call:

$ ./my-app -N=20
# or
$ ./my-app --count=20

Access:

int N = parser.get< int >( "N" );
参数
name name of the argument
space_delete remove spaces from the left and right of the string
Template Parameters
T the argument will be converted to this type if possible
注意
You can access positional arguments by their @ -prefixed name:
parser.get< 字符串 >( "@image" );
范例:
fld_lines.cpp , modules/shape/samples/shape_example.cpp , samples/cpp/camshiftdemo.cpp , samples/cpp/connected_components.cpp , samples/cpp/demhist.cpp , samples/cpp/distrans.cpp , samples/cpp/edge.cpp , samples/cpp/facedetect.cpp , samples/cpp/ffilldemo.cpp , samples/cpp/filestorage.cpp , samples/cpp/fitellipse.cpp , samples/cpp/grabcut.cpp , samples/cpp/laplace.cpp , samples/cpp/lkdemo.cpp , samples/cpp/pca.cpp , samples/cpp/peopledetect.cpp , samples/cpp/polar_transforms.cpp , samples/cpp/segment_objects.cpp , samples/cpp/tutorial_code/features2D/Homography/decompose_homography.cpp , samples/cpp/tutorial_code/features2D/Homography/homography_from_camera_displacement.cpp , samples/cpp/tutorial_code/features2D/Homography/pose_from_homography.cpp , samples/cpp/tutorial_code/ImgTrans/Sobel_Demo.cpp , samples/cpp/tutorial_code/photo/non_photorealistic_rendering/npr_demo.cpp , samples/cpp/warpPerspective_demo.cpp , samples/cpp/watershed.cpp , samples/dnn/classification.cpp , samples/dnn/openpose.cpp , samples/tapi/hog.cpp ,和 samples/tapi/squares.cpp .

◆  get() [2/2]

template<typename T >
T cv::CommandLineParser::get ( int  index ,
bool  space_delete = true  
) const
inline

Access positional arguments by index.

Returns argument converted to selected type. Indexes are counted from zero.

For example, define:

字符串 keys = "{@arg1||}{@arg2||}"

Call:

./my-app abc qwe

Access arguments:

字符串 val_1 = parser.get< 字符串 >(0); // returns "abc", arg1
字符串 val_2 = parser.get< 字符串 >(1); // returns "qwe", arg2
参数
index index of the argument
space_delete remove spaces from the left and right of the string
Template Parameters
T the argument will be converted to this type if possible

◆  getByIndex()

void cv::CommandLineParser::getByIndex ( int  index ,
bool  space_delete ,
Param   type ,
void *  dst  
) const
protected

◆  getByName()

void cv::CommandLineParser::getByName ( const 字符串 name ,
bool  space_delete ,
Param   type ,
void *  dst  
) const
protected

◆  getPathToApplication()

字符串 cv::CommandLineParser::getPathToApplication ( ) const

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()

◆  operator=()

CommandLineParser & cv::CommandLineParser::operator= ( const CommandLineParser parser )

Assignment operator.

◆  printErrors()

void cv::CommandLineParser::printErrors ( ) const

◆  printMessage()

Member Data Documentation

◆  impl

Impl* cv::CommandLineParser::impl
protected

The documentation for this class was generated from the following file: