How to output newline or carriage return with optparse

Discussion in 'Python' started by john.m.roach@gmail.com, Nov 8, 2007.

  1. Guest

    I'm trying to implement some simple command line options. Some of the
    'help' sections are long and I would like to control line breaks. How
    do you do this?

    Thanks!
     
    , Nov 8, 2007
    #1
    1. Advertising

  2. Tim Chase Guest

    > I'm trying to implement some simple command line options.
    > Some of the 'help' sections are long and I would like to
    > control line breaks. How do you do this?


    I had this problem earlier and solved it here:

    http://groups.google.com/group/comp.lang.python/browse_frm/thread/6df6e6b541a15bc2/09f28e26af0699b1

    thanks to a little guidance from Ben Finney on where to look.

    It came up again last month here:

    http://groups.google.com/group/comp.lang.python/browse_frm/thread/e72deee779d9989b/

    which should solve the problem for you.

    ASIDE: I've started refactoring this bit out in my local
    source...how would I go about contributing it back to the Python
    code-base? I didn't get any feedback from posting to the Optik
    site. My refactor basically takes an optional pre-processing
    function to parse your string into the output that gets passed to
    textwrap.wrap() and textwrap.fill(), defaulting to the old
    behavior, but offering a function for splitting it into
    paragraphs based on newlines.

    -tkc
     
    Tim Chase, Nov 8, 2007
    #2
    1. Advertising

  3. Tim Chase wrote:
    > ASIDE: I've started refactoring this bit out in my local source...how
    > would I go about contributing it back to the Python code-base? I didn't
    > get any feedback from posting to the Optik site.


    You can post a patch to bugs.python.org, but it will probably just get
    forwarded to the optik site because the optparse module is supposed to
    be automatically generated from the current optik release. My impression
    is that optik is mostly unmaintained, which makes getting patches
    applied to optik difficult...

    STeVe

    P.S. FWIW, I think it would be great to get your patch in -- this
    feature is requested all the time.
     
    Steven Bethard, Nov 8, 2007
    #3
  4. Tim Chase Guest

    >> ASIDE: I've started refactoring this bit out in my local
    >> source...how would I go about contributing it back to the
    >> Python code-base? I didn't get any feedback from posting to
    >> the Optik site.

    >
    > You can post a patch to bugs.python.org, but it will probably
    > just get forwarded to the optik site because the optparse
    > module is supposed to be automatically generated from the
    > current optik release. My impression is that optik is mostly
    > unmaintained, which makes getting patches applied to optik
    > difficult...


    What sorts of things are required? Just a patch? Or do I also
    need to include some sort of collection of doctest/unittests or
    documentation patches (and if so, against which documentation
    source)?

    -tkc
     
    Tim Chase, Nov 8, 2007
    #4
  5. Tim Chase wrote:
    >>> ASIDE: I've started refactoring this bit out in my local
    >>> source...how would I go about contributing it back to the
    >>> Python code-base? I didn't get any feedback from posting to
    >>> the Optik site.

    >>
    >> You can post a patch to bugs.python.org, but it will probably
    >> just get forwarded to the optik site because the optparse
    >> module is supposed to be automatically generated from the
    >> current optik release. My impression is that optik is mostly
    >> unmaintained, which makes getting patches applied to optik
    >> difficult...

    >
    > What sorts of things are required? Just a patch? Or do I also
    > need to include some sort of collection of doctest/unittests or
    > documentation patches (and if so, against which documentation
    > source)?


    Yes, your patch will need to include tests and docs as well. I would
    make the patch against the sources from the optik site (though I didn't
    see a CVS or SVN repository so you may just have to do it against the
    source release files). Add your tests to test/test_optik.py and your
    doc fixes to doc/reference.txt (or whichever doc/ file seems most
    appropriate).

    Thanks for taking this on, BTW.

    STeVe
     
    Steven Bethard, Nov 8, 2007
    #5
  6. Guest

    On Nov 8, 11:46 am, Tim Chase <> wrote:
    > > I'm trying to implement some simple command line options.
    > > Some of the 'help' sections are long and I would like to
    > > control line breaks. How do you do this?

    >
    > I had this problem earlier and solved it here:
    >
    > http://groups.google.com/group/comp.lang.python/browse_frm/thread/6df...
    >
    > thanks to a little guidance from Ben Finney on where to look.
    >
    > It came up again last month here:
    >
    > http://groups.google.com/group/comp.lang.python/browse_frm/thread/e72...
    >
    > which should solve the problem for you.
    >
    > ASIDE: I've started refactoring this bit out in my local
    > source...how would I go about contributing it back to the Python
    > code-base? I didn't get any feedback from posting to the Optik
    > site. My refactor basically takes an optional pre-processing
    > function to parse your string into the output that gets passed to
    > textwrap.wrap() and textwrap.fill(), defaulting to the old
    > behavior, but offering a function for splitting it into
    > paragraphs based on newlines.
    >
    > -tkc



    Thanks for the help Tim. I just copied and pasted your code into a
    file in my $PYTHONPATH (IndentedHelpFormatterWithNL.py), but I'm
    getting the following error:

    class IndentedHelpFormatterWithNL(IndentedHelpFormatter):
    NameError: name 'IndentedHelpFormatter' is not defined


    I tried adding: from optparse imoport IndentedHelpFormatter into the
    aforementioned file, but no luck again. What am I missing???

    Thanks.
     
    , Nov 8, 2007
    #6
  7. Tim Chase Guest

    > Thanks for the help Tim. I just copied and pasted your code into a
    > file in my $PYTHONPATH (IndentedHelpFormatterWithNL.py), but I'm
    > getting the following error:
    >
    > class IndentedHelpFormatterWithNL(IndentedHelpFormatter):
    > NameError: name 'IndentedHelpFormatter' is not defined
    >
    >
    > I tried adding: from optparse imoport IndentedHelpFormatter into the
    > aforementioned file, but no luck again. What am I missing???


    spelling "import" correctly? :) Also, make sure that, if you've
    named your module "IndentedHelpFormatterWithNL.py" that created
    your parser with

    parser = OptionParser(...
    formatter=
    IndentedHelpFormatterWithNL.IndentedHelpFormatterWithNL
    )

    You'll also want to make sure that these two lines:

    from optparse import IndentedHelpFormatter
    import textwrap

    are at the top of the IndentedHelpFormatterWithNL.py file, not at
    the top of the file importing the IndentedHelpFormatter.

    -tkc
     
    Tim Chase, Nov 8, 2007
    #7
  8. John Guest

    On Nov 8, 12:40 pm, Tim Chase <> wrote:
    > > Thanks for the help Tim. I just copied and pasted your code into a
    > > file in my $PYTHONPATH (IndentedHelpFormatterWithNL.py), but I'm
    > > getting the following error:

    >
    > > class IndentedHelpFormatterWithNL(IndentedHelpFormatter):
    > > NameError: name 'IndentedHelpFormatter' is not defined

    >
    > > I tried adding: from optparse imoport IndentedHelpFormatter into the
    > > aforementioned file, but no luck again. What am I missing???

    >
    > spelling "import" correctly? :) Also, make sure that, if you've
    > named your module "IndentedHelpFormatterWithNL.py" that created
    > your parser with
    >
    > parser = OptionParser(...
    > formatter=
    > IndentedHelpFormatterWithNL.IndentedHelpFormatterWithNL
    > )
    >
    > You'll also want to make sure that these two lines:
    >
    > from optparse import IndentedHelpFormatter
    > import textwrap
    >
    > are at the top of the IndentedHelpFormatterWithNL.py file, not at
    > the top of the file importing the IndentedHelpFormatter.
    >
    > -tkc


    That seems to do the trick. I'm sure there are quite a few people out
    there who have run into this same problem-- I'm glad you're adding it
    to the patches!
     
    John, Nov 8, 2007
    #8
    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. =?Utf-8?B?V2ViTWF0cml4?=

    Newline & carriage return characters over SOAP HTTP

    =?Utf-8?B?V2ViTWF0cml4?=, Mar 13, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    708
    =?Utf-8?B?V2ViTWF0cml4?=
    Mar 13, 2006
  2. WebMatrix

    Newline & carriage return charecters over SOAP HTTP

    WebMatrix, Mar 10, 2006, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    159
    WebMatrix
    Mar 10, 2006
  3. Xeno Campanoli
    Replies:
    0
    Views:
    246
    Xeno Campanoli
    Feb 13, 2006
  4. Steve Anderson
    Replies:
    3
    Views:
    277
    Steve Anderson
    Jun 21, 2004
  5. vflyer
    Replies:
    5
    Views:
    164
    scripts.contact
    Jun 1, 2007
Loading...

Share This Page