Skip to content

Commit 0251162

Browse files
committed
[docopt_c/__main__.py] Fix output name handling; [docopt_c/_data/template{.c,.h}] size_t -> int; [docopt_c/_data/template.h] inline stdbool implementation; [docopt_c/_data/template.c] Use EXIT_FAILURE and EXIT_SUCCESS over 0 and 1
1 parent f1ffe6f commit 0251162

File tree

4 files changed

+56
-53
lines changed

4 files changed

+56
-53
lines changed

docopt_c/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# -*- coding: utf-8 -*-
22

33
__author__ = "Vladimir Keleshev"
4-
__version__ = "0.0.2"
4+
__version__ = "0.0.3"
55
__description__ = "C generator for language for description of command-line interfaces"

docopt_c/__main__.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,13 @@ def main():
141141

142142
if not args['--output-name']:
143143
header_output_name = '<stdout>'
144-
elif args['--output-name'].endswith('.c'):
145-
header_output_name = os.path.splitext(args['--output-name'])[0] + '.h'
146144
else:
147-
args['--output-name'] = args['--output-name'] + '.h'
148-
header_output_name = args['--output-name'] + '.c'
145+
base, ext = os.path.splitext(args['--output-name'])
146+
if ext not in frozenset(('.h', '.c')):
147+
base = args['--output-name']
148+
149+
args['--output-name'] = "{base}.c".format(base=base)
150+
header_output_name = "{base}.h".format(base=base)
149151

150152
header_name = os.path.basename(header_output_name)
151153

docopt_c/_data/template.c

Lines changed: 35 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,7 @@
11
#include <stdio.h>
22
#include <stdlib.h>
3-
#include <stddef.h>
43
#include <string.h>
54

6-
#if defined(__STDC__) && defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
7-
8-
#include <stdbool.h>
9-
10-
#else
11-
12-
#include "stdbool.h"
13-
14-
#endif
15-
165
#include "$header_name"
176

187
struct Command {
@@ -35,9 +24,9 @@ struct Option {
3524
};
3625

3726
struct Elements {
38-
size_t n_commands;
39-
size_t n_arguments;
40-
size_t n_options;
27+
int n_commands;
28+
int n_arguments;
29+
int n_options;
4130
struct Command *commands;
4231
struct Argument *arguments;
4332
struct Option *options;
@@ -49,16 +38,16 @@ struct Elements {
4938
*/
5039

5140
struct Tokens {
52-
size_t argc;
41+
int argc;
5342
char **argv;
54-
size_t i;
43+
int i;
5544
char *current;
5645
};
5746

5847
const char usage_pattern[] =
5948
$usage_pattern;
6049

61-
struct Tokens tokens_new(size_t argc, char **argv) {
50+
struct Tokens tokens_new(int argc, char **argv) {
6251
struct Tokens ts;
6352
ts.argc = argc;
6453
ts.argv = argv;
@@ -82,10 +71,10 @@ struct Tokens *tokens_move(struct Tokens *ts) {
8271
* ARGV parsing functions
8372
*/
8473

85-
size_t parse_doubledash(struct Tokens *ts, struct Elements *elements) {
74+
int parse_doubledash(struct Tokens *ts, struct Elements *elements) {
8675
/*
87-
size_t n_commands = elements->n_commands;
88-
size_t n_arguments = elements->n_arguments;
76+
int n_commands = elements->n_commands;
77+
int n_arguments = elements->n_arguments;
8978
Command *commands = elements->commands;
9079
Argument *arguments = elements->arguments;
9180
@@ -95,10 +84,10 @@ size_t parse_doubledash(struct Tokens *ts, struct Elements *elements) {
9584
return 0;
9685
}
9786

98-
size_t parse_long(struct Tokens *ts, struct Elements *elements) {
99-
size_t i;
100-
size_t len_prefix;
101-
size_t n_options = elements->n_options;
87+
int parse_long(struct Tokens *ts, struct Elements *elements) {
88+
int i;
89+
int len_prefix;
90+
int n_options = elements->n_options;
10291
char *eq = strchr(ts->current, '=');
10392
struct Option *option;
10493
struct Option *options = elements->options;
@@ -136,10 +125,10 @@ size_t parse_long(struct Tokens *ts, struct Elements *elements) {
136125
return 0;
137126
}
138127

139-
size_t parse_shorts(struct Tokens *ts, struct Elements *elements) {
128+
int parse_shorts(struct Tokens *ts, struct Elements *elements) {
140129
char *raw;
141-
size_t i;
142-
size_t n_options = elements->n_options;
130+
int i;
131+
int n_options = elements->n_options;
143132
struct Option *option;
144133
struct Option *options = elements->options;
145134

@@ -154,7 +143,7 @@ size_t parse_shorts(struct Tokens *ts, struct Elements *elements) {
154143
if (i == n_options) {
155144
/* TODO -%s is specified ambiguously %d times */
156145
fprintf(stderr, "-%c is not recognized\n", raw[0]);
157-
return 1;
146+
return EXIT_FAILURE;
158147
}
159148
raw++;
160149
if (!option->argcount) {
@@ -163,7 +152,7 @@ size_t parse_shorts(struct Tokens *ts, struct Elements *elements) {
163152
if (raw[0] == '\0') {
164153
if (ts->current == NULL) {
165154
fprintf(stderr, "%s requires argument\n", option->oshort);
166-
return 1;
155+
return EXIT_FAILURE;
167156
}
168157
raw = ts->current;
169158
tokens_move(ts);
@@ -172,13 +161,13 @@ size_t parse_shorts(struct Tokens *ts, struct Elements *elements) {
172161
break;
173162
}
174163
}
175-
return 0;
164+
return EXIT_SUCCESS;
176165
}
177166

178-
size_t parse_argcmd(struct Tokens *ts, struct Elements *elements) {
179-
size_t i;
180-
size_t n_commands = elements->n_commands;
181-
/* size_t n_arguments = elements->n_arguments; */
167+
int parse_argcmd(struct Tokens *ts, struct Elements *elements) {
168+
int i;
169+
int n_commands = elements->n_commands;
170+
/* int n_arguments = elements->n_arguments; */
182171
struct Command *command;
183172
struct Command *commands = elements->commands;
184173
/* Argument *arguments = elements->arguments; */
@@ -188,7 +177,7 @@ size_t parse_argcmd(struct Tokens *ts, struct Elements *elements) {
188177
if (strcmp(command->name, ts->current) == 0) {
189178
command->value = true;
190179
tokens_move(ts);
191-
return 0;
180+
return EXIT_SUCCESS;
192181
}
193182
}
194183
/* not implemented yet, just skip for now
@@ -201,16 +190,16 @@ size_t parse_argcmd(struct Tokens *ts, struct Elements *elements) {
201190
fprintf(stderr, "'\n");
202191
*/
203192
tokens_move(ts);
204-
return 0;
193+
return EXIT_SUCCESS;
205194
}
206195

207-
size_t parse_args(struct Tokens *ts, struct Elements *elements) {
208-
size_t ret;
196+
int parse_args(struct Tokens *ts, struct Elements *elements) {
197+
int ret = EXIT_FAILURE;
209198

210199
while (ts->current != NULL) {
211200
if (strcmp(ts->current, "--") == 0) {
212201
ret = parse_doubledash(ts, elements);
213-
if (!ret) break;
202+
if (ret == EXIT_FAILURE) break;
214203
} else if (ts->current[0] == '-' && ts->current[1] == '-') {
215204
ret = parse_long(ts, elements);
216205
} else if (ts->current[0] == '-' && ts->current[1] != '\0') {
@@ -219,15 +208,15 @@ size_t parse_args(struct Tokens *ts, struct Elements *elements) {
219208
ret = parse_argcmd(ts, elements);
220209
if (ret) return ret;
221210
}
222-
return 0;
211+
return ret;
223212
}
224213

225-
size_t elems_to_args(struct Elements *elements, struct DocoptArgs *args,
214+
int elems_to_args(struct Elements *elements, struct DocoptArgs *args,
226215
const bool help, const char *version) {
227216
struct Command *command;
228217
struct Argument *argument;
229218
struct Option *option;
230-
size_t i, j;
219+
int i, j;
231220

232221
/* fix gcc-related compiler warnings (unused) */
233222
(void) command;
@@ -239,11 +228,11 @@ size_t elems_to_args(struct Elements *elements, struct DocoptArgs *args,
239228
if (help && option->value && strcmp(option->olong, "--help") == 0) {
240229
for (j = 0; j < $help_message_n; j++)
241230
puts(args->help_message[j]);
242-
return 1;
231+
return EXIT_FAILURE;
243232
} else if (version && option->value &&
244233
strcmp(option->olong, "--version") == 0) {
245234
puts(version);
246-
return 1;
235+
return EXIT_FAILURE;
247236
}$if_flag$if_option
248237
}
249238
/* commands */
@@ -256,15 +245,15 @@ size_t elems_to_args(struct Elements *elements, struct DocoptArgs *args,
256245
argument = &elements->arguments[i];
257246
$if_argument
258247
}
259-
return 0;
248+
return EXIT_SUCCESS;
260249
}
261250

262251

263252
/*
264253
* Main docopt function
265254
*/
266255

267-
struct DocoptArgs docopt(size_t argc, char *argv[], const bool help, const char *version) {
256+
struct DocoptArgs docopt(int argc, char *argv[], const bool help, const char *version) {
268257
struct DocoptArgs args = {$defaults
269258
usage_pattern,
270259
$help_message

docopt_c/_data/template.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,19 @@
99

1010
#else
1111

12-
#include "stdbool.h"
12+
#ifdef true
13+
#undef true
14+
#endif
15+
#ifdef false
16+
#undef false
17+
#endif
18+
#ifdef bool
19+
#undef bool
20+
#endif
21+
22+
#define true 1
23+
#define false !true
24+
typedef int bool;
1325

1426
#endif
1527

@@ -69,6 +81,6 @@ struct DocoptArgs {
6981
const char *help_message[$help_message_n];
7082
};
7183

72-
struct DocoptArgs docopt(size_t, char *[], bool, const char *);
84+
struct DocoptArgs docopt(int, char *[], bool, const char *);
7385

7486
#endif

0 commit comments

Comments
 (0)