I wanted to add a feature to optparse, which I
successfully accomplished by subclassing add_option. It
works fine and meets my needs, but it occurred to me
that perhaps others might find the new capability useful
enough to have the facility "built in" to the standard
version.
In a nutshell, I want to be able to write programs that (of
course) have useful default settings. However, I quickly
found that it would be helpful to be able to change
defaults in certain situations. For example, I wrote a
utility to access a MySQL database. optparse defaults
are set for such items as the MySQL host system
(specifically "localhost"), plus database name, etc.
Frequently I want to run the utility on the system where
the database is (thus, the localhost default). However, I
also want to run the utility on a different system,
accessing the database on the first system. Therefore I
explicity need to specify the database host DNS name or
IP address rather than localhost. (This is a real example:
I can't make the database host DNS name or IP address
the default because I have written the utility to be used
by anyone who has a similar database. Thus, "localhost".)
Of course, I could just define a shell alias, but decided it
would be more convenient to have optparse set defaults
from a .ini file (if it exists). This way I can create a
different .ini file as appropriate for each system (or
situation) and optparse will set "local" defaults.
I am attaching my .py (which describes a sample .ini file).
Please have a look and see if the generic feature might
be useful for a future version of optparse. Of course, I
would welcome any comments and suggestions to
improve what I have done.
Thanks
Allan Wilson
IniOptionParser.py; sample code
Logged In: YES
user_id=14422
I think this sort of thing is best addressed by a separate
module. See for example http://cfgparse.sourceforge.net/ .
Logged In: YES
user_id=725330
Thanks for the additional feedback. I haven't looked at this
code for some time, but will take a look in follow-up to
your suggestion.
Thanks
Allan