optional arguments with compact reporting in optparse

Steven Bethard steven.bethard at gmail.com
Thu Nov 8 11:44:56 EST 2007


braver wrote:
> Posted to the Optik list, but it seems defunct.  Optik is now Python's
> optparse.
> 
> I wonder how do you implement optional arguments to Optik.

You may want to check out argparse:

     http://argparse.python-hosting.com/

It supports optional arguments like this::

     parser = argparse.ArgumentParser()
     parser.add_argument('-P', metavar='file', nargs='?', const='data')
     args = parser.parse_args()
     if args.file is not None:
         # -P was supplied, so do something with the file
         # if no argument to -P was given, it will be 'data'


> Another question I have it how can I output a docstring right from the
> parser.add_option() block?  I prefer to define all option-related
> things in the same block once.  I'd like to output the help value from
> the block, or append it to a report.
> 
> Here's example from my Ruby wrapper for Ruby's optparse:

I don't understand what you want here (and I don't understand the Ruby 
code).  Can you try explaining what you're trying to do here a different 
way?

STeVe



More information about the Python-list mailing list