33 #include <visp3/io/vpParseArgv.h> 41 {
"-help", ARGV_HELP, (
char *)NULL, (
char *)NULL, "Print summary of command-line options and abort.\n"},
42 {NULL, ARGV_END, (
char *)NULL, (
char *)NULL, (
char *)NULL}};
44 int (*handlerProc1)(
const char *dst,
const char *key,
const char *argument);
45 int (*handlerProc2)(
const char *dst,
const char *key,
int valargc,
const char **argument);
87 unsigned long long nargs;
91 if (!(flags & ARGV_NO_PRINT)) \ 95 srcIndex = dstIndex = 0;
98 srcIndex = dstIndex = 1;
103 curArg = argv[srcIndex];
107 length = strlen(curArg);
116 for (
unsigned int i = 0; i < 2; i++) {
122 for (; infoPtr->type !=
ARGV_END; infoPtr++) {
123 if (infoPtr->key == NULL) {
126 if ((infoPtr->key[1] != c) || (strncmp(infoPtr->key, curArg, length) != 0)) {
129 if (infoPtr->key[length] == 0) {
136 if (matchPtr != NULL) {
137 FPRINTF(stderr,
"ambiguous option \"%s\"\n", curArg);
143 if (matchPtr == NULL) {
151 FPRINTF(stderr,
"unrecognized argument \"%s\"\n", curArg);
153 argv[dstIndex] = curArg;
163 switch (infoPtr->type) {
166 *((
int *)infoPtr->dst) = 1;
169 *((
bool *)infoPtr->dst) =
true;
172 nargs = (uintptr_t)infoPtr->src;
175 for (
unsigned long i = 0; i < nargs; i++) {
181 *(((
int *)infoPtr->dst) + i) = (
int)strtol(argv[srcIndex], &endPtr, 0);
182 if ((endPtr == argv[srcIndex]) || (*endPtr != 0)) {
183 FPRINTF(stderr,
"expected integer argument for \"%s\" but got \"%s\"\n", infoPtr->key, argv[srcIndex]);
192 nargs = (uintptr_t)infoPtr->src;
195 for (
unsigned long i = 0; i < nargs; i++) {
201 *(((
long *)infoPtr->dst) + i) = strtol(argv[srcIndex], &endPtr, 0);
202 if ((endPtr == argv[srcIndex]) || (*endPtr != 0)) {
203 FPRINTF(stderr,
"expected long argument for \"%s\" but got \"%s\"\n", infoPtr->key, argv[srcIndex]);
212 nargs = (uintptr_t)infoPtr->src;
215 for (
unsigned long i = 0; i < nargs; i++) {
219 *(((
const char **)infoPtr->dst) + i) = argv[srcIndex];
226 *((
int *)infoPtr->dst) = dstIndex;
229 nargs = (uintptr_t)infoPtr->src;
232 for (
unsigned long i = 0; i < nargs; i++) {
238 *(((
float *)infoPtr->dst) + i) = (
float)strtod(argv[srcIndex], &endPtr);
239 if ((endPtr == argv[srcIndex]) || (*endPtr != 0)) {
240 FPRINTF(stderr,
"expected floating-point argument for \"%s\" but got\"%s\"\n", infoPtr->key,
250 nargs = (uintptr_t)infoPtr->src;
253 for (
unsigned long i = 0; i < nargs; i++) {
259 *(((
double *)infoPtr->dst) + i) = strtod(argv[srcIndex], &endPtr);
260 if ((endPtr == argv[srcIndex]) || (*endPtr != 0)) {
261 FPRINTF(stderr,
"expected double-point argument for \"%s\" but got\"%s\"\n", infoPtr->key, argv[srcIndex]);
271 handlerProc1 = (int (*)(
const char *dst,
const char *key,
const char *argument))infoPtr->src;
273 if ((*handlerProc1)(infoPtr->dst, infoPtr->key, argv[srcIndex])) {
280 handlerProc2 = (int (*)(
const char *dst,
const char *key,
int valargc,
const char **argument))infoPtr->src;
282 argc = (*handlerProc2)(infoPtr->dst, infoPtr->key, argc, argv + srcIndex);
290 printUsage(argTable, flags);
294 FPRINTF(stderr,
"bad argument type %d in vpArgvInfo", infoPtr->type);
306 argv[dstIndex] = argv[srcIndex];
311 argv[dstIndex] = (
char *)NULL;
316 FPRINTF(stderr,
"\"%s\" option requires an additional argument\n", curArg);
336 void vpParseArgv::printUsage(vpArgvInfo *argTable,
int flags)
341 #define NUM_SPACES 20 342 static char spaces[] =
" ";
344 unsigned long long nargs;
348 if (!(flags & ARGV_NO_PRINT)) \ 357 for (
unsigned int i = 0; i < 2; i++) {
360 if (infoPtr->key == NULL) {
363 length = (int)strlen(infoPtr->key);
364 if (length > width) {
370 FPRINTF(stderr,
"Command-specific options:");
371 for (
unsigned int i = 0;; i++) {
373 if ((infoPtr->type ==
ARGV_HELP) && (infoPtr->key == NULL)) {
374 FPRINTF(stderr,
"\n%s", infoPtr->help);
377 FPRINTF(stderr,
"\n %s:", infoPtr->key);
378 numSpaces = width + 1 - (int)strlen(infoPtr->key);
379 while (numSpaces > 0) {
380 if (numSpaces >= NUM_SPACES) {
381 FPRINTF(stderr,
"%s", spaces);
383 FPRINTF(stderr,
"%s", spaces + NUM_SPACES - numSpaces);
385 numSpaces -= NUM_SPACES;
387 FPRINTF(stderr,
"%s", infoPtr->help);
388 switch (infoPtr->type) {
390 FPRINTF(stderr,
"\n\t\tDefault value:");
391 nargs = (uintptr_t)infoPtr->src;
394 for (
unsigned long j = 0; j < nargs; j++) {
395 FPRINTF(stderr,
" %d", *(((
int *)infoPtr->dst) + j));
400 FPRINTF(stderr,
"\n\t\tDefault value:");
401 nargs = (uintptr_t)infoPtr->src;
404 for (
unsigned long j = 0; j < nargs; j++) {
405 FPRINTF(stderr,
" %ld", *(((
long *)infoPtr->dst) + j));
410 FPRINTF(stderr,
"\n\t\tDefault value:");
411 nargs = (uintptr_t)infoPtr->src;
414 for (
unsigned long j = 0; j < nargs; j++) {
415 FPRINTF(stderr,
" %f", *(((
float *)infoPtr->dst) + j));
420 FPRINTF(stderr,
"\n\t\tDefault value:");
421 nargs = (uintptr_t)infoPtr->src;
424 for (
unsigned long j = 0; j < nargs; j++) {
425 FPRINTF(stderr,
" %g", *(((
double *)infoPtr->dst) + j));
432 nargs = (uintptr_t)infoPtr->src;
435 string = *((
const char **)infoPtr->dst);
436 if ((nargs == 1) && (
string == NULL))
438 for (
unsigned long j = 0; j < nargs; j++) {
439 string = *(((
const char **)infoPtr->dst) + j);
440 if (
string != NULL) {
441 FPRINTF(stderr,
" \"%s\"",
string);
464 FPRINTF(stderr,
"\nGeneric options for all commands:");
467 FPRINTF(stderr,
"\n");
502 const char *psz = NULL;
503 const char *pszParam = NULL;
506 psz = &(argv[iArg][0]);
509 chOpt = argv[iArg][1];
510 if (isalnum(chOpt) || ispunct(chOpt)) {
512 psz = strchr(validOpts, chOpt);
517 psz = &(argv[iArg][2]);
520 if (iArg + 1 < argc) {
521 psz = &(argv[iArg + 1][0]);
529 pszParam = &(argv[iArg][0]);
542 pszParam = &(argv[iArg][0]);
548 pszParam = &(argv[iArg][0]);
553 pszParam = &(argv[iArg][0]);
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
static vpArgvInfo defaultTable[2]