Fixed a little problem and added a shared and static example.
This commit is contained in:
parent
6aba00cead
commit
d322d4b51d
|
@ -3,15 +3,15 @@
|
||||||
all: build static-lib dynamic-lib example
|
all: build static-lib dynamic-lib example
|
||||||
|
|
||||||
build:
|
build:
|
||||||
gcc -c cmdlineoptions.c
|
gcc -c -g cmdlineoptions.c
|
||||||
|
|
||||||
static-lib: build
|
static-lib: build
|
||||||
ar cq libcmdlineoptions.a cmdlineoptions.o
|
ar cq libcmdlineoptions.a cmdlineoptions.o
|
||||||
|
|
||||||
|
|
||||||
dynamic-lib:
|
dynamic-lib:
|
||||||
gcc -c -fPIC -o cmdlineoptions-static.o cmdlineoptions.c
|
gcc -c -fPIC -g -o cmdlineoptions-dynamic.o cmdlineoptions.c
|
||||||
ld -G cmdlineoptions-static.o -o libcmdlineoptions.so
|
ld -G cmdlineoptions-dynamic.o -o libcmdlineoptions.so
|
||||||
|
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
|
@ -19,4 +19,5 @@ clean:
|
||||||
|
|
||||||
|
|
||||||
example:
|
example:
|
||||||
gcc -ggdb example.c -o example libcmdlineoptions.a
|
gcc -I. -g example.c -o example-shared -L. -lcmdlineoptions
|
||||||
|
gcc -g example.c -o example-static libcmdlineoptions.a
|
|
@ -5,11 +5,11 @@
|
||||||
|
|
||||||
CmdOptions cmdoptions;
|
CmdOptions cmdoptions;
|
||||||
|
|
||||||
void CmdOptions_Init(char addhelp) {
|
void CmdLO_Init(char addhelp) {
|
||||||
CmdOptions_InitCmd(addhelp, 0);
|
CmdLO_InitCmd(addhelp, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void CmdOptions_InitCmd(char addhelp, char cmdchar) {
|
void CmdLO_InitCmd(char addhelp, char cmdchar) {
|
||||||
if (cmdoptions.init == 0) {
|
if (cmdoptions.init == 0) {
|
||||||
cmdoptions.init = 1;
|
cmdoptions.init = 1;
|
||||||
if (cmdchar != 0)
|
if (cmdchar != 0)
|
||||||
|
@ -19,16 +19,16 @@ void CmdOptions_InitCmd(char addhelp, char cmdchar) {
|
||||||
// do we have to init something?
|
// do we have to init something?
|
||||||
// add the help option
|
// add the help option
|
||||||
if (addhelp != 0) {
|
if (addhelp != 0) {
|
||||||
CmdOptions_Add("help", "--help");
|
CmdLO_Add("help", "--help");
|
||||||
CmdOptions_Add("help", "-h");
|
CmdLO_Add("help", "-h");
|
||||||
CmdOptions_Add("help", "-?");
|
CmdLO_Add("help", "-?");
|
||||||
CmdOptions_AddDescription("help", "Display the help text.");
|
CmdLO_AddDescription("help", "Display the help text.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
CONode* CmdOptions_Create(char* name) {
|
CONode* CmdLO_Create(char* name) {
|
||||||
CONode* node = CmdOptions_NodeGet(name);
|
CONode* node = CmdLO_NodeGet(name);
|
||||||
if (node == 0) {
|
if (node == 0) {
|
||||||
node = malloc(sizeof(CONode));
|
node = malloc(sizeof(CONode));
|
||||||
if (cmdoptions.options == 0) {
|
if (cmdoptions.options == 0) {
|
||||||
|
@ -57,7 +57,7 @@ CONode* CmdOptions_Create(char* name) {
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
CONode* CmdOptions_NodeGet(char* name) {
|
CONode* CmdLO_NodeGet(char* name) {
|
||||||
if (cmdoptions.options != 0) {
|
if (cmdoptions.options != 0) {
|
||||||
CONode* node = cmdoptions.options;
|
CONode* node = cmdoptions.options;
|
||||||
while (node != 0) {
|
while (node != 0) {
|
||||||
|
@ -70,17 +70,14 @@ CONode* CmdOptions_NodeGet(char* name) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
CONode* CmdOptions_SearchNode(char* cmdlineargument) {
|
CONode* CmdLO_SearchNode(char* cmdlineargument) {
|
||||||
printf("searching\n");
|
|
||||||
if (cmdoptions.options != 0) {
|
if (cmdoptions.options != 0) {
|
||||||
printf("searching further\n");
|
|
||||||
CONode* node = cmdoptions.options;
|
CONode* node = cmdoptions.options;
|
||||||
while (node != 0) {
|
while (node != 0) {
|
||||||
if (node->option->optionscount > 0) {
|
if (node->option->optionscount > 0) {
|
||||||
printf("searching more! %d \n",node->option->optionscount);
|
|
||||||
int i = 0;
|
int i = 0;
|
||||||
for (; i < node->option->optionscount; ++i) {
|
for (; i < node->option->optionscount; ++i) {
|
||||||
printf("addresses 0x%x 0x%x\n",node->option->options, node->option->options[i]);
|
//printf("addresses 0x%x 0x%x\n",node->option->options, node->option->options[i]);
|
||||||
if (strcmp(node->option->options[i], cmdlineargument)
|
if (strcmp(node->option->options[i], cmdlineargument)
|
||||||
== 0) {
|
== 0) {
|
||||||
return node;
|
return node;
|
||||||
|
@ -93,7 +90,7 @@ CONode* CmdOptions_SearchNode(char* cmdlineargument) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int CmdOptions_Parse(int argc, char** argv) {
|
int CmdLO_Parse(int argc, char** argv) {
|
||||||
if (argc > 1) {
|
if (argc > 1) {
|
||||||
int i = 1;
|
int i = 1;
|
||||||
CONode* cnode = 0;
|
CONode* cnode = 0;
|
||||||
|
@ -101,8 +98,9 @@ int CmdOptions_Parse(int argc, char** argv) {
|
||||||
;
|
;
|
||||||
for (; i < argc; ++i) {
|
for (; i < argc; ++i) {
|
||||||
if (argv[i][0] == cmdoptions.cmdchar) {
|
if (argv[i][0] == cmdoptions.cmdchar) {
|
||||||
if ((cnode = CmdOptions_SearchNode(argv[i])) != 0) {
|
if ((cnode = CmdLO_SearchNode(argv[i])) != 0) {
|
||||||
cnode->option->set = 1;
|
cnode->option->set = 1;
|
||||||
|
continue;
|
||||||
} else {
|
} else {
|
||||||
carg = argv[i];
|
carg = argv[i];
|
||||||
fprintf(stderr, "CmdLineOptions: Unrecognized option '%s'.",
|
fprintf(stderr, "CmdLineOptions: Unrecognized option '%s'.",
|
||||||
|
@ -111,14 +109,14 @@ int CmdOptions_Parse(int argc, char** argv) {
|
||||||
}
|
}
|
||||||
if (cnode != 0) {
|
if (cnode != 0) {
|
||||||
if (cnode->option->possibleparametercount == 0) {
|
if (cnode->option->possibleparametercount == 0) {
|
||||||
CmdOptions_AddElement(&cnode->option->values,
|
CmdLO_AddElement(&(cnode->option->values),
|
||||||
&(cnode->option->valuecount), argv[i]);
|
&(cnode->option->valuecount), argv[i]);
|
||||||
} else {
|
} else {
|
||||||
int j = 0;
|
int j = 0;
|
||||||
for (; j < cnode->option->possibleparametercount; ++j) {
|
for (; j < cnode->option->possibleparametercount; ++j) {
|
||||||
if (strcmp(argv[i],
|
if (strcmp(argv[i],
|
||||||
cnode->option->possibleparameters[j]) == 0) {
|
cnode->option->possibleparameters[j]) == 0) {
|
||||||
CmdOptions_AddElement(&cnode->option->values,
|
CmdLO_AddElement(&(cnode->option->values),
|
||||||
&(cnode->option->valuecount), argv[i]);
|
&(cnode->option->valuecount), argv[i]);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -136,59 +134,112 @@ int CmdOptions_Parse(int argc, char** argv) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// parsing done or no arguments -> display help text
|
||||||
|
if (CmdLO_IsSet("help") > 0 || argc == 0) {
|
||||||
|
// iterate through the list
|
||||||
|
printf("-options\n");
|
||||||
|
CONode* node = cmdoptions.options;
|
||||||
|
unsigned int i = 0;
|
||||||
|
while (node != 0) {
|
||||||
|
printf("\t%s\t(", node->option->name);
|
||||||
|
for (i = 0; i < node->option->optionscount; ++i) {
|
||||||
|
printf("%s", node->option->options[i]);
|
||||||
|
if (i < node->option->optionscount - 1) {
|
||||||
|
printf(", ");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
printf("): default=[");
|
||||||
|
for (i = 0; i < node->option->defaultparametercount; ++i) {
|
||||||
|
printf("\"%s\"", node->option->defaultparameters[i]);
|
||||||
|
if (i < node->option->defaultparametercount - 1) {
|
||||||
|
printf(", ");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
printf("]\n");
|
||||||
|
printf("\t\t%s\n", node->option->description);
|
||||||
|
if (node->option->possibleparametercount > 0) {
|
||||||
|
printf("(possible parameters:");
|
||||||
|
for (i = 0; i < node->option->possibleparametercount; ++i) {
|
||||||
|
printf("\"%s\"", node->option->possibleparameters[i]);
|
||||||
|
if (i < node->option->possibleparametercount - 1) {
|
||||||
|
printf(", ");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
printf(")\n");
|
||||||
|
}
|
||||||
|
if (node->option->set > 0) {
|
||||||
|
if (node->option->valuecount > 0) {
|
||||||
|
printf("\t\t->> current value: ");
|
||||||
|
for (i = 0; i < node->option->valuecount; ++i) {
|
||||||
|
printf("\"%s\"", node->option->values[i]);
|
||||||
|
if (i < node->option->valuecount - 1) {
|
||||||
|
printf(", ");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
printf("\t\t->> is set!");
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
if (node->next != 0)
|
||||||
|
printf("\n");
|
||||||
|
node = node->next;
|
||||||
|
}
|
||||||
|
printf("/options");
|
||||||
|
}
|
||||||
// no arguments!
|
// no arguments!
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void CmdOptions_Add(char* name, char* option) {
|
void CmdLO_Add(char* name, char* option) {
|
||||||
CONode* node = CmdOptions_Create(name);
|
CONode* node = CmdLO_Create(name);
|
||||||
CmdOptions_AddElement(&node->option->options, &(node->option->optionscount),
|
CmdLO_AddElement(&(node->option->options), &(node->option->optionscount),
|
||||||
option);
|
option);
|
||||||
}
|
}
|
||||||
|
|
||||||
void CmdOptions_AddDefaultParameter(char* name, char* defaultparameter) {
|
void CmdLO_AddDefaultParameter(char* name, char* defaultparameter) {
|
||||||
CONode* node = CmdOptions_Create(name);
|
CONode* node = CmdLO_Create(name);
|
||||||
CmdOptions_AddElement(&node->option->defaultparameters,
|
CmdLO_AddElement(&(node->option->defaultparameters),
|
||||||
&(node->option->defaultparametercount), defaultparameter);
|
&(node->option->defaultparametercount), defaultparameter);
|
||||||
}
|
}
|
||||||
|
|
||||||
void CmdOptions_AddPossibleParameter(char* name, char* possibleParameter) {
|
void CmdLO_AddPossibleParameter(char* name, char* possibleParameter) {
|
||||||
CONode* node = CmdOptions_Create(name);
|
CONode* node = CmdLO_Create(name);
|
||||||
CmdOptions_AddElement(&node->option->possibleparameters,
|
CmdLO_AddElement(&(node->option->possibleparameters),
|
||||||
&(node->option->possibleparametercount), possibleParameter);
|
&(node->option->possibleparametercount), possibleParameter);
|
||||||
}
|
}
|
||||||
|
|
||||||
void CmdOptions_AddDescription(char* name, char* description) {
|
void CmdLO_AddDescription(char* name, char* description) {
|
||||||
CONode* node = CmdOptions_Create(name);
|
CONode* node = CmdLO_Create(name);
|
||||||
node->option->description = description;
|
node->option->description = description;
|
||||||
}
|
}
|
||||||
|
|
||||||
int CmdOptions_IsSet(char* name) {
|
int CmdLO_IsSet(char* name) {
|
||||||
CONode* node = CmdOptions_Create(name);
|
CONode* node = CmdLO_Create(name);
|
||||||
return node->option->set;
|
return node->option->set;
|
||||||
}
|
}
|
||||||
|
|
||||||
char* CmdOptions_Get(char* name) {
|
char* CmdLO_Get(char* name) {
|
||||||
CONode* node = CmdOptions_Create(name);
|
CONode* node = CmdLO_Create(name);
|
||||||
if (CmdOptions_IsSet(name) && node->option->valuecount > 0)
|
if (CmdLO_IsSet(name) && node->option->valuecount > 0)
|
||||||
return node->option->values[0];
|
return node->option->values[0];
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int CmdOptions_GetInt(char* name) {
|
int CmdLO_GetInt(char* name) {
|
||||||
return atoi(CmdOptions_Get(name));
|
return atoi(CmdLO_Get(name));
|
||||||
}
|
}
|
||||||
|
|
||||||
long CmdOptions_GetLong(char* name) {
|
long CmdLO_GetLong(char* name) {
|
||||||
return atol(CmdOptions_Get(name));
|
return atol(CmdLO_Get(name));
|
||||||
}
|
}
|
||||||
|
|
||||||
double CmdOptions_GetDouble(char* name) {
|
double CmdLO_GetDouble(char* name) {
|
||||||
return atof(CmdOptions_Get(name));
|
return atof(CmdLO_Get(name));
|
||||||
}
|
}
|
||||||
|
|
||||||
int CmdOptions_GetAll(char* name, char** values, unsigned int* count) {
|
int CmdLO_GetAll(char* name, char** values, unsigned int* count) {
|
||||||
CONode* node = CmdOptions_Create(name);
|
CONode* node = CmdLO_Create(name);
|
||||||
if (node == 0)
|
if (node == 0)
|
||||||
return 1;
|
return 1;
|
||||||
values = node->option->values;
|
values = node->option->values;
|
||||||
|
@ -196,15 +247,15 @@ int CmdOptions_GetAll(char* name, char** values, unsigned int* count) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void CmdOptions_AddElement(char*** target, int* counter, char* element) {
|
void CmdLO_AddElement(char*** target, unsigned int* counter, char* element) {
|
||||||
int cnt = *counter;
|
unsigned int cnt = *counter;
|
||||||
char** old = 0;
|
char** old = 0;
|
||||||
if (target != 0)
|
if (*target != 0)
|
||||||
old = *target;
|
old = *target;
|
||||||
*target = malloc((cnt + 1) * sizeof(char*));
|
*target = malloc((cnt + 1) * sizeof(char*));
|
||||||
if (old != 0)
|
if (old != 0)
|
||||||
memcpy(target, old, (cnt) * sizeof(char*));
|
memcpy(*target, old, (cnt) * sizeof(char*));
|
||||||
(*target)[cnt] = element;
|
*(*target + cnt) = element;
|
||||||
if (old != 0)
|
if (old != 0)
|
||||||
free(old);
|
free(old);
|
||||||
*counter = cnt + 1;
|
*counter = cnt + 1;
|
||||||
|
|
|
@ -36,61 +36,61 @@ extern CmdOptions cmdoptions;
|
||||||
|
|
||||||
/** Init function.
|
/** Init function.
|
||||||
*/
|
*/
|
||||||
void CmdOptions_InitCmd(char addhelp, char cmdchar);
|
void CmdLO_InitCmd(char addhelp, char cmdchar);
|
||||||
void CmdOptions_Init(char addhelp);
|
void CmdLO_Init(char addhelp);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The heart of this whole thing, the parsing of all options...
|
* The heart of this whole thing, the parsing of all options...
|
||||||
*/
|
*/
|
||||||
int CmdOptions_Parse(int argc, char** argv);
|
int CmdLO_Parse(int argc, char** argv);
|
||||||
CONode* CmdOptions_SearchNode(char* cmdlineargument);
|
CONode* CmdLO_SearchNode(char* cmdlineargument);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add a new option with a new name, or a new option alternative to an existing one.
|
* Add a new option with a new name, or a new option alternative to an existing one.
|
||||||
*/
|
*/
|
||||||
void CmdOptions_Add(char* name, char* option);
|
void CmdLO_Add(char* name, char* option);
|
||||||
/**
|
/**
|
||||||
* Add a defaultparameter to an existing option, specified by name.
|
* Add a defaultparameter to an existing option, specified by name.
|
||||||
*/
|
*/
|
||||||
void CmdOptions_AddDefaultParameter(char* name, char* defaultparameter);
|
void CmdLO_AddDefaultParameter(char* name, char* defaultparameter);
|
||||||
/**
|
/**
|
||||||
* Add a possible parameter to an existing option, specified by name.
|
* Add a possible parameter to an existing option, specified by name.
|
||||||
* Keep in mind that you LIMIT that option to all possible parameters (?)
|
* Keep in mind that you LIMIT that option to all possible parameters (?)
|
||||||
*/
|
*/
|
||||||
void CmdOptions_AddPossibleParameter(char* name, char* possibleParameter);
|
void CmdLO_AddPossibleParameter(char* name, char* possibleParameter);
|
||||||
/**
|
/**
|
||||||
* Add a description to an existing option, specified by name.
|
* Add a description to an existing option, specified by name.
|
||||||
*/
|
*/
|
||||||
void CmdOptions_AddDescription(char* name, char* description);
|
void CmdLO_AddDescription(char* name, char* description);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if the option specified by name is set.
|
* Check if the option specified by name is set.
|
||||||
*/
|
*/
|
||||||
int CmdOptions_IsSet(char* name);
|
int CmdLO_IsSet(char* name);
|
||||||
/**
|
/**
|
||||||
* Get first parameter of the option specified by name.
|
* Get first parameter of the option specified by name.
|
||||||
*/
|
*/
|
||||||
char* CmdOptions_Get(char* name);
|
char* CmdLO_Get(char* name);
|
||||||
/**
|
/**
|
||||||
* Get the first parameter of the option specified by name as integer.
|
* Get the first parameter of the option specified by name as integer.
|
||||||
*/
|
*/
|
||||||
int CmdOptions_GetInt(char* name);
|
int CmdLO_GetInt(char* name);
|
||||||
/**
|
/**
|
||||||
* Get the first parameter of the option specified by name as long.
|
* Get the first parameter of the option specified by name as long.
|
||||||
*/
|
*/
|
||||||
long CmdOptions_GetLong(char* name);
|
long CmdLO_GetLong(char* name);
|
||||||
/**
|
/**
|
||||||
* Get the first parameter of the option specified by name as double.
|
* Get the first parameter of the option specified by name as double.
|
||||||
*/
|
*/
|
||||||
double CmdOptions_GetDouble(char* name);
|
double CmdLO_GetDouble(char* name);
|
||||||
/**
|
/**
|
||||||
* Get all parameters of the given option name as string array.
|
* Get all parameters of the given option name as string array.
|
||||||
* This is the 'Do It Yourself' retrieve function.
|
* This is the 'Do It Yourself' retrieve function.
|
||||||
*/
|
*/
|
||||||
int CmdOptions_GetAll(char* name, char** values, unsigned int* count);
|
int CmdLO_GetAll(char* name, char** values, unsigned int* count);
|
||||||
|
|
||||||
CONode* CmdOptions_Create(char* name);
|
CONode* CmdLO_Create(char* name);
|
||||||
CONode* CmdOptions_NodeGet(char* name);
|
CONode* CmdLO_NodeGet(char* name);
|
||||||
void CmdOptions_AddElement(char*** target,int* counter, char* element);
|
void CmdLO_AddElement(char*** target,unsigned int* counter, char* element);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
10
c/example.c
10
c/example.c
|
@ -1,16 +1,16 @@
|
||||||
#include "cmdlineoptions.h"
|
#include "cmdlineoptions.h"
|
||||||
|
|
||||||
void configureOptions() {
|
void configureOptions() {
|
||||||
CmdOptions_Init(1);
|
CmdLO_Init(1);
|
||||||
CmdOptions_Add("test","--test");
|
CmdLO_Add("test","--test");
|
||||||
CmdOptions_Add("test","-t");
|
CmdLO_Add("test","-t");
|
||||||
CmdOptions_AddDescription("test","This is a test Option.");
|
CmdLO_AddDescription("test", "This is a test Option.");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char** argv) {
|
int main(int argc, char** argv) {
|
||||||
configureOptions();
|
configureOptions();
|
||||||
CmdOptions_Parse(argc,argv);
|
CmdLO_Parse(argc,argv);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue