Cog 2.0, beta 2

Sunday 28 August 2005

I've added some more features to Cog, and am presenting them as Cog 2.0 beta 2.

New features include globally defined strings on the command line, marker suffixes for generated lines, and @files that provide options. On the negative side, Python 2.2 is no longer supported. If you need 2.2, I can make it work, just let me know. I'll describe each feature in turn.

Globally defined strings: Theo Burt requested a way to pass an argument into the Python code from the command line. The new -D option does just that. For example, invoking Cog like this:

cog -D thefile=fooey.xml mycode.txt

will run Cog over mycode.txt, but first define a global variable called thefile with a value of "fooey.xml". This variable can then be referenced in your generator code. You can provide multiple -D arguments on the command line, and all will be defined and available. Only strings can be defined (to simplify the problem of quoting).

Marker suffixes: When grepping a large amount of source, I find it helpful to be able to see at a glance that the found line was generated rather than hand-written. The new -s option provides a suffix which is appended to every non-blank line generated, so that the lines can be readily identified as generated lines.

For example, with this input file (mycode.txt):

mycode.txt

[[[cog
cog.outl('One through five:\n')
for i in range(5):
    cog.outl('This is line %d' % i)
]]]
[[[end]]]

invoking cog like this:

cog -s " //(generated)" mycode.txt

will produce this output:

[[[cog
cog.outl('One through five:\n')
for i in range(5):
    cog.outl('This is line %d' % i)
]]]
One through five: //(generated)

This is line 0 //(generated)
This is line 1 //(generated)
This is line 2 //(generated)
This is line 3 //(generated)
This is line 4 //(generated)
[[[end]]]

@file options: To make these options easier to use over multiple files, the @file feature has been extended to allow for command-line options to be provided along with file names. For example, you can create a file cogfiles.txt:

cogfiles.txt

# These are the files I run through cog
mycode.cpp
myothercode.cpp
myschema.sql -s " --**cogged**"
reademe.txt -s ""

then invoke cog like this:

cog -s " //**cogged" @cogfiles.txt

Now cog will process four files, using C++ syntax for markers on all the C++ files, SQL syntax for the .sql file, and no markers at all on the readme.txt file.

Another example: cogfiles2.txt could be:

cogfiles2.txt

template.h -D thefile=data1.xml -o data1.h
template.h -D thefile=data2.xml -o data2.h

with cog invoked like this:

cog -D version=3.4.1 @cogfiles2.txt

Cog will process template.h twice, creating both data1.h and data2.h. Both executions would define the variable version as "3.4.1", but the first run would have thefile equal to "data1.xml" and the second run would have thefile equal to "data2.xml".

Feedback: Please try it, and let me know what you think.

tagged: » react

Comments

Add a comment:

name
email
Ignore this:
not displayed and no spam.
Leave this empty:
www
not searched.
 
Name and either email or www are required.
Don't put anything here:
Leave this empty:
URLs auto-link and some tags are allowed: <a><b><i><p><br><pre>.