In the first few seconds of the talk, I thought to myself “I know where this is going. It’s brilliant.” I said so to the guy next to me. Then I took it back because while argparse is a pretty cool idea, its not the brilliant idea I thought it was.
See, I thought the tool would automatically introspect a script’s main method and use the docstring to provide any missing information. That’s what I thought would be brilliant.
Since it didn’t do that, I did it. Its certainly not full-featured yet, but it works for basic options and arguments. For 90% of scripts, that’s all you need. I called it opterator and put it on github:
git clone git://github.com/buchuki/opterator.git
Here’s an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
from opterator import opterate @opterate def main(filename1, filename2, recursive=False, backup=False, suffix='~', *other_filenames): '''An example copy script with some example parameters that might be used in a copy command. @param recursive store_true -r --recursive copy directories recursively @param backup store_true -b --backup backup any files you copy over @param suffix store -S --suffix override the usual backup suffix ''' filenames = [filename1, filename2] + list(other_filenames) destination = filenames.pop() print "You asked to move %s to %s" % (filenames, destination) if recursive: print "You asked to copy directories recursively." if backup: print "You asked to backup any overwritten files." print "You would use the suffix %s" % suffix if __name__ == '__main__': main()
Obviously, that only parses the options, its not doing the actual copying because implementing that would do nothing to illustrate how opterator is working. Here’s one run of it:
dusty:opterator $ python copy.pyc -b a b c d -r You asked to move ['a', 'b', 'c'] to d You asked to copy directories recursively. You asked to backup any overwritten files. You would use the suffix ~
Here’s the automatically generated help:
dusty:opterator $ python copy.py -h Usage: copy.py [options] filename1 filename2 [other_filenames] An example copy script with some example parameters that might be used in a copy command. Options: -h, --help show this help message and exit -r, --recursive copy directories recursively -b, --backup backup any files you copy over -S SUFFIX, --suffix=SUFFIX override the usual backup suffix
I named it opterator because I wanted a name that didn’t have ‘parse’ in it. I was going to go for operator but I realized if you heard about this app called operator and searched google for ‘python operator’ you’d get swamped. ‘opterator’ is unique and highlights the option part of the task. Plus it allowed me to invent a new verb: ‘opterate’. Days when I get to invent new verbs are always good days.
I used py.test for testing (another gem I discovered at pycon). You don’t need to run the tests, but the tests are useful examples to see what opterator can do, so have a look.
So I hope somebody finds this useful or even awesome and contributes some patches to make it more of the above. Enjoy!