Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #5263
| Date | 2011-05-12 22:40 +0200 |
|---|---|
| From | Karim <karim.liateni@free.fr> |
| Subject | Re: Customize help output from optparse (or argparse) |
| References | <MPG.2836013fca77f958989809@news.individual.de> <2635961.LZWGnKmheA@PointedEars.de> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.1496.1305232809.9059.python-list@python.org> (permalink) |
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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Customize help output from optparse (or argparse) Thorsten Kampe <thorsten@thorstenkampe.de> - 2011-05-12 15:38 +0200
Re: Customize help output from optparse (or argparse) Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-05-12 22:22 +0200
Re: Customize help output from optparse (or argparse) Karim <karim.liateni@free.fr> - 2011-05-12 22:40 +0200
Re: Customize help output from optparse (or argparse) Thorsten Kampe <thorsten@thorstenkampe.de> - 2011-05-21 23:53 +0200
csiph-web