An optparse question

Discussion in 'Python' started by T, Jul 21, 2006.

  1. T

    T Guest

    I have a short program using optparse.OptionParser that prints out help
    message with -h flag:

    % myprog.py -h
    usage: myprog.py [options] input_file

    options:
    -h, --help show this help message and exit
    -v, --verbose print program's version number and exit
    -o FILE Output file


    My question is, is there a way to print a blank line (or any string)
    before "usage: myprog.py [options] input_file" ? I tried using
    callbacks without success. I think somehow I need to modify the
    behavior of optparse.OptionParser.print_usage() function?
    T, Jul 21, 2006
    #1
    1. Advertising

  2. T wrote:
    > I have a short program using optparse.OptionParser that prints out help
    > message with -h flag:
    >
    > % myprog.py -h
    > usage: myprog.py [options] input_file
    >
    > options:
    > -h, --help show this help message and exit
    > -v, --verbose print program's version number and exit
    > -o FILE Output file
    >
    >
    > My question is, is there a way to print a blank line (or any string)
    > before "usage: myprog.py [options] input_file" ? I tried using
    > callbacks without success. I think somehow I need to modify the
    > behavior of optparse.OptionParser.print_usage() function?


    you can make the usage line anything you want.

    ....
    usage = 'This is a line before the usage line\nusage %prog [options]
    input_file'
    parser = OptionsParser(usage=usage)
    parser.print_help()
    ....
    fuzzylollipop, Jul 21, 2006
    #2
    1. Advertising

  3. T

    T Guest

    fuzzylollipop wrote:
    >
    > you can make the usage line anything you want.
    >
    > ...
    > usage = 'This is a line before the usage line\nusage %prog [options]
    > input_file'
    > parser = OptionsParser(usage=usage)
    > parser.print_help()
    > ...
    >


    No, that affects the string printed only *after* the "usage = " string.
    What I would like to do is insert some string *before* the "usage = "
    string, which is right after the command I type at the command prompt.
    So I would like to make it look like this:

    % myprog.py -h
    ************ THIS IS NEWLY INSERTED STRING ************
    usage: myprog.py [options] input_file


    options:
    -h, --help show this help message and exit
    -v, --verbose print program's version number and exit
    -o FILE Output file
    T, Jul 21, 2006
    #3
  4. T

    Guest

    > No, that affects the string printed only *after* the "usage = " string.
    > What I would like to do is insert some string *before* the "usage = "
    > string, which is right after the command I type at the command prompt.
    > So I would like to make it look like this:


    The example was fine (except for a typo) as far as demonstrating the
    concept. Try this corrected version:

    from optparse import OptionParser

    usage = '************ THIS IS NEWLY INSERTED STRING
    ************\nusage: %prog [options] input_file'
    parser = OptionParser(usage=usage)
    parser.print_help()
    , Jul 21, 2006
    #4
  5. T

    Simon Forman Guest

    T wrote:
    > fuzzylollipop wrote:
    > >
    > > you can make the usage line anything you want.
    > >
    > > ...
    > > usage = 'This is a line before the usage line\nusage %prog [options]
    > > input_file'
    > > parser = OptionsParser(usage=usage)
    > > parser.print_help()
    > > ...
    > >

    >
    > No, that affects the string printed only *after* the "usage = " string.
    > What I would like to do is insert some string *before* the "usage = "
    > string, which is right after the command I type at the command prompt.
    > So I would like to make it look like this:
    >
    > % myprog.py -h
    > ************ THIS IS NEWLY INSERTED STRING ************
    > usage: myprog.py [options] input_file
    >
    >
    > options:
    > -h, --help show this help message and exit
    > -v, --verbose print program's version number and exit
    > -o FILE Output file


    It's possible, but it ain't easy:

    from optparse import OptionParser, _, IndentedHelpFormatter

    class MyFormatter(IndentedHelpFormatter):
    pre_usage = "Hi there!\n"
    def format_usage(self, usage):
    return _("%susage: %s\n") % (self.pre_usage, usage)

    parser = OptionParser(formatter=MyFormatter())


    The above filthy hack will print "Hi there!" before the usual usage
    message.
    Simon Forman, Jul 21, 2006
    #5
  6. T

    Simon Forman Guest

    wrote:
    > > No, that affects the string printed only *after* the "usage = " string.
    > > What I would like to do is insert some string *before* the "usage = "
    > > string, which is right after the command I type at the command prompt.
    > > So I would like to make it look like this:

    >
    > The example was fine (except for a typo) as far as demonstrating the
    > concept. Try this corrected version:
    >
    > from optparse import OptionParser
    >
    > usage = '************ THIS IS NEWLY INSERTED STRING
    > ************\nusage: %prog [options] input_file'
    > parser = OptionParser(usage=usage)
    > parser.print_help()


    Nope. That only *nearly* does what T wants. The usage message will
    still be printed immediately *after* the 'usage: ' string.

    >>> parser = OptionParser(usage=usage)
    >>> parser.print_help()

    usage: ************ THIS IS NEWLY INSERTED STRING************
    usage: lopts.py [options] input_file

    options:
    -h, --help show this help message and exit


    I had the same problem, and in order to get something printed before
    the usage message, I found one easy-ish way was to subclass the
    Formatter passed in to the Parser.

    IMHO, optparse does a tricky task well, but it's implemented in a hard
    to follow, inflexible manner. My "favorite" pet peeve is that the
    options "dictionary" it returns isn't a dict. I wound up doing this to
    it to get something [I considered] useful:

    o, a = parser.parse_args()
    o = o.__dict__.copy()


    Peace,
    ~Simon
    Simon Forman, Jul 21, 2006
    #6
  7. T

    Steve Holden Guest

    T wrote:
    > fuzzylollipop wrote:
    >
    >>you can make the usage line anything you want.
    >>
    >>...
    >>usage = 'This is a line before the usage line\nusage %prog [options]
    >>input_file'
    >>parser = OptionsParser(usage=usage)
    >>parser.print_help()
    >>...
    >>

    >
    >
    > No, that affects the string printed only *after* the "usage = " string.
    > What I would like to do is insert some string *before* the "usage = "
    > string, which is right after the command I type at the command prompt.
    > So I would like to make it look like this:
    >
    > % myprog.py -h
    > ************ THIS IS NEWLY INSERTED STRING ************
    > usage: myprog.py [options] input_file
    >
    >
    > options:
    > -h, --help show this help message and exit
    > -v, --verbose print program's version number and exit
    > -o FILE Output file
    >

    Do a Google search for "monkey patching". You probably want to
    monkey-patch the class's usage method.

    regards
    Steve
    --
    Steve Holden +44 150 684 7255 +1 800 494 3119
    Holden Web LLC/Ltd http://www.holdenweb.com
    Skype: holdenweb http://holdenweb.blogspot.com
    Recent Ramblings http://del.icio.us/steve.holden
    Steve Holden, Jul 21, 2006
    #7
  8. T

    Guest


    > Nope. That only *nearly* does what T wants. The usage message will
    > still be printed immediately *after* the 'usage: ' string.
    >
    > >>> parser = OptionParser(usage=usage)
    > >>> parser.print_help()

    > usage: ************ THIS IS NEWLY INSERTED STRING************
    > usage: lopts.py [options] input_file
    >
    > options:
    > -h, --help show this help message and exit


    Yes, I see what T meant now. Behavior expectations (assumptions) has a
    way of clouding one's vision.

    Thanks
    , Jul 21, 2006
    #8
  9. T

    John J. Lee Guest

    "T" <> writes:
    [...]
    > What I would like to do is insert some string *before* the "usage = "
    > string, which is right after the command I type at the command prompt.
    > So I would like to make it look like this:
    >
    > % myprog.py -h
    > ************ THIS IS NEWLY INSERTED STRING ************
    > usage: myprog.py [options] input_file
    >
    >
    > options:
    > -h, --help show this help message and exit
    > -v, --verbose print program's version number and exit
    > -o FILE Output file


    HelpFormatter is what you need. Seems undocumented in the official
    docs, but doesn't look risky to use (famous last words). Seems just
    that nobody got around to documenting it.

    import optparse

    class NonstandardHelpFormatter(optparse.HelpFormatter):

    def __init__(self,
    indent_increment=2,
    max_help_position=24,
    width=None,
    short_first=1):
    optparse.HelpFormatter.__init__(
    self, indent_increment, max_help_position, width, short_first)

    def format_usage(self, usage):
    return "************ THIS IS NEWLY INSERTED STRING ************\nusage: %s\n" % usage

    def format_heading(self, heading):
    return "%*s%s:\n" % (self.current_indent, "", heading)

    parser = optparse.OptionParser(
    usage="%prog [options] input_file",
    formatter=NonstandardHelpFormatter())
    parser.parse_args()


    John
    John J. Lee, Jul 22, 2006
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. GMTaglia

    optparse question

    GMTaglia, Sep 18, 2004, in forum: Python
    Replies:
    5
    Views:
    652
    Thorsten Kampe
    Sep 19, 2004
  2. Steven W. Orr
    Replies:
    0
    Views:
    434
    Steven W. Orr
    Feb 9, 2007
  3. Steven W. Orr
    Replies:
    3
    Views:
    320
    Steven Bethard
    Feb 11, 2007
  4. Pat

    optparse question

    Pat, Jan 27, 2009, in forum: Python
    Replies:
    14
    Views:
    510
    Thorsten Kampe
    Jan 28, 2009
  5. Joseph Garvin
    Replies:
    0
    Views:
    315
    Joseph Garvin
    May 20, 2009
Loading...

Share This Page