Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!news2.arglkargh.de!news.wiretrip.org!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'parser': 0.05; 'default,': 0.07; 'defaults': 0.07; 'formatting': 0.07; 'though.': 0.07; 'python': 0.07; '"""': 0.09; 'deprecated': 0.09; 'optparse': 0.09; 'configure': 0.11; 'pm,': 0.11; 'output': 0.12; 'error:': 0.14; 'wrote:': 0.14; '"new': 0.16; '--help': 0.16; '-h,': 0.16; 'argparse': 0.16; 'constructor.': 0.16; 'from:addr:free.fr': 0.16; 'last,': 0.16; 'module:': 0.16; 'received:212.27': 0.16; 'received:212.27.42': 0.16; 'received:free.fr': 0.16; 'subclassing': 0.16; 'url:devel': 0.16; 'url:tools': 0.16; 'argument': 0.16; 'subject:help': 0.19; 'usage': 0.20; 'seems': 0.21; 'help.': 0.22; 'header:In-Reply-To:1': 0.22; 'additionally': 0.23; 'script.': 0.23; "i'm": 0.26; 'classes': 0.26; 'pass': 0.27; 'error': 0.29; 'class': 0.29; 'exit': 0.29; 'option:': 0.31; 'does': 0.31; "skip:' 10": 0.32; 'to:addr:python-list': 0.32; 'using': 0.34; 'received:192': 0.34; 'skip:" 10': 0.34; 'change': 0.34; 'received:192.168.0': 0.35; 'file': 0.35; 'print': 0.35; 'header:User-Agent:1': 0.35; 'options,': 0.35; 'options:': 0.35; 'error.': 0.36; 'think': 0.36; 'received:192.168': 0.37; 'skip:- 10': 0.37; 'either': 0.37; 'but': 0.38; 'help': 0.39; 'adds': 0.39; 'to:addr:python.org': 0.39; 'subject: (': 0.39; 'would': 0.40; 'might': 0.40; '8bit%:14': 0.60; 'favor': 0.64; 'show': 0.67; 'details:': 0.72; '10:22': 0.84; '______': 0.84; 'url:text': 0.91 Date: Thu, 12 May 2011 22:40:01 +0200 From: Karim User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.17) Gecko/20110424 Thunderbird/3.1.10 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Customize help output from optparse (or argparse) References: <2635961.LZWGnKmheA@PointedEars.de> In-Reply-To: <2635961.LZWGnKmheA@PointedEars.de> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 75 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1305232810 news.xs4all.nl 81483 [::ffff:82.94.164.166]:54104 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:5263 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 >> >> 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 >> >> Options: >> -h, --help show this help message and exit >> """ >> >> 2. The output from "--doesnotexit" is: >> """ >> Usage: script.py >> >> 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 >> >> 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. > > ______ > ¹ 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 /lib/python2.7/argparse.py Cheers Karim