This is one of the truly awesome posts that I have been dying to share. Terminal is like the launchpad for developers. It’s where we go to do things. For a while, all I could do is write bash scripts, and then I barely started adding custom commands in the
After writing bash commands for a year or so, and realizing that I hate it so bad, and I am barely getting good at it, I decided to go full-fledged python. Every script I write should be in python, whether it was to rename files, or deploy this post online. But how can I quickly write python scripts and make them accessible? Here is how:
The Bad Way™
The way I did it initially was:
Quickly, I realized I am typing things again and again… Not to mention the inability to define and edit functions easily. The lack of autoComplete and accessible documentation was another thing, but some might argue it’s solvable by ipython, which never worked well for me.
The next immediate course of action was to simply create a directory somewhere, call it sandbox or something, and put the scripts there. The issue here is having to navigate to that directory to run the script, or at least add the full path of the scripts…
Slightly Better + Python
Let’s add some python scripts to the slightly better way, and see how the pipeline becomes simply awesome.
First, we will put all the scripts in a folder, as explained above. In my case, this folder is ~/.pyscripts:
In my case,
blog.py contains scripts that help me manage my blog,
imageutils.py, if you haven’t guessed, manipulates images, … etc.
Each script file would have at least one method defined with a
cmd_ prefix. For example, here is part of the
OK… So, we have these script files with
cmd_X functions defined, then what?
.bash_profile, which is executed each time terminal is loaded, we add this code:
Needless to say, the first line executes the
gen_cmds.py script. This script generates things (we’ll see in a bit) that are written to the
.pycmds file. The second line above “copies” all the contents of
.bash_profile. Think of it as a
#include ".pycmds" type of thing.
Here is where the fun begins. Look at the
What this simply does is, it iterates all the script files for the
cmd_ functions, and write them, with their docs, to the
.pycmds file! Here is how the
.pycmds looks like:
Can you see what’s going on here? Other than the
echo commands, that simply display the commands in the beginning, it generates an alias with the command’s name that passes the function’s name to a
driver.py script, which in turn executes the script! The real beauty lies in the fact that the alias ends just after
--arguments. This is what allows us to pass as many parameters as we want to the script!
Let’s look at the driver script, and then we’ll do a quick recap:
Amazing, if I do say so myself! The script adds an
argparse, which allows it to take input from terminal. The input is
arguments. So, it imports the module, uses
getattr to fetch the function, and BAM, calls the function with the arguments!
Let’s recap here…
- Add scripts to
- When terminal launches, call
gen_cmds.pyto generate the
- Include the file that
gen_cmds.pygenerates, by using
- When a script is executed, it’s passed with the arguments to
driver.pywill fetch the function, pass it the arguments, and voila!