32 #include <visp3/io/vpParseArgv.h>
42 {
"-help", ARGV_HELP, (
char *)
nullptr, (
char *)
nullptr,
"Print summary of command-line options and abort.\n"},
43 {
nullptr, ARGV_END, (
char *)
nullptr, (
char *)
nullptr, (
char *)
nullptr} };
45 int (*handlerProc1)(
const char *dst,
const char *key,
const char *argument);
46 int (*handlerProc2)(
const char *dst,
const char *key,
int valargc,
const char **argument);
88 unsigned long long nargs;
92 if (!(flags & ARGV_NO_PRINT)) \
96 srcIndex = dstIndex = 0;
100 srcIndex = dstIndex = 1;
105 curArg = argv[srcIndex];
109 length = strlen(curArg);
118 for (
unsigned int i = 0; i < 2; ++i) {
125 for (; infoPtr->type !=
ARGV_END; ++infoPtr) {
126 if (infoPtr->key !=
nullptr) {
127 bool stop_for_loop =
false;
128 if ((infoPtr->key[1] != c) || (strncmp(infoPtr->key, curArg, length) != 0)) {
129 stop_for_loop =
true;;
131 if (!stop_for_loop) {
132 if (infoPtr->key[length] == 0) {
137 stop_for_loop =
true;
139 if (!stop_for_loop) {
140 if (matchPtr !=
nullptr) {
141 FPRINTF(stderr,
"ambiguous option \"%s\"\n", curArg);
150 if (matchPtr ==
nullptr) {
158 FPRINTF(stderr,
"unrecognized argument \"%s\"\n", curArg);
160 argv[dstIndex] = curArg;
170 switch (infoPtr->type) {
173 *((
int *)infoPtr->dst) = 1;
176 *((
bool *)infoPtr->dst) =
true;
179 nargs = (uintptr_t)infoPtr->src;
182 for (
unsigned long i = 0; i < nargs; ++i) {
187 char *endPtr =
nullptr;
189 *(((
int *)infoPtr->dst) + i) = (
int)strtol(argv[srcIndex], &endPtr, 0);
190 if ((endPtr == argv[srcIndex]) || (*endPtr != 0)) {
191 FPRINTF(stderr,
"expected integer argument for \"%s\" but got \"%s\"\n", infoPtr->key, argv[srcIndex]);
200 nargs = (uintptr_t)infoPtr->src;
203 for (
unsigned long i = 0; i < nargs; ++i) {
208 char *endPtr =
nullptr;
210 *(((
long *)infoPtr->dst) + i) = strtol(argv[srcIndex], &endPtr, 0);
211 if ((endPtr == argv[srcIndex]) || (*endPtr != 0)) {
212 FPRINTF(stderr,
"expected long argument for \"%s\" but got \"%s\"\n", infoPtr->key, argv[srcIndex]);
221 nargs = (uintptr_t)infoPtr->src;
224 for (
unsigned long i = 0; i < nargs; ++i) {
229 *(((
const char **)infoPtr->dst) + i) = argv[srcIndex];
236 *((
int *)infoPtr->dst) = dstIndex;
239 nargs = (uintptr_t)infoPtr->src;
242 for (
unsigned long i = 0; i < nargs; ++i) {
249 *(((
float *)infoPtr->dst) + i) = (
float)strtod(argv[srcIndex], &endPtr);
250 if ((endPtr == argv[srcIndex]) || (*endPtr != 0)) {
251 FPRINTF(stderr,
"expected floating-point argument for \"%s\" but got\"%s\"\n", infoPtr->key,
261 nargs = (uintptr_t)infoPtr->src;
264 for (
unsigned long i = 0; i < nargs; ++i) {
271 *(((
double *)infoPtr->dst) + i) = strtod(argv[srcIndex], &endPtr);
272 if ((endPtr == argv[srcIndex]) || (*endPtr != 0)) {
273 FPRINTF(stderr,
"expected double-point argument for \"%s\" but got\"%s\"\n", infoPtr->key, argv[srcIndex]);
283 handlerProc1 = (int (*)(
const char *dst,
const char *key,
const char *argument))infoPtr->src;
285 if ((*handlerProc1)(infoPtr->dst, infoPtr->key, argv[srcIndex])) {
292 handlerProc2 = (int (*)(
const char *dst,
const char *key,
int valargc,
const char **argument))infoPtr->src;
294 argc = (*handlerProc2)(infoPtr->dst, infoPtr->key, argc, argv + srcIndex);
302 printUsage(argTable, flags);
306 FPRINTF(stderr,
"bad argument type %d in vpArgvInfo", infoPtr->type);
318 argv[dstIndex] = argv[srcIndex];
323 argv[dstIndex] = (
char *)
nullptr;
328 FPRINTF(stderr,
"\"%s\" option requires an additional argument\n", curArg);
348 void vpParseArgv::printUsage(vpArgvInfo *argTable,
int flags)
353 #define NUM_SPACES 20
354 static char spaces[] =
" ";
356 unsigned long long nargs;
360 if (!(flags & ARGV_NO_PRINT)) \
369 for (
unsigned int i = 0; i < 2; ++i) {
372 if (infoPtr->key ==
nullptr) {
375 length = (int)strlen(infoPtr->key);
376 if (length > width) {
382 FPRINTF(stderr,
"Command-specific options:");
383 for (
unsigned int i = 0;; ++i) {
385 if ((infoPtr->type ==
ARGV_HELP) && (infoPtr->key ==
nullptr)) {
386 FPRINTF(stderr,
"\n%s", infoPtr->help);
389 FPRINTF(stderr,
"\n %s:", infoPtr->key);
390 numSpaces = width + 1 - (int)strlen(infoPtr->key);
391 while (numSpaces > 0) {
392 if (numSpaces >= NUM_SPACES) {
393 FPRINTF(stderr,
"%s", spaces);
396 FPRINTF(stderr,
"%s", spaces + NUM_SPACES - numSpaces);
398 numSpaces -= NUM_SPACES;
400 FPRINTF(stderr,
"%s", infoPtr->help);
401 switch (infoPtr->type) {
403 FPRINTF(stderr,
"\n\t\tDefault value:");
404 nargs = (uintptr_t)infoPtr->src;
407 for (
unsigned long j = 0; j < nargs; ++j) {
408 FPRINTF(stderr,
" %d", *(((
int *)infoPtr->dst) + j));
413 FPRINTF(stderr,
"\n\t\tDefault value:");
414 nargs = (uintptr_t)infoPtr->src;
417 for (
unsigned long j = 0; j < nargs; ++j) {
418 FPRINTF(stderr,
" %ld", *(((
long *)infoPtr->dst) + j));
423 FPRINTF(stderr,
"\n\t\tDefault value:");
424 nargs = (uintptr_t)infoPtr->src;
427 for (
unsigned long j = 0; j < nargs; ++j) {
428 FPRINTF(stderr,
" %f", *(((
float *)infoPtr->dst) + j));
433 FPRINTF(stderr,
"\n\t\tDefault value:");
434 nargs = (uintptr_t)infoPtr->src;
437 for (
unsigned long j = 0; j < nargs; ++j) {
438 FPRINTF(stderr,
" %g", *(((
double *)infoPtr->dst) + j));
445 nargs = (uintptr_t)infoPtr->src;
448 string = *((
const char **)infoPtr->dst);
449 if ((nargs == 1) && (
string ==
nullptr))
451 for (
unsigned long j = 0; j < nargs; ++j) {
452 string = *(((
const char **)infoPtr->dst) + j);
453 if (
string !=
nullptr) {
454 FPRINTF(stderr,
" \"%s\"",
string);
477 FPRINTF(stderr,
"\nGeneric options for all commands:");
480 FPRINTF(stderr,
"\n");
515 const char *psz =
nullptr;
516 const char *pszParam =
nullptr;
519 psz = &(argv[iArg][0]);
522 chOpt = argv[iArg][1];
523 if (isalnum(chOpt) || ispunct(chOpt)) {
525 psz = strchr(validOpts, chOpt);
526 if (psz !=
nullptr) {
530 psz = &(argv[iArg][2]);
533 if (iArg + 1 < argc) {
534 psz = &(argv[iArg + 1][0]);
543 pszParam = &(argv[iArg][0]);
559 pszParam = &(argv[iArg][0]);
566 pszParam = &(argv[iArg][0]);
572 pszParam = &(argv[iArg][0]);
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
static vpArgvInfo defaultTable[2]
@ ARGV_NO_DEFAULTS
No default options like -help.
@ ARGV_DONT_SKIP_FIRST_ARG
Don't skip first argument.
@ ARGV_NO_LEFTOVERS
Print an error message if an option is not in the argument list.
@ ARGV_DOUBLE
Argument is associated to a double.
@ ARGV_LONG
Argument is associated to a long.
@ ARGV_STRING
Argument is associated to a char * string.
@ ARGV_FLOAT
Argument is associated to a float.
@ ARGV_CONSTANT
Stand alone argument. Same as vpParseArgv::ARGV_CONSTANT_INT.
@ ARGV_INT
Argument is associated to an int.
@ ARGV_CONSTANT_BOOL
Stand alone argument associated to a bool var that is set to true.
@ ARGV_CONSTANT_INT
Stand alone argument associated to an int var that is set to 1.
@ ARGV_END
End of the argument list.
@ ARGV_HELP
Argument is for help displaying.