[pypy-commit] creflect default: more command-line tweaks

arigo noreply at buildbot.pypy.org
Mon Nov 17 16:04:57 CET 2014


Author: Armin Rigo <arigo at tunes.org>
Branch: 
Changeset: r50:555bd1e5574e
Date: 2014-09-19 16:35 +0200
http://bitbucket.org/cffi/creflect/changeset/555bd1e5574e/

Log:	more command-line tweaks

diff --git a/creflect/cmdline.py b/creflect/cmdline.py
--- a/creflect/cmdline.py
+++ b/creflect/cmdline.py
@@ -1,19 +1,19 @@
 """Usage:
 
-    creflect [options] input.rfl.c [output.c]
+    creflect [options] input{.rfl.c,.h} output.c
 
 Read the 'input.rfl.c' file and expand the CREFLECT sections in it
 into regular C code.  Write the result to 'output.c'.
 
-The default output file name is built from the input file name, by
-removing the '.rfl' part.  The input and/or output file names
-can be specified as '-' to read from stdin and/or write to stdout.
+The input and/or output file names can be specified as '-' to read
+ from stdin and/or write to stdout.
 
 Options:
     -m, --main      for debugging, include a main() function that
                       prints the recorded reflection information
     -n, --no-copy   output only the translated creflect sections,
                       without copying the text around them
+    -i, --include   emit a line ``#include "inputfile"''; implies -n
     -h, --help      display this help and exit
     --version       output version information and exit
 """
@@ -28,17 +28,21 @@
 
 def main(argv):
     try:
-        options, args = getopt.gnu_getopt(argv, "mnh",
-            ["main", "no-copy", "help", "version"])
+        options, args = getopt.gnu_getopt(argv, "mnih",
+            ["main", "no-copy", "include", "help", "version"])
     except getopt.GetoptError, e:
         return error(e)
     #
     from . import __version__
     include_main = False
     include_text_outside_creflect = True
+    include_includes = False
     for option, value in options:
         if option == '-n' or option == '--no-copy':
             include_text_outside_creflect = False
+        elif option == '-i' or option == '--include':
+            include_text_outside_creflect = False
+            include_includes = True
         elif option == '-m' or option == '--main':
             include_main = True
         elif option == '-h' or option == '--help':
@@ -48,22 +52,10 @@
             print 'CReflect %s' % __version__
             return 0
     #
-    if len(args) == 0:
-        return error("missing input file")
-    inputfile = args[0]
-    if len(args) > 1:
-        outputfile = args[1]
-        if len(args) > 2:
-            return error("too many arguments")
-    else:
-        if inputfile == '-':
-            return error("destination file must be specified if input is '-'")
-        i = inputfile.lower().rfind('.rfl')
-        if i < 0:
-            return error("cannot find '.rfl' in the input file name: specify "
-                         "the output file name explicitly")
-        outputfile = inputfile[:i] + inputfile[i+4:]
-    if inputfile != '-' and inputfile == outputfile:
+    if len(args) != 2:
+        return error("expected exactly 2 arguments, got %d" % (len(args),))
+    inputfile, outputfile = args
+    if inputfile == outputfile:
         return error("not overwriting the file %r" % (inputfile,))
     #
     from . import driver
@@ -79,6 +71,8 @@
         outputf = open(outputfile, 'w')
     #
     try:
+        if include_includes:
+            outputf.write('#include "%s"\n' % (inputfile,))
         blocks = driver.copy_file_and_expand(inputf, outputf,
             include_text_outside_creflect = include_text_outside_creflect)
         if include_main:
diff --git a/creflect/driver.py b/creflect/driver.py
--- a/creflect/driver.py
+++ b/creflect/driver.py
@@ -38,6 +38,8 @@
         if funcname is END:
             raise CDefError("line %d: 'CREFLECT: end' without "
                             "'CREFLECT: funcname()' before" % lineno)
+        if not include_text_outside_creflect:
+            outputf.write("\n")
         #
         # inside a 'CREFLECT:' section
         csource = []


More information about the pypy-commit mailing list