33 #include <visp/vpParseArgv.h>
47 {
"-help", ARGV_HELP, (
char *) NULL, (
char *) NULL,
48 "Print summary of command-line options and abort.\n"},
49 {NULL, ARGV_END, (
char *) NULL, (
char *) NULL,
53 int (*handlerProc1)(
const char *dst,
const char *key,
const char *argument);
54 int (*handlerProc2)(
const char *dst,
const char *key,
int valargc,
const char **argument);
84 register vpArgvInfo *infoPtr;
99 unsigned long long nargs;
102 #define FPRINTF if (!(flags&ARGV_NO_PRINT)) (void) fprintf
105 srcIndex = dstIndex = 0;
108 srcIndex = dstIndex = 1;
113 curArg = argv[srcIndex];
117 length = strlen(curArg);
126 for (
unsigned int i = 0; i < 2; i++) {
132 for (; infoPtr->type !=
ARGV_END; infoPtr++) {
133 if (infoPtr->key == NULL) {
136 if ((infoPtr->key[1] != c)
137 || (strncmp(infoPtr->key, curArg, length) != 0)) {
140 if (infoPtr->key[length] == 0) {
147 if (matchPtr != NULL) {
148 FPRINTF(stderr,
"ambiguous option \"%s\"\n", curArg);
154 if (matchPtr == NULL) {
162 FPRINTF(stderr,
"unrecognized argument \"%s\"\n", curArg);
164 argv[dstIndex] = curArg;
174 switch (infoPtr->type) {
176 *((
long long *) infoPtr->dst) = 1;
179 nargs = (uintptr_t) infoPtr->src;
180 if (nargs<1) nargs=1;
181 for (
unsigned long i=0; i<nargs; i++) {
187 *(((
int *) infoPtr->dst)+i) =
188 strtol(argv[srcIndex], &endPtr, 0);
189 if ((endPtr == argv[srcIndex]) || (*endPtr != 0)) {
191 "expected integer argument for \"%s\" but got \"%s\"\n",
192 infoPtr->key, argv[srcIndex]);
201 nargs = (uintptr_t) infoPtr->src;
202 if (nargs<1) nargs=1;
203 for (
unsigned long i=0; i<nargs; i++) {
209 *(((
long *) infoPtr->dst)+i) =
210 strtol(argv[srcIndex], &endPtr, 0);
211 if ((endPtr == argv[srcIndex]) || (*endPtr != 0)) {
213 "expected long argument for \"%s\" but got \"%s\"\n",
214 infoPtr->key, argv[srcIndex]);
223 nargs = (uintptr_t) infoPtr->src;
224 if (nargs<1) nargs=1;
225 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;
240 if (nargs<1) nargs=1;
241 for (
unsigned long i=0; i<nargs; i++) {
247 *(((
float *) infoPtr->dst)+i) =
248 (
float)strtod(argv[srcIndex], &endPtr);
249 if ((endPtr == argv[srcIndex]) || (*endPtr != 0)) {
251 "expected floating-point argument for \"%s\" but got\"%s\"\n",
252 infoPtr->key, argv[srcIndex]);
261 nargs = (uintptr_t) infoPtr->src;
262 if (nargs<1) nargs=1;
263 for (
unsigned long i=0; i<nargs; i++) {
269 *(((
double *) infoPtr->dst)+i) =
270 strtod(argv[srcIndex], &endPtr);
271 if ((endPtr == argv[srcIndex]) || (*endPtr != 0)) {
273 "expected double-point argument for \"%s\" but got\"%s\"\n",
274 infoPtr->key, argv[srcIndex]);
284 handlerProc1 = (int (*)(
const char *dst,
const char *key,
const char *argument))infoPtr->src;
286 if ((*handlerProc1)(infoPtr->dst, infoPtr->key, argv[srcIndex]))
294 handlerProc2 = (int (*)(
const char *dst,
const char *key,
int valargc,
const char **argument))infoPtr->src;
296 argc = (*handlerProc2)(infoPtr->dst, infoPtr->key, argc, argv+srcIndex);
304 printUsage (argTable, flags);
308 FPRINTF(stderr,
"bad argument type %d in vpArgvInfo",
321 argv[dstIndex] = argv[srcIndex];
326 argv[dstIndex] = (
char *) NULL;
331 FPRINTF(stderr,
"\"%s\" option requires an additional argument\n", curArg);
352 vpParseArgv::printUsage(vpArgvInfo * argTable,
int flags)
354 register vpArgvInfo *infoPtr;
357 #define NUM_SPACES 20
358 static char spaces[] =
" ";
360 unsigned long long nargs;
363 #define FPRINTF if (!(flags&ARGV_NO_PRINT)) (void) fprintf
371 for (
unsigned int i = 0; i < 2; i++) {
373 infoPtr->type !=
ARGV_END; infoPtr++) {
375 if (infoPtr->key == NULL) {
378 length = (int)strlen(infoPtr->key);
379 if (length > width) {
385 FPRINTF(stderr,
"Command-specific options:");
386 for (
unsigned int i = 0; ; i++) {
388 infoPtr->type !=
ARGV_END; infoPtr++) {
389 if ((infoPtr->type ==
ARGV_HELP) && (infoPtr->key == NULL)) {
390 FPRINTF(stderr,
"\n%s", infoPtr->help);
393 FPRINTF(stderr,
"\n %s:", infoPtr->key);
394 numSpaces = width + 1 - (int)strlen(infoPtr->key);
395 while (numSpaces > 0) {
396 if (numSpaces >= NUM_SPACES) {
397 FPRINTF(stderr,
"%s",spaces);
399 FPRINTF(stderr,
"%s",spaces+NUM_SPACES-numSpaces);
401 numSpaces -= NUM_SPACES;
403 FPRINTF(stderr,
"%s",infoPtr->help);
404 switch (infoPtr->type) {
406 FPRINTF(stderr,
"\n\t\tDefault value:");
407 nargs = (uintptr_t) infoPtr->src;
408 if (nargs<1) nargs=1;
409 for (
unsigned long j=0; j<nargs; j++) {
410 FPRINTF(stderr,
" %d", *(((
int *) infoPtr->dst)+j));
415 FPRINTF(stderr,
"\n\t\tDefault value:");
416 nargs = (uintptr_t) infoPtr->src;
417 if (nargs<1) nargs=1;
418 for (
unsigned long j=0; j<nargs; j++) {
419 FPRINTF(stderr,
" %ld", *(((
long *) infoPtr->dst)+j));
424 FPRINTF(stderr,
"\n\t\tDefault value:");
425 nargs = (uintptr_t) infoPtr->src;
426 if (nargs<1) nargs=1;
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;
435 if (nargs<1) nargs=1;
436 for (
unsigned long j=0; j<nargs; j++) {
437 FPRINTF(stderr,
" %g", *(((
double *) infoPtr->dst)+j));
444 nargs = (uintptr_t) infoPtr->src;
445 if (nargs<1) nargs=1;
446 string = *((
const char **) infoPtr->dst);
447 if ((nargs==1) && (
string == NULL))
break;
448 for (
unsigned long j=0; j<nargs; j++) {
449 string = *(((
const char **) infoPtr->dst)+j);
450 if (
string != NULL) {
451 FPRINTF(stderr,
" \"%s\"",
string);
472 FPRINTF(stderr,
"\nGeneric options for all commands:");
475 FPRINTF(stderr,
"\n");
512 const char* psz = NULL;
513 const char* pszParam = NULL;
516 psz = &(argv[iArg][0]);
519 chOpt = argv[iArg][1];
520 if (isalnum(chOpt) || ispunct(chOpt)) {
522 psz = strchr(validOpts, chOpt);
527 psz = &(argv[iArg][2]);
531 psz = &(argv[iArg+1][0]);
540 pszParam = &(argv[iArg][0]);
556 pszParam = &(argv[iArg][0]);
563 pszParam = &(argv[iArg][0]);
569 pszParam = &(argv[iArg][0]);
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
static vpArgvInfo defaultTable[2]