29 #include <visp3/io/vpParseArgv.h>
43 {
"-help", ARGV_HELP, (
char *) NULL, (
char *) NULL,
44 "Print summary of command-line options and abort.\n"},
45 {NULL, ARGV_END, (
char *) NULL, (
char *) NULL,
49 int (*handlerProc1)(
const char *dst,
const char *key,
const char *argument);
50 int (*handlerProc2)(
const char *dst,
const char *key,
int valargc,
const char **argument);
95 unsigned long long nargs;
98 #define FPRINTF if (!(flags&ARGV_NO_PRINT)) (void) fprintf
101 srcIndex = dstIndex = 0;
104 srcIndex = dstIndex = 1;
109 curArg = argv[srcIndex];
113 length = strlen(curArg);
122 for (
unsigned int i = 0; i < 2; i++) {
128 for (; infoPtr->type !=
ARGV_END; infoPtr++) {
129 if (infoPtr->key == NULL) {
132 if ((infoPtr->key[1] != c)
133 || (strncmp(infoPtr->key, curArg, length) != 0)) {
136 if (infoPtr->key[length] == 0) {
143 if (matchPtr != NULL) {
144 FPRINTF(stderr,
"ambiguous option \"%s\"\n", curArg);
150 if (matchPtr == NULL) {
158 FPRINTF(stderr,
"unrecognized argument \"%s\"\n", curArg);
160 argv[dstIndex] = curArg;
170 switch (infoPtr->type) {
172 *((
int *) infoPtr->dst) = 1;
175 nargs = (uintptr_t) infoPtr->src;
176 if (nargs<1) nargs=1;
177 for (
unsigned long i=0; i<nargs; i++) {
183 *(((
int *) infoPtr->dst)+i) =
184 (
int)strtol(argv[srcIndex], &endPtr, 0);
185 if ((endPtr == argv[srcIndex]) || (*endPtr != 0)) {
187 "expected integer argument for \"%s\" but got \"%s\"\n",
188 infoPtr->key, argv[srcIndex]);
197 nargs = (uintptr_t) infoPtr->src;
198 if (nargs<1) nargs=1;
199 for (
unsigned long i=0; i<nargs; i++) {
205 *(((
long *) infoPtr->dst)+i) =
206 strtol(argv[srcIndex], &endPtr, 0);
207 if ((endPtr == argv[srcIndex]) || (*endPtr != 0)) {
209 "expected long argument for \"%s\" but got \"%s\"\n",
210 infoPtr->key, argv[srcIndex]);
219 nargs = (uintptr_t) infoPtr->src;
220 if (nargs<1) nargs=1;
221 for (
unsigned long i=0; i<nargs; i++) {
225 *(((
const char **)infoPtr->dst)+i) = argv[srcIndex];
232 *((
int *) infoPtr->dst) = dstIndex;
235 nargs = (uintptr_t) infoPtr->src;
236 if (nargs<1) nargs=1;
237 for (
unsigned long i=0; i<nargs; i++) {
243 *(((
float *) infoPtr->dst)+i) =
244 (
float)strtod(argv[srcIndex], &endPtr);
245 if ((endPtr == argv[srcIndex]) || (*endPtr != 0)) {
247 "expected floating-point argument for \"%s\" but got\"%s\"\n",
248 infoPtr->key, argv[srcIndex]);
257 nargs = (uintptr_t) infoPtr->src;
258 if (nargs<1) nargs=1;
259 for (
unsigned long i=0; i<nargs; i++) {
265 *(((
double *) infoPtr->dst)+i) =
266 strtod(argv[srcIndex], &endPtr);
267 if ((endPtr == argv[srcIndex]) || (*endPtr != 0)) {
269 "expected double-point argument for \"%s\" but got\"%s\"\n",
270 infoPtr->key, argv[srcIndex]);
280 handlerProc1 = (int (*)(
const char *dst,
const char *key,
const char *argument))infoPtr->src;
282 if ((*handlerProc1)(infoPtr->dst, infoPtr->key, argv[srcIndex]))
290 handlerProc2 = (int (*)(
const char *dst,
const char *key,
int valargc,
const char **argument))infoPtr->src;
292 argc = (*handlerProc2)(infoPtr->dst, infoPtr->key, argc, argv+srcIndex);
300 printUsage (argTable, flags);
304 FPRINTF(stderr,
"bad argument type %d in vpArgvInfo",
317 argv[dstIndex] = argv[srcIndex];
322 argv[dstIndex] = (
char *) NULL;
327 FPRINTF(stderr,
"\"%s\" option requires an additional argument\n", curArg);
348 vpParseArgv::printUsage(vpArgvInfo * argTable,
int flags)
353 #define NUM_SPACES 20
354 static char spaces[] =
" ";
356 unsigned long long nargs;
359 #define FPRINTF if (!(flags&ARGV_NO_PRINT)) (void) fprintf
367 for (
unsigned int i = 0; i < 2; i++) {
369 infoPtr->type !=
ARGV_END; infoPtr++) {
371 if (infoPtr->key == NULL) {
374 length = (int)strlen(infoPtr->key);
375 if (length > width) {
381 FPRINTF(stderr,
"Command-specific options:");
382 for (
unsigned int i = 0; ; i++) {
384 infoPtr->type !=
ARGV_END; infoPtr++) {
385 if ((infoPtr->type ==
ARGV_HELP) && (infoPtr->key == NULL)) {
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);
395 FPRINTF(stderr,
"%s",spaces+NUM_SPACES-numSpaces);
397 numSpaces -= NUM_SPACES;
399 FPRINTF(stderr,
"%s",infoPtr->help);
400 switch (infoPtr->type) {
402 FPRINTF(stderr,
"\n\t\tDefault value:");
403 nargs = (uintptr_t) infoPtr->src;
404 if (nargs<1) nargs=1;
405 for (
unsigned long j=0; j<nargs; j++) {
406 FPRINTF(stderr,
" %d", *(((
int *) infoPtr->dst)+j));
411 FPRINTF(stderr,
"\n\t\tDefault value:");
412 nargs = (uintptr_t) infoPtr->src;
413 if (nargs<1) nargs=1;
414 for (
unsigned long j=0; j<nargs; j++) {
415 FPRINTF(stderr,
" %ld", *(((
long *) infoPtr->dst)+j));
420 FPRINTF(stderr,
"\n\t\tDefault value:");
421 nargs = (uintptr_t) infoPtr->src;
422 if (nargs<1) nargs=1;
423 for (
unsigned long j=0; j<nargs; j++) {
424 FPRINTF(stderr,
" %f", *(((
float *) infoPtr->dst)+j));
429 FPRINTF(stderr,
"\n\t\tDefault value:");
430 nargs = (uintptr_t) infoPtr->src;
431 if (nargs<1) nargs=1;
432 for (
unsigned long j=0; j<nargs; j++) {
433 FPRINTF(stderr,
" %g", *(((
double *) infoPtr->dst)+j));
440 nargs = (uintptr_t) infoPtr->src;
441 if (nargs<1) nargs=1;
442 string = *((
const char **) infoPtr->dst);
443 if ((nargs==1) && (
string == NULL))
break;
444 for (
unsigned long j=0; j<nargs; j++) {
445 string = *(((
const char **) infoPtr->dst)+j);
446 if (
string != NULL) {
447 FPRINTF(stderr,
" \"%s\"",
string);
468 FPRINTF(stderr,
"\nGeneric options for all commands:");
471 FPRINTF(stderr,
"\n");
508 const char* psz = NULL;
509 const char* pszParam = NULL;
512 psz = &(argv[iArg][0]);
515 chOpt = argv[iArg][1];
516 if (isalnum(chOpt) || ispunct(chOpt)) {
518 psz = strchr(validOpts, chOpt);
523 psz = &(argv[iArg][2]);
527 psz = &(argv[iArg+1][0]);
536 pszParam = &(argv[iArg][0]);
552 pszParam = &(argv[iArg][0]);
559 pszParam = &(argv[iArg][0]);
565 pszParam = &(argv[iArg][0]);
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
static vpArgvInfo defaultTable[2]