Skip to content

Commit f40e6a0

Browse files
committed
Added "--" to tell the parser to stop reading keys
1 parent 2c6e9db commit f40e6a0

File tree

2 files changed

+25
-39
lines changed

2 files changed

+25
-39
lines changed

argparse.c

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* IN char *** argv: array pointer
2323
*
2424
* RETURN: return code */
25-
static arg_return arg_parse_value (int * argc, char *** argv, void ** data, size_t * size) {
25+
static ARG_INLINE arg_return arg_parse_value (int * argc, char *** argv, void ** data, size_t * size) {
2626
if (!(***argv)) {
2727
if (*argc == 1)
2828
return ARG_NVALUE;
@@ -64,18 +64,13 @@ static ARG_INLINE arg_return arg_parse_call_handler (int * argc, char *** argv,
6464
return ARG_SUCCESS;
6565
}
6666

67-
/* The author is very lazy and, apparentely, also has very doubtful
68-
* knowledge of C, so, to avoid using all these fancy things like
69-
* __thread, he came up with the stupidest idea ever: "why don't I
70-
* pass the state of the parser to every single function?" */
71-
7267
/* Sub to parse long arguments
7368
* IN size_t * argc: -
7469
* IN char *** argv: pointer to the array
7570
* IN arg_list list: the list to parse from
7671
*
7772
* RETURN: Parse code */
78-
static arg_return arg_parse_long (int * argc, char *** argv, arg_list list, size_t len) {
73+
static ARG_INLINE arg_return arg_parse_long (int * argc, char *** argv, arg_list list, size_t len) {
7974
size_t i;
8075
for (i = 0; i < len; ++i) {
8176
if (ARG_STREQ(list[i].long_arg, &(**argv))) {
@@ -92,7 +87,7 @@ static arg_return arg_parse_long (int * argc, char *** argv, arg_list list, size
9287
* IN arg_list list: the list to parse from
9388
*
9489
* RETURN: Parse code */
95-
static arg_return arg_parse_short (int * argc, char *** argv, arg_list list, size_t len) {
90+
static ARG_INLINE arg_return arg_parse_short (int * argc, char *** argv, arg_list list, size_t len) {
9691

9792
arg_return code;
9893
size_t i;
@@ -146,13 +141,21 @@ char ** arg_parse (int argc, char ** argv, arg_list list, char ** not_keys, size
146141
++argv;
147142
arg_return ret_code = 0;
148143

144+
char accept_args = 1;
145+
149146
while (--argc) {
150-
if (**argv == '-') {
147+
if (**argv == '-' && accept_args) {
151148
if (*(*argv) == 0x0) {
152149
*code = ARG_UNEXP;
153150
return argv;
154151
}
155-
if (*(++*argv) == '-' && **argv) { /* If the "--" is passed we have to stop accepting the arguments */
152+
if (*(++*argv) == '-') { /* If the "--" is passed we have to stop accepting the arguments */
153+
154+
if (**argv == '-' && !*(*argv + 1)) {
155+
accept_args = 0;
156+
++argv;
157+
continue;
158+
}
156159
++(*argv);
157160
if ((ret_code = arg_parse_long (&argc, &argv, list, list_len)) != 0) {
158161
*code = ret_code;

argparse.h

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,15 @@
1919
* check whether or not the argument belongs in line
2020
* check if its long or short type
2121
* parse accordingly:
22-
* short, meaning that the data can be inserted after the argument key or after a space
23-
* long, meaning that the data can be inserted after a space or after a '=' char
24-
* The ordering plays some role */
22+
* short, meaning that the data can be inserted after a whitespace
23+
* long, meaning that the data can be inserted after a space char
24+
*
25+
* flags can be merged together, if they are flags (i.e: tar -xzvf ...) */
2526

2627

2728
#ifndef __SL_ARGPARSE_H__
2829
#define __SL_ARGPARSE_H__
2930

30-
#include <malloc.h>
31-
3231
/* A simple implementation of some important "string.h" functions */
3332
#ifdef ARG_STANDALONE
3433
# define ARG_ASSERT(x)
@@ -58,7 +57,6 @@ static void * arg_memcpy (void * dest, void * src, size_t n) {
5857
return dest;
5958
}
6059

61-
6260
#else
6361
# include <string.h>
6462
# include <assert.h>
@@ -84,9 +82,8 @@ static void * arg_memcpy (void * dest, void * src, size_t n) {
8482
#define ARG_NMATCH -6
8583
#define ARG_NVALUE -7
8684

87-
#define ARG_ORDER 0x0001
88-
89-
85+
/* If you want to make this work, you have to recompile the library
86+
* with -DARG_TRUE_EQ_ONE (makes successful returns equal to 1) */
9087
#ifndef ARG_TRUE_EQ_ONE
9188
# define ARG_SUCCESS 0
9289
#else
@@ -95,11 +92,11 @@ static void * arg_memcpy (void * dest, void * src, size_t n) {
9592

9693
#define ARG_STREQ(a, b) (arg_strcmp(a, b) == 0)
9794

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)
95+
/* A slightly altered strcmp(), will move the pointer of the second
96+
* string, but will restore it to the former state, if strings don't
97+
* match
10198
* IN char * f: first string
102-
* IN char * f: second string
99+
* IN char ** f: second string
103100
*
104101
* RETURN int:
105102
* non-zero, if the strings don't match */
@@ -118,7 +115,6 @@ static int arg_strcmp (char * f, char ** s) {
118115
return diff;
119116
}
120117

121-
typedef int arg_callback ();
122118
typedef int p_arg_handler (void * data_ptr, size_t blksize, void * retval);
123119

124120
/* The handler is the parser for the value of the argument
@@ -129,10 +125,6 @@ typedef p_arg_handler * arg_handler;
129125

130126
/* A handler for a generic string. */
131127
static p_arg_handler arg_string_handler;
132-
/* A handler for a raw data, meaning that the raw byte
133-
* data will be stored inside a pointer. Acts almost the
134-
* same way as the arg_string_handler does. */
135-
static p_arg_handler arg_raw_handler;
136128

137129
static int arg_string_handler (void * data_ptr, size_t blksize, void * retval) {
138130
if (blksize == 0) {
@@ -143,19 +135,10 @@ static int arg_string_handler (void * data_ptr, size_t blksize, void * retval) {
143135
return 0;
144136
}
145137

146-
static int arg_raw_handler (void * data_ptr, size_t blksize, void * retval) {
147-
if (blksize == 0) {
148-
retval = NULL;
149-
return 0;
150-
}
151-
ARG_MEMCPY(retval, data_ptr, blksize);
152-
return 0;
153-
}
154-
155138
/* Flags define the behaviour of the arguments parser
156139
* As example, ARG_FLAG_HALT will cause parser to be stopped when
157140
* the specified argument is met. */
158-
typedef unsigned char arg_flags;
141+
typedef unsigned char arg_flags; /* NOT IMPLEMENTED YET */
159142

160143
/* Type for the return codes */
161144
typedef signed char arg_return;
@@ -197,7 +180,7 @@ typedef struct arg_argument arg_list[];
197180
*
198181
* NOTE:
199182
* this function changes the addresses of the pointers, to save them
200-
* as they were, consider writing them into another set of variables.*/
183+
* as they were, consider writing them into another set of variables. */
201184
char ** arg_parse (int argc, char ** argv, arg_list list, char ** not_keys, size_t * not_keys_size, arg_return * return_code);
202185

203186
#endif

0 commit comments

Comments
 (0)