33 #include <visp/vpParseArgv.h>
46 {
"-help", ARGV_HELP, (
char *) NULL, (
char *) NULL,
47 "Print summary of command-line options and abort.\n"},
48 {NULL, ARGV_END, (
char *) NULL, (
char *) NULL,
52 int (*handlerProc1)(
const char *dst,
const char *key,
const char *argument);
53 int (*handlerProc2)(
const char *dst,
const char *key,
int valargc,
const char **argument);
83 register vpArgvInfo *infoPtr;
101 #define FPRINTF if (!(flags&ARGV_NO_PRINT)) (void) fprintf
104 srcIndex = dstIndex = 0;
107 srcIndex = dstIndex = 1;
112 curArg = argv[srcIndex];
116 length = strlen(curArg);
125 for (
unsigned int i = 0; i < 2; i++) {
131 for (; infoPtr->type !=
ARGV_END; infoPtr++) {
132 if (infoPtr->key == NULL) {
135 if ((infoPtr->key[1] != c)
136 || (strncmp(infoPtr->key, curArg, length) != 0)) {
139 if (infoPtr->key[length] == 0) {
146 if (matchPtr != NULL) {
147 FPRINTF(stderr,
"ambiguous option \"%s\"\n", curArg);
153 if (matchPtr == NULL) {
161 FPRINTF(stderr,
"unrecognized argument \"%s\"\n", curArg);
163 argv[dstIndex] = curArg;
173 switch (infoPtr->type) {
175 *((
int *) infoPtr->dst) = (long) infoPtr->src;
178 nargs = (
unsigned long) infoPtr->src;
179 if (nargs<1) nargs=1;
180 for (
unsigned long i=0; i<nargs; i++) {
186 *(((
int *) infoPtr->dst)+i) =
187 strtol(argv[srcIndex], &endPtr, 0);
188 if ((endPtr == argv[srcIndex]) || (*endPtr != 0)) {
190 "expected integer argument for \"%s\" but got \"%s\"\n",
191 infoPtr->key, argv[srcIndex]);
200 nargs = (
unsigned long) infoPtr->src;
201 if (nargs<1) nargs=1;
202 for (
unsigned long i=0; i<nargs; i++) {
208 *(((
long *) infoPtr->dst)+i) =
209 strtol(argv[srcIndex], &endPtr, 0);
210 if ((endPtr == argv[srcIndex]) || (*endPtr != 0)) {
212 "expected long argument for \"%s\" but got \"%s\"\n",
213 infoPtr->key, argv[srcIndex]);
222 nargs = (
unsigned long) infoPtr->src;
223 if (nargs<1) nargs=1;
224 for (
unsigned long i=0; i<nargs; i++) {
228 *(((
const char **)infoPtr->dst)+i) = argv[srcIndex];
235 *((
int *) infoPtr->dst) = dstIndex;
238 nargs = (
unsigned long) infoPtr->src;
239 if (nargs<1) nargs=1;
240 for (
unsigned long i=0; i<nargs; i++) {
246 *(((
float *) infoPtr->dst)+i) =
247 (
float)strtod(argv[srcIndex], &endPtr);
248 if ((endPtr == argv[srcIndex]) || (*endPtr != 0)) {
250 "expected floating-point argument for \"%s\" but got\"%s\"\n",
251 infoPtr->key, argv[srcIndex]);
260 nargs = (
unsigned long) infoPtr->src;
261 if (nargs<1) nargs=1;
262 for (
unsigned long i=0; i<nargs; i++) {
268 *(((
double *) infoPtr->dst)+i) =
269 strtod(argv[srcIndex], &endPtr);
270 if ((endPtr == argv[srcIndex]) || (*endPtr != 0)) {
272 "expected double-point argument for \"%s\" but got\"%s\"\n",
273 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]))
293 handlerProc2 = (int (*)(
const char *dst,
const char *key,
int valargc,
const char **argument))infoPtr->src;
295 argc = (*handlerProc2)(infoPtr->dst, infoPtr->key, argc, argv+srcIndex);
303 printUsage (argTable, flags);
307 FPRINTF(stderr,
"bad argument type %d in vpArgvInfo",
320 argv[dstIndex] = argv[srcIndex];
325 argv[dstIndex] = (
char *) NULL;
330 FPRINTF(stderr,
"\"%s\" option requires an additional argument\n", curArg);
351 vpParseArgv::printUsage(vpArgvInfo * argTable,
int flags)
353 register vpArgvInfo *infoPtr;
356 #define NUM_SPACES 20
357 static char spaces[] =
" ";
362 #define FPRINTF if (!(flags&ARGV_NO_PRINT)) (void) fprintf
370 for (
unsigned int i = 0; i < 2; i++) {
372 infoPtr->type !=
ARGV_END; infoPtr++) {
374 if (infoPtr->key == NULL) {
377 length = (int)strlen(infoPtr->key);
378 if (length > width) {
384 FPRINTF(stderr,
"Command-specific options:");
385 for (
unsigned int i = 0; ; i++) {
387 infoPtr->type !=
ARGV_END; infoPtr++) {
388 if ((infoPtr->type ==
ARGV_HELP) && (infoPtr->key == NULL)) {
389 FPRINTF(stderr,
"\n%s", infoPtr->help);
392 FPRINTF(stderr,
"\n %s:", infoPtr->key);
393 numSpaces = width + 1 - (int)strlen(infoPtr->key);
394 while (numSpaces > 0) {
395 if (numSpaces >= NUM_SPACES) {
396 FPRINTF(stderr,
"%s",spaces);
398 FPRINTF(stderr,
"%s",spaces+NUM_SPACES-numSpaces);
400 numSpaces -= NUM_SPACES;
402 FPRINTF(stderr,
"%s",infoPtr->help);
403 switch (infoPtr->type) {
405 FPRINTF(stderr,
"\n\t\tDefault value:");
406 nargs = (
unsigned long) infoPtr->src;
407 if (nargs<1) nargs=1;
408 for (
unsigned long j=0; j<nargs; j++) {
409 FPRINTF(stderr,
" %d", *(((
int *) infoPtr->dst)+j));
414 FPRINTF(stderr,
"\n\t\tDefault value:");
415 nargs = (
unsigned long) infoPtr->src;
416 if (nargs<1) nargs=1;
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 = (
unsigned long) infoPtr->src;
425 if (nargs<1) nargs=1;
426 for (
unsigned long j=0; j<nargs; j++) {
427 FPRINTF(stderr,
" %f", *(((
float *) infoPtr->dst)+j));
432 FPRINTF(stderr,
"\n\t\tDefault value:");
433 nargs = (
unsigned long) infoPtr->src;
434 if (nargs<1) nargs=1;
435 for (
unsigned long j=0; j<nargs; j++) {
436 FPRINTF(stderr,
" %g", *(((
double *) infoPtr->dst)+j));
443 nargs = (
unsigned long) infoPtr->src;
444 if (nargs<1) nargs=1;
445 string = *((
const char **) infoPtr->dst);
446 if ((nargs==1) && (
string == NULL))
break;
447 for (
unsigned long j=0; j<nargs; j++) {
448 string = *(((
const char **) infoPtr->dst)+j);
449 if (
string != NULL) {
450 FPRINTF(stderr,
" \"%s\"",
string);
453 FPRINTF(stderr,
" \"%s\"",
string);
474 FPRINTF(stderr,
"\nGeneric options for all commands:");
477 FPRINTF(stderr,
"\n");
514 const char* psz = NULL;
515 const char* pszParam = NULL;
518 psz = &(argv[iArg][0]);
521 chOpt = argv[iArg][1];
522 if (isalnum(chOpt) || ispunct(chOpt)) {
524 psz = strchr(validOpts, chOpt);
529 psz = &(argv[iArg][2]);
533 psz = &(argv[iArg+1][0]);
542 pszParam = &(argv[iArg][0]);
558 pszParam = &(argv[iArg][0]);
565 pszParam = &(argv[iArg][0]);
571 pszParam = &(argv[iArg][0]);
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
static vpArgvInfo defaultTable[2]