-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharg_parser.c
59 lines (47 loc) · 1007 Bytes
/
arg_parser.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <stdio.h>
#define ERROR -1
#define OPCION 1
#define VALOR 2
#define PARAMETRO 3
int check (char *key, char *value );
int imprimir (char *key, char *value, char type);
int main (int argc, char *argv[])
{
if (parse (argc, argv, check) == ERROR) return ERROR;
}
int parse ( int argc, char *argv[], int * callbk_check() )
{
int n;
int arg_type [20]={0}; // arreglo que guarda que tipo es cada parametro que se le paso
for ( n=1 ; n<argc ; n++ )
{
if ( *(argv[n][0]) != '-' )
{
arg_type [n] = PARAMETRO;
continue;
}
if ( (*argv[n+1][0] == '-') || ( n==(argc-1) ) )
{
printf("Falta un valor para el argumento %d", n);
return ERROR;
}
arg_type [n] = OPCION;
arg_type [n+1] = VALOR;
n++;
}
for ( n=1 ; n<argc ; n++ )
{
if ( *argv[n][0] == '-' )
{
if ( callbk_check( argv[n] , argv[n+1] ) )
{
printf("Opcion no valida en argumento %d", n);
return ERROR;
}
n++;
}
}
int check (char *key, char *value )
{
return 0;
}