home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Micro R&D 1
/
MicroRD-CD-ROM-Vol1-1994.iso
/
disktools
/
moni
/
bb20.lzh
/
BB
/
getoption.c
< prev
next >
Wrap
C/C++ Source or Header
|
1992-01-18
|
1KB
|
70 lines
/*
* getoption.c - command line argument parsing aid
*
* Bruno Costa & Lucia Darsa - 20 May 90 - 20 May 90
*/
#include <string.h>
int getoption (int argc, char *argv[], char *argopts, char *noargopts, char *data)
{
static int argn = 1, i = 0;
char c;
if (argn >= argc)
return 0;
if (i == 0)
if (argv[argn][0] != '-')
{
strcpy (data, argv[argn++]);
return 1;
}
else
++i;
c = argv[argn][i++];
if (strchr (argopts, c))
{
if (argv[argn][i] == '\0')
strcpy (data, argv[++argn]);
else
strcpy (data, &argv[argn][i]);
++argn;
i = 0;
}
else if (strchr (noargopts, c))
{
strcpy (data, "");
if (argv[argn][i] == '\0')
{
++argn;
i = 0;
}
}
else
return -c;
return (c);
}
#ifdef TEST
#include <stdio.h>
void main (int argc, char *argv[])
{
char data[100];
int c;
while (c = getoption (argc, argv, "xyz", "abc", data))
if (c < 0)
printf ("unknown option %c\n", -c);
else if (c == 1)
printf ("argument = %s\n", data);
else
printf ("option = %c, data = %s\n", c, data);
}
#endif