[getopt-sig] ObjectCLI
Shane Hathaway
shane@zope.com
Thu, 14 Feb 2002 10:19:05 -0500
Hi everyone,
Here is an approach I'd like you to consider for parsing command-line
arguments. I used this approach in the "pma" project (an NNTP server
built on ZODB--see the zodbex project at SourceForge.)
ObjectCLI is a class that lets you invoke methods of a Python object
directly from the command line. The concept is similar to Zope's
publisher, which lets you call methods directly from the Web. Say you
created a class like this:
class ServerControl:
"""A proxy server."""
def start(port):
"""Starts the server on the specified port."""
def stop():
"""Stops the server."""
With two or three lines of glue, you can then invoke the methods of a
ServerControl instance directly from the command line:
python server_control.py start --port=3128
python server_control.py stop
You can also get the documentation of ServerControl, primarily derived
from the docstrings:
python server_control.py --help
And you can get more in-depth documentation about each method:
python server_control.py start --help
The author of the ServerControl class never has to use getopt(). All
you have to do is expose an object to the command line via ObjectCLI.
(ObjectCLI uses getopt() to do its work.)
Major benefits:
- You almost never have to look up any library documentation to write
new command line utilities. You just write a regular Python class.
- The command line arguments and the implementation are less likely to
fall out of sync.
- The documentation comes directly from the docstrings.
What do you think?
Shane