Tuesday, December 08, 2009

An ideal options / arguments parsing library for Python

One of primary areas of Python applications is helper scripts and system utilities that are often called from command line. Even programming in Python itself is not comfortable without interactive mode at hand. That's why argument parsing libraries are important to some degree to many Python developers.

There once was getopt library, but it was awkward compared to newer optparse module, but even optparse nowadays is not enough for some who is willing to replace it with argparse. optparse is a kind of "stable minimal API" that is not easy to push a change to. As a person who has a lot of Python command tools at hand I often feel that I need patched version of optparse, but I can't afford managing dependencies or inserting a bulk of code into 5 minute script that should be as minimal and clear as possible for easy maintenance. So I wouldn't mind against more featured argparse library.

However, argparse can only be useful in standard library if it will be:
  1. Minimalistic. Like optparse won't take much brainspace to avoid referencing documentation for 80% of the stuff
  2. Easily extensible
  3. Contains copy/paste recipes for popular usages
On the first look argparse is not minimalistic - even in constructor there are a lot of params that won't be used in 80% of cases and that could be easily moved into configuration methods.

Is it easily extensible? It allows to specify formatter class, conflict_handler class, but it seems like you have very limited control over what will be matched as option and what as an argument. There are two options to constructor to define options prefix - prefix_chars and fromfile_prefix_chars, but they are awkward. I wonder if I can provide /longarg on windows in addition to --longarg leaving also /s and -s options? It could be better done with the same custom class, which could be provided in recipes or as an optional import.

There are no recipes in argparse. That's bad, because that means the library is designed as a comprehensive solution to all command-line problems, but it still can't help me make the majority of my scripts more convenient without too much overhead.

argparse is not a huge leap from optparse as the latter once was for getopt in my opinion. For any argument parsing library to become a quality leap from optparse, it should allow an easy way to build svn like command line interfaces with the following features:
  1. calling without arguments shows tool name, version and usage info
  2. usage info contains either command list, options list or both
  3. "tool.py help " is equivalent to "tool.py -h"
  4. remove ugly default optparse help for options with arguments

    -f FILE, --file=FILE  write report to FILE
    SVN is fine
    -r [--revision] ARG      : ARG (some commands also take ARG1:ARG2 range)
    or Mercurial 
    
    -l --logfile       read commit message from  
    
  5. cross-platform optional paging

This is a functionality of an ideal argument parsing library. Feel free to add your own user story.