Customize help output from optparse (or argparse)
Thomas 'PointedEars' Lahn
PointedEars at web.de
Thu May 12 16:22:20 EDT 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.
More information about the Python-list
mailing list