Skip to content

Commit ccaee08

Browse files
committed
Added the long arguments
Fixed all of the previous issues, added the implementation for the long arguments
1 parent 537f05a commit ccaee08

File tree

2 files changed

+36
-30
lines changed

2 files changed

+36
-30
lines changed

argparse.c

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,12 @@ static ARG_INLINE arg_return arg_parse_call_handler (int * argc, char *** argv,
7676
static arg_return arg_parse_long (int * argc, char *** argv, arg_list list, size_t len) {
7777
size_t i;
7878
for (i = 0; i < len; ++i) {
79-
if (ARG_STREQ(list[i].long_arg, **argv)) {
80-
81-
arg_parse_call_handler (argc, argv, &list[i]);
79+
if (ARG_STREQ(list[i].long_arg, &(**argv))) {
80+
81+
return arg_parse_call_handler (argc, argv, &list[i]);
8282
}
8383
}
84-
return ARG_SUCCESS;
84+
return ARG_NMATCH;
8585
}
8686

8787
/* Sub to parse short arguments
@@ -154,23 +154,24 @@ char ** arg_parse (int argc, char ** argv, arg_list list, arg_return * code) {
154154
arg_return ret_code = 0;
155155
while (--argc) {
156156
if (**argv == '-') {
157+
if (*(*argv) == 0x0) {
158+
*code = ARG_UNEXP;
159+
return argv;
160+
}
157161
if (*(++*argv) == '-' && **argv) { /* If the "--" is passed we have to stop accepting the arguments */
162+
++(*argv);
158163
if ((ret_code = arg_parse_long (&argc, &argv, list, list_len)) != 0) {
159164
*code = ret_code;
160165
return argv;
161166
}
162-
}
163167

164-
if (*(*argv) == 0x0) {
165-
*code = ARG_UNEXP;
166-
return argv;
168+
continue;
167169
}
170+
168171
if ((ret_code = arg_parse_short_a (&argc, &argv, list, list_len)) != 0) {
169172
*code = ret_code;
170173
return argv;
171-
}
172-
if (argc != 1)
173-
++argv;
174+
} else continue;
174175

175176
/* Not a key */
176177
arg_parse_non (&argc, &argv, list, list_len);

argparse.h

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
/* A simple implementation of some important "string.h" functions */
3333
#ifdef ARG_STANDALONE
3434
# define ARG_ASSERT(x)
35-
# define ARG_STRCMP(a, b) arg_strcmp(a, b)
3635
# define ARG_STRLEN(str) arg_strlen(str)
3736
# define ARG_STRCPY(dest, src) arg_strcpy(dest, src)
3837
# define ARG_MEMCPY(dest, src, n) arg_memcpy(dest, src, n)
@@ -59,28 +58,11 @@ static void * arg_memcpy (void * dest, void * src, size_t n) {
5958
return dest;
6059
}
6160

62-
/* A slightly altered strcmp(), will return 0 even if the strings
63-
* don't match after the first one ended (i.e: abc = abcde, but
64-
* abc != abdc)
65-
* IN char * f: first string
66-
* IN char * f: second string
67-
*
68-
* RETURN int:
69-
* non-zero, if the strings don't match */
70-
static int arg_strcmp (char * f, char * s) {
71-
while (*f && *s) {
72-
if (!(*f == *s))
73-
break;
74-
++f; ++s;
75-
}
76-
return *(unsigned char *)f - *(unsigned char *)s;
77-
}
7861

7962
#else
8063
# include <string.h>
8164
# include <assert.h>
8265
# define ARG_ASSERT(x) assert(x)
83-
# define ARG_STRCMP(a, b) strcmp(a, b)
8466
# define ARG_STRLEN(str) strlen(str)
8567
# define ARG_STRCPY(dest, src) strcpy(dest, src)
8668
# define ARG_MEMCPY(dest, src, n) memcpy(dest, src, n)
@@ -111,7 +93,30 @@ static int arg_strcmp (char * f, char * s) {
11193
# define ARG_SUCCESS 1
11294
#endif
11395

114-
#define ARG_STREQ(a, b) (ARG_STRCMP(a, b) == 0)
96+
#define ARG_STREQ(a, b) (arg_strcmp(a, b) == 0)
97+
98+
/* A slightly altered strcmp(), will return 0 even if the strings
99+
* don't match after the first one ended (i.e: abc = abcde, but
100+
* abc != abdc)
101+
* IN char * f: first string
102+
* IN char * f: second string
103+
*
104+
* RETURN int:
105+
* non-zero, if the strings don't match */
106+
static int arg_strcmp (char * f, char ** s) {
107+
char * rest_ptr = *s;
108+
while (*f && **s) {
109+
if (!(*f == **s)) {
110+
break;
111+
}
112+
++f; ++(*s);
113+
}
114+
int diff;
115+
if ((diff = *(unsigned char *)f - *(unsigned char *)*s) != 0)
116+
*s = rest_ptr;
117+
118+
return diff;
119+
}
115120

116121
typedef int arg_callback ();
117122
typedef int p_arg_handler (void * data_ptr, size_t blksize, void * retval);

0 commit comments

Comments
 (0)