Development

NAVIGATION
CATEGORIES
REFERRENCE
LINKS
  • An optparse question

    8 answers - 525 bytes - related search similar search Add To My Delicious Add To My Stumble Upon Add To My Google Mark Add To My Facebook Add To My Digg Add To My Reddit

    I have a short program using Parser that prints out help
    message with -h flag:
    % myprog.py -h
    usage: myprog.py [options] input_file
    options:
    -h, show this help message and exit
    -v, print program's version number and exit
    -o FILE 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 Parser.print_usage() function?
  • No.1 | | 746 bytes | |

    T wrote:
    I have a short program using Parser that prints out help
    message with -h flag:

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

    options:
    -h, show this help message and exit
    -v, print program's version number and exit
    -o FILE 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 Parser.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 = Parser(usage=usage)
    parser.print_help()
  • No.2 | | 726 bytes | |

    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 = Parser(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, show this help message and exit
    -v, print program's version number and exit
    -o FILE file
  • No.3 | | 567 bytes | |

    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 Parser

    usage = ' THIS IS NEWLY INSERTED STRING
    \nusage: %prog [options] input_file'
    parser = Parser(usage=usage)
    parser.print_help()
  • No.4 | | 1158 bytes | |

    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 = Parser(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, show this help message and exit
    -v, print program's version number and exit
    -o FILE file

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

    from optparse import Parser, _, IndentedHelpFormatter

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

    parser = Parser(formatter=MyFormatter())

    The above filthy hack will print "Hi there!" before the usual usage
    message.
  • No.5 | | 1483 bytes | |

    dan.g (AT) gmail (DOT) com 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 Parser

    usage = ' THIS IS NEWLY INSERTED STRING
    \nusage: %prog [options] input_file'
    parser = Parser(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 = Parser(usage=usage)
    parser.print_help()
    usage: THIS IS NEWLY INSERTED STRING
    usage: lopts.py [options] input_file

    options:
    -h, 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.

    IMH, 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 = odictcopy()

    Peace,
    ~Simon
  • No.6 | | 1012 bytes | |

    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 = Parser(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, show this help message and exit
    -v, print program's version number and exit
    -o FILE file

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

    regards
    Steve
  • No.7 | | 462 bytes | |

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

    parser = Parser(usage=usage)
    parser.print_help()
    usage: THIS IS NEWLY INSERTED STRING
    usage: lopts.py [options] input_file

    options:
    -h, 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
  • No.8 | | 1242 bytes | |

    "T" <ty.2006 (AT) yahoo (DOT) comwrites:
    []
    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, show this help message and exit
    -v, print program's version number and exit
    -o FILE 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):
    (
    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 = Parser(
    usage="%prog [options] input_file",
    ())
    parser.parse_args()

    John

Re: An optparse question


max 4000 letters.
Your nickname that display:
In order to stop the spam: 2 + 2 =
QUESTION ON "Development"

EMSDN.COM