Visual Servoing Platform  version 3.5.0 under development (2022-02-15)
vpParseArgv Class Reference

#include <visp3/io/vpParseArgv.h>

Public Types

enum  vpArgvType {
  ARGV_CONSTANT, ARGV_CONSTANT_INT, ARGV_CONSTANT_BOOL, ARGV_INT,
  ARGV_LONG, ARGV_STRING, ARGV_REST, ARGV_FLOAT,
  ARGV_DOUBLE, ARGV_FUNC, ARGV_GENFUNC, ARGV_HELP,
  ARGV_END
}
 
enum  vpArgvFlags {
  ARGV_NO_DEFAULTS = 0x1, ARGV_NO_LEFTOVERS = 0x2, ARGV_NO_ABBREV = 0x4, ARGV_DONT_SKIP_FIRST_ARG = 0x8,
  ARGV_NO_PRINT = 0x10
}
 

Static Public Member Functions

static bool parse (int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
 
static int parse (int argc, const char **argv, const char *validOpts, const char **param)
 

Static Public Attributes

static vpArgvInfo defaultTable [2]
 

Detailed Description

Command line argument parsing.

The code below shows a first way to parse command line arguments using vpParseArgv class. It allows to specify an option name with more than one character.

#include <stdio.h>
#include <visp3/core/vpMath.h>
#include <visp3/io/vpParseArgv.h>
// Usage : [-bool] [-int <integer value>] [-long <long value>]
// [-float <float value>] [-double <double value>] [-string <string value>] [-h]
int main(int argc, const char ** argv)
{
// Variables to set by command line parsing
bool b_val = false;
int i_val = 10;
long l_val = 123456;
float f_val = 0.1f;
double d_val = M_PI;
char *s_val;
// Parse the command line to set the variables
vpParseArgv::vpArgvInfo argTable[] =
{
{"-bool", vpParseArgv::ARGV_CONSTANT_BOOL, 0, (char *) &b_val,
"Flag enabled."},
{"-int", vpParseArgv::ARGV_INT, (char*) NULL, (char *) &i_val,
"An integer value."},
{"-long", vpParseArgv::ARGV_LONG, (char*) NULL, (char *) &l_val,
"An integer value."},
{"-float", vpParseArgv::ARGV_FLOAT, (char*) NULL, (char *) &f_val,
"A float value."},
{"-double", vpParseArgv::ARGV_DOUBLE, (char*) NULL, (char *) &d_val,
"A double value."},
{"-string", vpParseArgv::ARGV_STRING, (char*) NULL, (char *) &s_val,
"A string value."},
{"-h", vpParseArgv::ARGV_HELP, (char*) NULL, (char *) NULL,
"Print the help."},
{(char*) NULL, vpParseArgv::ARGV_END, (char*) NULL, (char*) NULL, (char*) NULL} } ;
// Read the command line options
if(vpParseArgv::parse(&argc, argv, argTable,
return (false);
}
// b_val, i_val, l_val, f_val, d_val, s_val may have new values
}

The code below shows an other way to parse command line arguments using vpParseArgv class. Here command line options are only one character long.

#include <stdio.h>
#include <stdlib.h>
#include <visp3/io/vpParseArgv.h>
#include <visp3/core/vpMath.h>
// List of allowed command line options
#define GETOPTARGS "bi:l:f:d:h" // double point mean here that the preceding option request an argument
// Usage : [-b] [-i <integer value>] [-l <long value>]
// [-f <float value>] [-d <double value>] [-s <string value>] [-h]
int main(int argc, const char ** argv)
{
// Variables to set by command line parsing
bool b_val = false;
int i_val = 10;
long l_val = 123456;
float f_val = 0.1f;
double d_val = M_PI;
char *s_val;
// Parse the command line to set the variables
const char *optarg;
int c;
while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg)) > 1) {
switch (c) {
case 'b': b_val = true; break;
case 'i': i_val = atoi(optarg); break;
case 'l': l_val = atol(optarg); break;
case 'f': f_val = static_cast<float>(atof(optarg)); break;
case 'd': d_val = atof(optarg); break;
case 's': sprintf(s_val, "%s", optarg); break;
case 'h': printf("Usage: ...\n"); return EXIT_SUCCESS; break;
default:
printf("Usage: ...\n"); return EXIT_SUCCESS; break;
}
}
if ((c == 1) || (c == -1)) {
// standalone param or error
printf("Usage: ...\n");
return EXIT_FAILURE;
}
// b_val, i_val, l_val, f_val, d_val, s_val may have new values
}

Definition at line 147 of file vpParseArgv.h.

Member Enumeration Documentation

◆ vpArgvFlags

Flag bits.

Enumerator
ARGV_NO_DEFAULTS 

No default options like -help.

ARGV_NO_LEFTOVERS 

Print an error message if an option is not in the argument list.

ARGV_NO_ABBREV 

No abrevation. Print an error message if an option is abrevated (ie "-i" in place of "-int" which is requested).

ARGV_DONT_SKIP_FIRST_ARG 

Don't skip first argument.

ARGV_NO_PRINT 

No printings.

Definition at line 172 of file vpParseArgv.h.

◆ vpArgvType

Legal values for the type field of a vpArgvInfo.

Enumerator
ARGV_CONSTANT 

Stand alone argument. Same as vpParseArgv::ARGV_CONSTANT_INT.

ARGV_CONSTANT_INT 

Stand alone argument associated to an int var that is set to 1.

ARGV_CONSTANT_BOOL 

Stand alone argument associated to a bool var that is set to true.

ARGV_INT 

Argument is associated to an int.

ARGV_LONG 

Argument is associated to a long.

ARGV_STRING 

Argument is associated to a char * string.

ARGV_REST 
ARGV_FLOAT 

Argument is associated to a float.

ARGV_DOUBLE 

Argument is associated to a double.

ARGV_FUNC 
ARGV_GENFUNC 
ARGV_HELP 

Argument is for help displaying.

ARGV_END 

End of the argument list.

Definition at line 153 of file vpParseArgv.h.

Member Function Documentation

◆ parse() [1/2]

bool vpParseArgv::parse ( int *  argcPtr,
const char **  argv,
vpArgvInfo *  argTable,
int  flags 
)
static

Process an argv array according to a table of expectedvcommand-line options.

The return value is a boolean value with true indicating an error. If an error occurs then an error message is printed on stderr. Under normal conditions, both *argcPtr and *argv are modified to return the arguments that couldn't be processed here (they didn't match the option table, or followed an vpParseArgv::ARGV_REST argument).

Parameters
argcPtrPointer to the count of command line arguments.
argvArray of command line argument strings.
argTableArray of command-specific argument descriptions.
flagsThis parameter is to set with vpParseArgv::vpArgvFlags values or combinations of these values using the OR operator (vpParseArgv::ARGV_NO_LEFTOVERS | vpParseArgv::ARGV_NO_DEFAULTS). If the vpParseArgv::ARGV_NO_DEFAULTS bit is set, then don't generate information for default options.
Examples:
AROgre.cpp, AROgreBasic.cpp, BSpline.cpp, displayD3D.cpp, displayGTK.cpp, displayOpenCV.cpp, displaySequence.cpp, displayX.cpp, displayXMulti.cpp, fernClassifier.cpp, grab1394CMU.cpp, grab1394Two.cpp, grabDirectShow.cpp, grabDirectShowMulti.cpp, grabDisk.cpp, grabFlyCapture.cpp, grabV4l2.cpp, grabV4l2MultiCpp11Thread.cpp, histogram.cpp, homographyHartleyDLT2DObject.cpp, homographyHLM2DObject.cpp, homographyHLM3DObject.cpp, homographyRansac2DObject.cpp, imageDiskRW.cpp, imageSequenceReader.cpp, keyPointSurf.cpp, mbtEdgeKltTracking.cpp, mbtEdgeTracking.cpp, mbtGenericTracking.cpp, mbtGenericTracking2.cpp, mbtGenericTrackingDepth.cpp, mbtGenericTrackingDepthOnly.cpp, mbtKltTracking.cpp, moveAfma4.cpp, moveBiclops.cpp, parallelPort.cpp, parse-argv1.cpp, parse-argv2.cpp, photometricVisualServoing.cpp, photometricVisualServoingWithoutVpServo.cpp, planarObjectDetector.cpp, poseVirtualVS.cpp, ringLight.cpp, servoAfma4Point2DCamVelocityKalman.cpp, servoBiclopsPoint2DArtVelocity.cpp, servoSimu3D_cdMc_CamVelocity.cpp, servoSimu3D_cdMc_CamVelocityWithoutVpServo.cpp, servoSimu3D_cMcd_CamVelocity.cpp, servoSimu3D_cMcd_CamVelocityWithoutVpServo.cpp, servoSimu4Points.cpp, servoSimuAfma6FourPoints2DCamVelocity.cpp, servoSimuCircle2DCamVelocity.cpp, servoSimuCircle2DCamVelocityDisplay.cpp, servoSimuCylinder.cpp, servoSimuCylinder2DCamVelocityDisplay.cpp, servoSimuCylinder2DCamVelocityDisplaySecondaryTask.cpp, servoSimuFourPoints2DCamVelocity.cpp, servoSimuFourPoints2DCamVelocityDisplay.cpp, servoSimuFourPoints2DPolarCamVelocityDisplay.cpp, servoSimuLine2DCamVelocityDisplay.cpp, servoSimuPoint2DCamVelocity1.cpp, servoSimuPoint2DCamVelocity2.cpp, servoSimuPoint2DCamVelocity3.cpp, servoSimuPoint2DhalfCamVelocity1.cpp, servoSimuPoint2DhalfCamVelocity2.cpp, servoSimuPoint2DhalfCamVelocity3.cpp, servoSimuPoint3DCamVelocity.cpp, servoSimuSphere.cpp, servoSimuSphere2DCamVelocity.cpp, servoSimuSphere2DCamVelocityDisplay.cpp, servoSimuSphere2DCamVelocityDisplaySecondaryTask.cpp, servoSimuSquareLine2DCamVelocityDisplay.cpp, servoSimuThetaUCamVelocity.cpp, servoSimuViper850FourPoints2DCamVelocity.cpp, SickLDMRS-Process.cpp, simulateCircle2DCamVelocity.cpp, simulateFourPoints2DCartesianCamVelocity.cpp, simulateFourPoints2DPolarCamVelocity.cpp, templateTracker.cpp, testAutoThreshold.cpp, testClick.cpp, testConnectedComponents.cpp, testContours.cpp, testConversion.cpp, testCrop.cpp, testCropAdvanced.cpp, testDisplayPolygonLines.cpp, testDisplayRoi.cpp, testDisplays.cpp, testFeatureSegment.cpp, testFloodFill.cpp, testGenericTracker.cpp, testGenericTrackerDepth.cpp, testHistogram.cpp, testImageComparison.cpp, testImageFilter.cpp, testImageNormalizedCorrelation.cpp, testImageTemplateMatching.cpp, testImgproc.cpp, testIoPGM.cpp, testIoPPM.cpp, testKeyPoint-2.cpp, testKeyPoint-3.cpp, testKeyPoint-4.cpp, testKeyPoint-5.cpp, testKeyPoint-6.cpp, testKeyPoint-7.cpp, testKeyPoint.cpp, testMatrixDeterminant.cpp, testMatrixInverse.cpp, testMatrixPseudoInverse.cpp, testMouseEvent.cpp, testNurbs.cpp, testPerformanceLUT.cpp, testReadImage.cpp, testRobust.cpp, testSvd.cpp, testTrackDot.cpp, testUndistortImage.cpp, testVideoDevice.cpp, testVideoDeviceDual.cpp, testXmlParser.cpp, trackDot.cpp, trackDot2.cpp, trackDot2WithAutoDetection.cpp, trackKltOpencv.cpp, trackMeCircle.cpp, trackMeEllipse.cpp, trackMeLine.cpp, trackMeNurbs.cpp, videoReader.cpp, and wireframeSimulator.cpp.

Definition at line 69 of file vpParseArgv.cpp.

References ARGV_CONSTANT, ARGV_CONSTANT_BOOL, ARGV_CONSTANT_INT, ARGV_DONT_SKIP_FIRST_ARG, ARGV_DOUBLE, ARGV_END, ARGV_FLOAT, ARGV_FUNC, ARGV_GENFUNC, ARGV_HELP, ARGV_INT, ARGV_LONG, ARGV_NO_ABBREV, ARGV_NO_DEFAULTS, ARGV_NO_LEFTOVERS, ARGV_REST, ARGV_STRING, and defaultTable.

◆ parse() [2/2]

int vpParseArgv::parse ( int  argc,
const char **  argv,
const char *  validOpts,
const char **  param 
)
static

Get next command line option and parameter.

Parameters
argcCount of command line arguments.
argvArray of command line argument strings.
validOptsString of valid case-sensitive option characters, a ':' following a given character means that option can take a parameter.
paramPointer to a pointer to a string for output.
Returns
If valid option is found, the character value of that option is returned, and *param points to the parameter if given, or is NULL if no param.
If standalone parameter (with no option) is found, 1 is returned, and *param points to the standalone parameter
If option is found, but it is not in the list of valid options, -1 is returned, and *param points to the invalid argument.
When end of argument list is reached, 0 is returned, and *param is NULL.

Definition at line 498 of file vpParseArgv.cpp.

Member Data Documentation

◆ defaultTable

vpParseArgv::vpArgvInfo vpParseArgv::defaultTable
static
Initial value:
= {
{"-help", ARGV_HELP, (char *)NULL, (char *)NULL, "Print summary of command-line options and abort.\n"},
{NULL, ARGV_END, (char *)NULL, (char *)NULL, (char *)NULL}}

Definition at line 195 of file vpParseArgv.h.

Referenced by parse().