Skip to content

Commit

Permalink
Fix the previous commit with better implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
kblomqvist committed Mar 30, 2013
1 parent 1c58813 commit 9c7cc84
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 30 deletions.
7 changes: 1 addition & 6 deletions docopt.c
Original file line number Diff line number Diff line change
Expand Up @@ -218,13 +218,8 @@ const char usage_pattern[] =
" the_program -h | --help | --version";

DocoptArgs docopt(int argc, char *argv[], bool help, const char *version) {
static char host[] = "localhost";
static char port[] = "1234";
static char timeout[] = "10";
static char baud[] = "9600";

DocoptArgs args = {
0, 0, 0, 0, host, port, timeout, baud,
0, 0, 0, 0, (char*) "localhost", (char*) "1234", (char*) "10", (char*) "9600",
usage_pattern, help_message
};
Element options[] = {
Expand Down
31 changes: 7 additions & 24 deletions docopt_c.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,8 @@
<<<usage_pattern>>>;
DocoptArgs docopt(int argc, char *argv[], bool help, const char *version) {
<<<defaults>>>
DocoptArgs args = {
<<<defaults_initialization_list>>>,
<<<defaults>>>,
usage_pattern, help_message
};
Element options[] = {
Expand Down Expand Up @@ -225,6 +224,7 @@
"""
import sys
import re
import docopt


Expand Down Expand Up @@ -269,25 +269,6 @@ def c_if_not_flag(o):
c_name(o.long or o.short))


def c_default_options_static_strings(opts):
t = ''
for o in opts:
if type(o.value) is str:
t += "static char %s[] = %s;\n" % (c_name(o.long), to_c(o.value))
return t


def c_default_options_initialization_list(opts):
t = ''
for o in opts:
if type(o.value) is str:
t += c_name(o.long) + ', '
else:
t += to_c(o.value) + ', '
return t.strip(', ')



if __name__ == '__main__':
doc = sys.stdin.read()
usage_sections = docopt.parse_section('usage:', doc)
Expand All @@ -310,9 +291,11 @@ def c_default_options_initialization_list(opts):
for o in options if o.argcount == 1))
out = out.replace('<<<help_message>>>', to_c(doc))
out = out.replace('<<<usage_pattern>>>', to_c(usage))
out = out.replace('<<<defaults>>>', c_default_options_static_strings(options))
out = out.replace('<<<defaults_initialization_list>>>',
c_default_options_initialization_list(options))

defaults = ', '.join(to_c(o.value) for o in sorted(options, key=lambda o: o.argcount))
defaults = re.sub(r'"(.*?)"', r'(char*) "\1"', defaults)
out = out.replace('<<<defaults>>>', defaults)

out = out.replace('<<<options>>>',
',\n '.join(c_option(o) for o in options))
out = out.replace('<<<if_flag>>>',
Expand Down

0 comments on commit 9c7cc84

Please sign in to comment.