G
Greg B
Well since getopt() doesn't seem to be compatible with Windows, and
the free implementation of it for Windows that I found still had some
annoying restrictions, I thought I'd whip up a simple parser myself.
Just wanted to see if anyone could provide me with some constructive
criticism any feedback would be greatly appreciated
-----------------------------------------------------------------------------
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#define MAX_OPTION_ARGS 10
enum optionName {
Start,
Stop,
Remove,
Install,
Debug,
Num_Opts
};
typedef struct optionStruct {
char *name;
int expNumVal;
char *val[MAX_OPTION_ARGS];
int valid;
} optStruct;
optStruct optData[Num_Opts] = {
{ "-start", 1 },
{ "-stop", 2 },
{ "-remove", 2 },
{ "-install", 1 },
{ "-debug", 3 }
};
int usage(void)
{
printf("Usage is as follows...");
return 0;
}
int StartService(char * service_name)
{
return 0;
}
int main (int argc, char * argv[])
{
int a, b, c;
const char *optstring;
const struct option *longopts;
int *longindex;
for (a = 1; a < argc; a++)
{
for (b = 0; b < Num_Opts; b++)
{
if (strcmp(argv[a], optData.name) == 0)
{
if (a + optData.expNumVal< argc)
{
for (c = 0; c < optData.expNumVal; c++)
{
optData.val[c] = argv[a + c + 1];
}
optData.valid = 1;
a += optData.expNumVal;
}
else
{
fprintf(stderr, "ERROR: insufficient number of arguments for
option %s\n", optData.name);
usage();
return 1;
}
break;
}
}
if (b == Num_Opts)
fprintf(stderr, "Warning: unknown option %s (ignored)\n", argv[a]);
}
/*
Sample usage...
if (optData[Start].valid == 1)
{
StartService(optData[Start].val[0]);
}
*/
return 0;
}
the free implementation of it for Windows that I found still had some
annoying restrictions, I thought I'd whip up a simple parser myself.
Just wanted to see if anyone could provide me with some constructive
criticism any feedback would be greatly appreciated
-----------------------------------------------------------------------------
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#define MAX_OPTION_ARGS 10
enum optionName {
Start,
Stop,
Remove,
Install,
Debug,
Num_Opts
};
typedef struct optionStruct {
char *name;
int expNumVal;
char *val[MAX_OPTION_ARGS];
int valid;
} optStruct;
optStruct optData[Num_Opts] = {
{ "-start", 1 },
{ "-stop", 2 },
{ "-remove", 2 },
{ "-install", 1 },
{ "-debug", 3 }
};
int usage(void)
{
printf("Usage is as follows...");
return 0;
}
int StartService(char * service_name)
{
return 0;
}
int main (int argc, char * argv[])
{
int a, b, c;
const char *optstring;
const struct option *longopts;
int *longindex;
for (a = 1; a < argc; a++)
{
for (b = 0; b < Num_Opts; b++)
{
if (strcmp(argv[a], optData.name) == 0)
{
if (a + optData.expNumVal< argc)
{
for (c = 0; c < optData.expNumVal; c++)
{
optData.val[c] = argv[a + c + 1];
}
optData.valid = 1;
a += optData.expNumVal;
}
else
{
fprintf(stderr, "ERROR: insufficient number of arguments for
option %s\n", optData.name);
usage();
return 1;
}
break;
}
}
if (b == Num_Opts)
fprintf(stderr, "Warning: unknown option %s (ignored)\n", argv[a]);
}
/*
Sample usage...
if (optData[Start].valid == 1)
{
StartService(optData[Start].val[0]);
}
*/
return 0;
}