working on parser and added example

This commit is contained in:
Marcel M. Otte 2013-12-27 18:59:33 +00:00
parent 67ebb18bc8
commit 0119e8dfb0
4 changed files with 107 additions and 13 deletions

View File

@ -1,6 +1,6 @@
all: build static-lib dynamic-lib all: build static-lib dynamic-lib example
build: build:
gcc -c cmdlineoptions.c gcc -c cmdlineoptions.c
@ -15,4 +15,8 @@ dynamic-lib:
clean: clean:
rm -f *.o *.a *.so rm -f *.o *.a *.so example
example:
gcc -ggdb example.c -o example libcmdlineoptions.a

View File

@ -1,20 +1,31 @@
#include "cmdlineoptions.h" #include "cmdlineoptions.h"
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h>
CmdOptions cmdoptions; CmdOptions cmdoptions;
void CmdOptions_Init() { void CmdOptions_Init(char addhelp){
CmdOptions_InitCmd(addhelp,0);
}
void CmdOptions_InitCmd(char addhelp, char cmdchar) {
if (cmdoptions.init == 0) { if (cmdoptions.init == 0) {
cmdoptions.init = 1; cmdoptions.init = 1;
if (cmdchar != 0)
cmdoptions.cmdchar = cmdchar;
else
cmdoptions.cmdchar = '-';
// do we have to init something? // do we have to init something?
// add the help option // add the help option
if (addhelp != 0) {
CmdOptions_Add("help", "--help"); CmdOptions_Add("help", "--help");
CmdOptions_Add("help", "-h"); CmdOptions_Add("help", "-h");
CmdOptions_Add("help", "-?"); CmdOptions_Add("help", "-?");
CmdOptions_AddDescription("help", "Display the help text."); CmdOptions_AddDescription("help", "Display the help text.");
} }
} }
}
CONode* CmdOptions_Create(char* name) { CONode* CmdOptions_Create(char* name) {
CONode* node = CmdOptions_NodeGet(name); CONode* node = CmdOptions_NodeGet(name);
@ -59,8 +70,67 @@ CONode* CmdOptions_NodeGet(char* name) {
return 0; return 0;
} }
int CmdOptions_Parse(int argc, char** argv) { CONode* CmdOptions_SearchNode(char* cmdlineargument) {
if (cmdoptions.options != 0) {
CONode* node = cmdoptions.options;
while (node != 0) {
int i = 0;
for (; i < node->option->optionscount; ++i) {
if (strcmp(node->option->options[i], cmdlineargument) == 0) {
return node;
}
}
node = node->next;
}
}
return 0;
}
int CmdOptions_Parse(int argc, char** argv) {
if (argc > 1) {
int i = 1;
CONode* cnode = 0;
char* carg = 0;
;
for (; i < argc; ++i) {
if (argv[i][0] == cmdoptions.cmdchar) {
if ((cnode = CmdOptions_SearchNode(argv[i])) != 0) {
cnode->option->set = 1;
} else {
carg = argv[i];
fprintf(stderr, "CmdLineOptions: Unrecognized option '%s'.",
carg);
}
}
if (cnode != 0) {
if (cnode->option->possibleparametercount == 0) {
CmdOptions_AddElement(cnode->option->values,
&(cnode->option->valuecount), argv[i]);
} else {
int j = 0;
for (; j < cnode->option->possibleparametercount; ++j) {
if (strcmp(argv[i],
cnode->option->possibleparameters[j]) == 0) {
CmdOptions_AddElement(cnode->option->values,
&(cnode->option->valuecount), argv[i]);
break;
}
}
if (cnode->option->possibleparametercount == j) {
fprintf(stderr,
"CmdLineOptions: Parameter '%s' is not allowed, parameter not added.",
argv[i]);
}
}
} else {
fprintf(stderr,
"CmdLineOptions: Unrecognized option parameter '%s' for unrecognized option '%s'.",
argv[i], carg);
}
}
}
// no arguments!
return 1;
} }
void CmdOptions_Add(char* name, char* option) { void CmdOptions_Add(char* name, char* option) {
@ -107,7 +177,7 @@ long CmdOptions_GetLong(char* name) {
} }
double CmdOptions_GetDouble(char* name) { double CmdOptions_GetDouble(char* name) {
return atod(CmdOptions_Get(name)); return atof(CmdOptions_Get(name));
} }
int CmdOptions_GetAll(char* name, char** values, unsigned int* count) { int CmdOptions_GetAll(char* name, char** values, unsigned int* count) {

View File

@ -26,21 +26,24 @@ typedef struct _CONode {
typedef struct _CmdOptions { typedef struct _CmdOptions {
char init; char init;
CONode* options; CONode* options;
char cmdchar;
} CmdOptions; } CmdOptions;
/* /**
* Main structure * Main structure
*/ */
extern CmdOptions cmdoptions; extern CmdOptions cmdoptions;
/** Init function dummy. /** Init function.
*/ */
void CmdOptions_Init(); void CmdOptions_InitCmd(char addhelp, char cmdchar);
void CmdOptions_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 CmdOptions_Parse(int argc, char** argv);
CONode* CmdOptions_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.

17
c/example.c Normal file
View File

@ -0,0 +1,17 @@
#include "cmdlineoptions.h"
void configureOptions() {
CmdOptions_Init(1);
CmdOptions_Add("test","--test");
CmdOptions_Add("test","-t");
CmdOptions_AddDescription("test","This is a test Option.");
}
int main(int argc, char** argv) {
configureOptions();
CmdOptions_Parse(argc,argv);
}