summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--arg.c12
-rw-r--r--arg.h108
-rw-r--r--test/test1.c47
3 files changed, 109 insertions, 58 deletions
diff --git a/arg.c b/arg.c
index d22825f..7a34641 100644
--- a/arg.c
+++ b/arg.c
@@ -132,6 +132,7 @@ arg_t* arg_load( int argc, char **argv, def_arg *argl )
int n = n1 > n2 ? n1 : n2;
if (strncmp(s,declared_params->vals[j],n)==0)
{
+ //PNL();
param_num += 1;
char **realoc_ptr = realloc(parse_params, sizeof(char *)*param_num);
if (realoc_ptr)
@@ -188,6 +189,7 @@ arg_t* arg_load( int argc, char **argv, def_arg *argl )
arg_list *list=NULL;
{
+ //PNL();
//hack depends on __create_arg logic
s_arg_list arg;
arg.num = param_num;
@@ -201,9 +203,11 @@ arg_t* arg_load( int argc, char **argv, def_arg *argl )
{
ENL();
}
+
argl[pos].used = 1;
+ ((s_arg_list *)argl[pos].def)->result = list;
used[i] = 1;
used[i+1] = 1;
@@ -234,12 +238,12 @@ arg_t* arg_load( int argc, char **argv, def_arg *argl )
//if (((s_arg_val*)argl[pos].def)->ptr != NULL)
// val->def = 0;
- //val->used = 1;
val->ptr = argv[i+1];
__add_arg( ret, ARGT_VAL, val );
//set that requied option allready where at cmd
argl[pos].used = 1;
+ ((s_arg_val *)argl[pos].def)->result = val;
//set used cmd arg
used[i ] = 1;
@@ -264,13 +268,17 @@ arg_t* arg_load( int argc, char **argv, def_arg *argl )
flg = __create_arg( argl[pos].type, argl[pos].def );
- //flg->used = 1;
flg->flag = 1;
__add_arg( ret, ARGT_FLAG, flg );
+
argl[pos].used = 1;
+ ((s_arg_flag *)argl[pos].def)->result = flg;
+
+ printf("result = ?\n");
+
used[i] = 1;
break;
diff --git a/arg.h b/arg.h
index e011a2a..98c4681 100644
--- a/arg.h
+++ b/arg.h
@@ -15,7 +15,7 @@
#define ARGT_VAL 6 //command line direct val
#define ARGT_FLAG 7 //flag value on/off
-#define ARG_ENTRY(P,T,V) {.param=P,.type=ARGT_##T,.def=V,.used=0}
+#define ARG_ENTRY(P,T,V,H) {.param=P,.type=ARGT_##T,.def=V,.used=0,.help=H}
/*
used to define table with predefine arguments
@@ -25,7 +25,9 @@ typedef struct def_arg
char *param; //parametr that comes from shell
uint32_t type; //type of argument
void *def; //define default values, NULL if no default values
- uint8_t used;
+ uint8_t used;
+ char *help; //command description
+ void *result; //result
} def_arg;
typedef struct argv_t
@@ -37,11 +39,20 @@ typedef struct argv_t
typedef struct arg_t
{
+ int used;
int size; //number of arguments
argv_t **arg; //list of pointers to arguments
} arg_t;
//ip argument default behaviour structure
+
+//resulting structure with ip/port values
+typedef struct arg_ip
+{
+ int used;
+ uint32_t ip; //IP4 ip addrers
+ uint16_t port;//port number
+} arg_ip;
/*
Supported ip addresses:
...1 = 127.0.0.1:[default port]
@@ -55,16 +66,20 @@ typedef struct s_arg_ip
uint32_t default_ip;
uint16_t port;
uint32_t default_port;
+ arg_ip *result;
} s_arg_ip;
-//resulting structure with ip/port values
-typedef struct arg_ip
-{
- uint32_t ip; //IP4 ip addrers
- uint16_t port;//port number
-} arg_ip;
+
//configure range argument behaviour
+//configuration structure for range argument
+typedef struct arg_range
+{
+ int used;
+ uint32_t start;
+ uint32_t end;
+ uint32_t step;
+} arg_range;
/*
number format decimal ones
1k = 1000
@@ -85,38 +100,42 @@ typedef struct s_arg_range
uint32_t default_end; //default end value
uint32_t step; //
uint32_t default_step; //default step value
+ arg_range *result;
} s_arg_range;
-//configuration structure for range argument
-typedef struct arg_range
-{
- uint32_t start;
- uint32_t end;
- uint32_t step;
-} arg_range;
+
//configuration structure for float argument
+typedef struct arg_float
+{
+ int used;
+ float val;
+} arg_float;
+
typedef struct s_arg_float
{
int used;
int def; //says if default should be used if no value in cmd
float val;
float default_val;
+ arg_float *result;
} s_arg_float;
-typedef struct arg_float
-{
- float val;
-} arg_float;
+
//configuration structure for list argument
+typedef struct arg_list
+{
+ int used;
+ uint32_t num;
+ char **vals;
+} arg_list;
/*
list arguments by name
supports
PARAM1|PARAM2|PARAM3
*/
-
typedef struct s_arg_list
{
int used;
@@ -124,17 +143,24 @@ typedef struct s_arg_list
uint32_t num;
char **vals; //list of supported arguments
char *default_val; //points to default arguments in da list
+ arg_list *result;
} s_arg_list;
-typedef struct arg_list
-{
- uint32_t num;
- char **vals;
-} arg_list;
+
/*
configuration structure to default file argument
*/
+/*
+resulting structure for file argument
+*/
+typedef struct arg_file
+{
+ int used;
+ char *name;
+ char *abspath;
+} arg_file;
+
typedef struct s_arg_file
{
int used;
@@ -143,42 +169,42 @@ typedef struct s_arg_file
char *default_name; //default filename
char *abspath; //dire where to search stuff?
char *default_abspath; //default absoulute file path
+ arg_file *result;
} s_arg_file;
-/*
-resulting structure for file argument
-*/
-typedef struct arg_file
-{
- char *name;
- char *abspath;
-} arg_file;
+typedef struct arg_val
+{
+ int used;
+ char *ptr;
+} arg_val;
+
typedef struct s_arg_val
{
int used;
int def; //says if default should be used if no value in cmd
char *ptr;
char *default_ptr;
+ arg_val *result;
} s_arg_val;
-typedef struct arg_val
+
+//no need as there is no default values
+typedef struct arg_flag
{
- char *ptr;
-} arg_val;
+ int used;
+ int flag;
+} arg_flag;
typedef struct s_arg_flag
{
int used;
int flag;
+ arg_flag *result;
} s_arg_flag;
-//no need as there is no default values
-typedef struct arg_flag
-{
- int flag;
-} arg_flag;
+
arg_t* arg_load( int argc, char **argv, def_arg *argl );
void arg_free( arg_t *arg );
diff --git a/test/test1.c b/test/test1.c
index 1b1e98e..5d9cb0c 100644
--- a/test/test1.c
+++ b/test/test1.c
@@ -7,7 +7,8 @@
s_arg_ip src_ip =
{
.port = 8080,
- .ip = 0x7f000001
+ .ip = 0x7f000001,
+ .result = NULL
};
//?
@@ -16,12 +17,14 @@ s_arg_range freq_range =
{
.start = 88,
.end = 108,
- .step = 100e3
+ .step = 100e3,
+ .result = NULL
};
s_arg_float ratio_float =
{
- .val = 0.8
+ .val = 0.8,
+ .result = NULL
};
char *color_list[] = {"RED","GREEN","BLUE"};
@@ -30,24 +33,33 @@ s_arg_list color_name_list =
{
.num = 3,
.default_val = "RED",
- .vals = color_list
+ .vals = color_list,
+ .result = NULL
};
s_arg_val number_val =
{
- .ptr = NULL
+ .ptr = NULL,
+ .default_ptr = NULL,
+ .result = NULL
+};
+
+s_arg_flag flag_help =
+{
+ .used = 0,
+ .result = NULL
};
//cant be const
def_arg cmd_arg[] =
{
- ARG_ENTRY("-a",IP,&src_ip),
- ARG_ENTRY("-b",RANGE,&freq_range),
- ARG_ENTRY("-c",FLOAT,&ratio_float),
- ARG_ENTRY("-d",LIST,&color_name_list),
- ARG_ENTRY("-e",VAL,&number_val),
- ARG_ENTRY("-f",FILE,NULL),
- ARG_ENTRY("-g",FLAG,NULL),
+ ARG_ENTRY("-a",IP, &src_ip,"test ip address argument"),
+ ARG_ENTRY("-b",RANGE,&freq_range,"test integer range argument"),
+ ARG_ENTRY("-c",FLOAT,&ratio_float,"test float number"),
+ ARG_ENTRY("-d",LIST, &color_name_list,"check list of values"),
+ ARG_ENTRY("-e",VAL, &number_val,"check number values"),
+ ARG_ENTRY("-f",FILE, NULL,"check file argument"),
+ ARG_ENTRY("-g",FLAG, &flag_help,"check flag argument"),
{NULL,0,NULL}
};
@@ -61,7 +73,7 @@ int main( int argc, char **argv )
i = 0;
while ( cmd_arg[i].param != NULL )
{
- printf("%s\n",cmd_arg[i].param);
+ printf("%s - %s\n",cmd_arg[i].param, cmd_arg[i].help);
i++;
}
@@ -70,7 +82,7 @@ int main( int argc, char **argv )
if (cfg->size == 0)
{
printf("No arguments found\n");
- goto exit_error;
+ //goto exit_error;
}
for (i=0;i<cfg->size;i++)
@@ -78,7 +90,12 @@ int main( int argc, char **argv )
printf("TYPE:%d\n", cfg->arg[i]->type);
}
- arg_print( cfg );
+ //arg_print( cfg );
+
+ if (flag_help.result != NULL)
+ {
+ printf("Help flag\n");
+ }
arg_free( cfg );