Code Snippets For You, Part 2

Every coder has to deal with some repetitive tasks when they create a new application. Every new file added to your growing application, has some common properties, apart from selected language. Moreover, if you're into design patterns, these common properties become harder to repeat everytime you need them. Of course, i know, there are modern editors with code snippet support in these days.

However I invite you to imagine some apocalyptic scenario in which some crazy virus destroyed all Code-Snippet-Supporting-Editors. So we need to create our own code snippets, in order to re-create Code-Snippet-Supporting-Editors.

Someone has to do it, right?

In this part, we will build our add_class script line by line, using python. I prefer Python 2.7 since it's still much more widely available than it's successor.

We have a script to code

Before the start, we should create a working folder for ourselves.

~$ mkdir ~/TemplateScripts
~$ cd ~/TemplateScripts
~/TemplateScripts$ _

Then touch add_class file,

~/TemplateScripts$ touch add_class

After that, open it with your favorite editor, and fill in the following lines:

#!/usr/bin/env python

def main():
    print '[1] read the class name from script argument'
    print '[2] read a class template file'
    print '[3] open the corresponding class file for writing'
    print '[4] replace template variables with class name'
    print '[5] write replaced content to previously opened class file'
    print '[6] close class file'
    print '[7] go to [2] if more class template files exist'
    print '[8] end'

if __name__ == '__main__':
    main()

It's just a sketch of the script we intended to write. If we set the script file as executable, we should be able to run it.

~/TemplateScripts$ chmod +x add_class

The first line in our script, referred as shebang, decides which application interprets our script. In our case, it will be python interpreter from the current environment. Use of /usr/bin/env comes handy, especially if you prefer to work with virtual python environments. However it's also logical to use the following line as shebang,

#!/usr/bin/python

If we run our script at this point, we can see the expected output:

~/TemplateScripts$ ./add_class
[1] read class name from script argument
...
[8] end
~/TemplateScripts$ _

At this point, you may initialize a local git repo in your script folder. I will just continue by reading the class name, defining template files, and creating empty class files.

Reading names

In python, we can use argv parameter from sys module, to read command line arguments.

import sys
...
args = sys.argv

In this case, args will be an array started with script name, and followed by arguments. If no arguments are given, it will contain only script name.

Since we need only arguments, we will do this:

import sys
...
args = sys.argv[1:]

By using [1:], we can skip first element (that is script name) of argument array.

When we try it on our example,

~/TemplateScripts$ ./add_class Warrior Warlock

args array will hold this after command invocation:

['Warrior', 'Warlock']

These are our class names. So our final code segment will be like this:

import sys
...
class_names = sys.argv[1:]

Files first

We use an array to keep our template file names.

templates = [
    '_class_template_.h',
    '_class_template_.cpp'
]

Now we should process these templates for each entry in class_names variable.

Since it's no good to have our main() function get messy, we use another function here:

...
for name in class_names:
    GenerateClass( name )
...

And in the definition of GenerateClass method,

...
def GenerateClass( class_name ):
    for t in templates:
        class_file_name = t.replace('_class_template_', class_name)
...

For given class name, for loop in this method will generate a proper class file name, using template file names. Finally we should create these files in our folder:

...
with open(class_file_name, 'wt') as fClass:
    fClass.writelines(['TODO\n'])
...

This will create our class files with only 'TODO' text in them. Here is the current state of our add_class script:

If we run it like this,

~/TemplateScripts$ ./addclass Warrior Warlock

then we check our folder to see created files:

~/TemplateScripts$ ls
add_class Warlock.cpp Warlock.h Warrior.cpp Warrior.h
~/TemplateScripts$ _

and check content of one:

~/TemplateScripts$ cat Warlock.cpp
TODO
~/TemplateScripts$ _

That's it, for now.