Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Customize help output from optparse (or argparse)

Reply
Thread Tools

Customize help output from optparse (or argparse)

 
 
Thorsten Kampe
Guest
Posts: n/a
 
      05-12-2011
Hi,

I'm using optparse for a little Python script.

1. The output from "--help" is:
"""
Usage: script.py <arg>

script.py does something

Options:
-h, --help show this help message and exit
"""

I would prefer to have the description before the usage, like...
"""
script.py does something

Usage: script.py <arg>

Options:
-h, --help show this help message and exit
"""

2. The output from "--doesnotexit" is:
"""
Usage: script.py <arg>

script.py: error: no such option: --doesnotexist
"""

I would prefer to have the error first, then the usage and additionally
the options, like...
"""
script.py: error: no such option: --doesnotexist

Usage: script.py <arg>

Options:
-h, --help show this help message and exit
"""

Is that possible with either optparse or the "new kid on the block"
argparse. If so how?

Thorsten
 
Reply With Quote
 
 
 
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      05-12-2011
Thorsten Kampe wrote:

> I'm using optparse for a little Python script.
>
> 1. The output from "--help" is:
> """
> Usage: script.py <arg>
>
> script.py does something
>
> Options:
> -h, --help show this help message and exit
> """
>
> I would prefer to have the description before the usage, like...
> """
> script.py does something
>
> Usage: script.py <arg>
>
> Options:
> -h, --help show this help message and exit
> """
>
> 2. The output from "--doesnotexit" is:
> """
> Usage: script.py <arg>
>
> script.py: error: no such option: --doesnotexist
> """
>
> I would prefer to have the error first, then the usage and additionally
> the options, like...
> """
> script.py: error: no such option: --doesnotexist
>
> Usage: script.py <arg>
>
> Options:
> -h, --help show this help message and exit
> """
>
> Is that possible with either optparse or the "new kid on the block"
> argparse. If so how?


You can easily have #1 with optparse.OptionParser(usage="…")¹, but optparse
is deprecated in favor of argparse.ArgumentParser. I do not think you can
have #2 with either optparse or argparse: OptionParser() would print the
error message last, and ArgumentParser() would not print the description
on error. Subclassing ArgumentParser might be feasible, though.

______
¹ <http://PointedEars.de/devel/tools/text/odfinfo/>
--
PointedEars

Bitte keine Kopien per E-Mail. / Please do not Cc: me.
 
Reply With Quote
 
 
 
 
Karim
Guest
Posts: n/a
 
      05-12-2011
On 05/12/2011 10:22 PM, Thomas 'PointedEars' Lahn wrote:
> Thorsten Kampe wrote:
>
>> I'm using optparse for a little Python script.
>>
>> 1. The output from "--help" is:
>> """
>> Usage: script.py<arg>
>>
>> script.py does something
>>
>> Options:
>> -h, --help show this help message and exit
>> """
>>
>> I would prefer to have the description before the usage, like...
>> """
>> script.py does something
>>
>> Usage: script.py<arg>
>>
>> Options:
>> -h, --help show this help message and exit
>> """
>>
>> 2. The output from "--doesnotexit" is:
>> """
>> Usage: script.py<arg>
>>
>> script.py: error: no such option: --doesnotexist
>> """
>>
>> I would prefer to have the error first, then the usage and additionally
>> the options, like...
>> """
>> script.py: error: no such option: --doesnotexist
>>
>> Usage: script.py<arg>
>>
>> Options:
>> -h, --help show this help message and exit
>> """
>>
>> Is that possible with either optparse or the "new kid on the block"
>> argparse. If so how?

> You can easily have #1 with optparse.OptionParser(usage="…")¹, but optparse
> is deprecated in favor of argparse.ArgumentParser. I do not think you can
> have #2 with either optparse or argparse: OptionParser() would print the
> error message last, and ArgumentParser() would not print the description
> on error. Subclassing ArgumentParser might be feasible, though.
>
> ______
> ¹<http://PointedEars.de/devel/tools/text/odfinfo/>


Please find documentation to configure help in ArgumentParser BUT for
argparse module:

- HelpFormatter, RawDescriptionHelpFormatter, RawTextHelpFormatter,
50 ArgumentDefaultsHelpFormatter -- Formatter classes which
51 may be passed as the formatter_class= argument to the
52 ArgumentParser constructor. HelpFormatter is the default,
53 RawDescriptionHelpFormatter and RawTextHelpFormatter tell
the parser
54 not to change the formatting for help text, and
55 ArgumentDefaultsHelpFormatter adds information about
argument defaults
56 to the help.

So It seems easy to a different pass formatter_class to ArgumentParser.
You can inherite
from HelpFormater class but you have to know the implementation details:
File is located at <python install>/lib/python2.7/argparse.py

Cheers
Karim
 
Reply With Quote
 
Thorsten Kampe
Guest
Posts: n/a
 
      05-21-2011
* Thomas 'PointedEars' Lahn (Thu, 12 May 2011 22:22:20 +0200)
> Thorsten Kampe wrote:
> > I'm using optparse for a little Python script.
> >
> > 1. The output from "--help" is:
> > """
> > Usage: script.py <arg>
> >
> > script.py does something
> >
> > Options:
> > -h, --help show this help message and exit
> > """
> >
> > I would prefer to have the description before the usage, like...
> > """
> > script.py does something
> >
> > Usage: script.py <arg>
> >
> > Options:
> > -h, --help show this help message and exit
> > """
> > [...]
> > Is that possible with either optparse or the "new kid on the block"
> > argparse. If so how?

>
> You can easily have #1 with optparse.OptionParser(usage="…")¹, but optparse
> is deprecated in favor of argparse.ArgumentParser.


I'm already using usage. That's where optparse has it from. Putting the
usage message into the description and vice versa is of course not a
viable way to go.

Thorsten
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"docbook -> pdf" - customize author-output tthunder@gmx.de XML 1 01-20-2008 10:47 PM
How to output newline or carriage return with optparse john.m.roach@gmail.com Python 7 11-08-2007 08:07 PM
optparse help output Dan Python 3 10-24-2007 06:24 PM
[JSF][Java Studio Creator] Format output and customize display FD Java 0 08-25-2005 06:28 PM
[Q] Howto customize output from java.util.logging API in Java 1.4 Michael T. Peterson Java 2 09-09-2003 06:14 PM



Advertisments