Showing posts with label command line. Show all posts
Showing posts with label command line. Show all posts

Wednesday, March 17, 2010

Coding Dojo Again

So we tried another coding dojo at PyGTA this month. The goal was to write a command application in Python which is trivial to do using only the standard library. However, a coding dojo is an opportunity to learn new techniques so I suggested we use ScriptTest and argparse, although we never got very far with the latter.

This is the second coding dojo we've done at PyGTA so that's the only benchmark I have to judge how well it worked. There was definitely more frustration evident this time which is an indication we are doing something wrong. I like the coding dojo format but we will need to figure out how to run them better before trying it again.

Saturday, February 20, 2010

Command Line Arguments

I've switched to argparse, an awesome replacement for optparse, the standard Python library for parsing command line arguments. Here is a feature comparison.

Subcommand support was the feature that sold me on argparse. It is now common practice to break a complex command line utility into subcommands that are invoked by the first positional argument. argparse handles this out of the box and even provides a way to store a function name, so you don't even have to figure out which subcommand was invoked.

Saturday, September 19, 2009

Twidge

I was using Ping.fm to post to Twitter and Identica simultaneously. Ping.fm worked most of the time but occasionally it would suddenly became very unreliable for a few days. I decided that it was time to try a command line utility call Twidge, which has been on my play-with-this-cool-toy list for a while.

Twidge can post to both Twitter and Identica but can't do it with a single invocation. That's easily solved with a simple three line shell script taken from the Twidge web site. Or you can use the slightly more fancy script which gives a text length marker so you don't exceed the 140 character limit.

Of course, it was hard to resist "improving" things. ^_^ Here is my current script:
#!/bin/bash
tmpfile=$(mktemp)
trap "rm ${tmpfile}" EXIT
vim ${tmpfile}
if [ ! -s "${tmpfile}" ]; then
echo "No text, aborting update."
exit 1
fi
text=$(cat "${tmpfile}")
twidge -c "$HOME/.twidge/identica" update "${text}"
text=$(echo "${text}"|sed 's/\(^\| \)!\([[:alnum:]]\)/\1#\2/g')
twidge -c "$HOME/.twidge/twitter" update "${text}"
There basically two additions over the original script. First, the text for the post is created in the vim editor, which is a more comfortable writing enviroment and, more importantly, has a spell checker! The second addition is to convert Identica groups, which are specified as "!tag", into a Twitter "#tag", since Twitter doesn't have groups.

Hah, I hear you say! Why did my script become so complicated? But it is not really complicated once you realize these scripts are able to duplicate the basic functionality of Ping.fm, a complex web service, in a few lines of shell code. And so far, the script has proven to be more reliable that Ping.fm. The script is only as complicated as it needs to be, but no more.

Long live the command line!