Example VXparse Program for command line parametersThe following source code is a program that illustrates the use of VXparse to parse command line parameters.
/****************************************************************/
/* Example VisionX program that demonstrates the use of VXparse */
/* and related tools for command line argument parsing */
/* Syntax: */
/* vpex if=infile of=outfile */
/****************************************************************/
#include "VisXV4.h"
#include "Vutil.h"
/* Every VisionX program should have */
/* a "par" declaration that defines the */
/* prefixes for the command line arguments */
/* vparse will fill the second element of */
/* the structure with the address of */
/* any matched prefixes in the command */
VXparam_t par[] = { /* command line structure */
{ "if=", 0, " input file name "},
{ "of=", 0, " ouput file name "},
{ "-i", 0, " a simple flag "},
{ "n=", 0, " an integer input value "},
{ "s=", 0, " a floating point input value "},
{ "xy=", 0, " input one or two values "},
{ 0, 0, 0} /* list terminator */
};
#define IVAL par[0].val /* these defines give symbolic names */
#define OVAL par[1].val /* to the second element of the par */
#define IFLAG par[2].val /* structure. For example, the string */
#define NVAL par[3].val /* matched to "if=" (if it is found) */
#define SVAL par[4].val /* may be referred to in the program */
#define XYVAL par[5].val /* as IVAL */
extern double atof();
main(argc, argv)
int argc;
char *argv[];
{
int n;
float s;
int x, y;
VXparse(&argc, &argv, par); /* parse the command line */
if (IVAL)
(void) fprintf(stdout, "input file name is %s\n", IVAL);
if (OVAL)
(void) fprintf(stdout, "output file name is %s\n", OVAL);
if (IFLAG)
(void) fprintf(stdout, "Option -i has been selected\n");
n = 0; /* default for n */
if (NVAL) {
n = atoi(NVAL);
(void) fprintf(stdout, "n= (integer argument) is %d\n", n);
}
s = 0.0; /* default for s */
if (SVAL) {
s = (float)atof(SVAL);
(void) fprintf(stdout, "s= (float argument) is %f\n", s);
}
x = 0; /* default for x */
y = 0; /* default for y */
if (XYVAL) /* input two values */
switch (sscanf(XYVAL, "%d,%d", &x, &y)) {
case 2:
break;
case 1:
y = x;
break;
default:
(void) fprintf(stderr, "Bad input to xy=\n");
exit(1);
}
exit(0);
}
A. P. Reeves, © Copyright 1998-2012 11/12/2012 |