Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!news.tele.dk!feed118.news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!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; 'output': 0.05; 'great.': 0.07; 'modify': 0.07; 'none:': 0.07; 'see.': 0.07; 'logic': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'used.': 0.09; 'python': 0.11; 'def': 0.12; 'action):': 0.16; 'formatted': 0.16; 'kern': 0.16; 'patches': 0.16; 'perfect.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'subject:argparse': 0.16; 'sure.': 0.16; 'wrote:': 0.18; 'options.': 0.19; 'subject:request': 0.19; 'seems': 0.21; '>>>': 0.22; 'header:User-Agent:1': 0.23; 'skip:_ 20': 0.27; 'header:X -Complaints-To:1': 0.27; 'idea': 0.28; "doesn't": 0.30; 'robert': 0.30; 'code': 0.31; 'that.': 0.31; 'usually': 0.31; '>>>>': 0.31; 'produces': 0.31; 'values.': 0.31; 'class': 0.32; 'thanks!': 0.32; 'option': 0.32; 'url:python': 0.33; 'guess': 0.33; 'skip:_ 10': 0.34; "i'd": 0.34; 'problem': 0.35; 'something': 0.35; 'but': 0.35; 'add': 0.35; 'false': 0.36; 'method': 0.36; 'useful': 0.36; 'url:org': 0.36; 'implement': 0.38; 'url:library': 0.38; 'to:addr :python-list': 0.38; 'anything': 0.39; 'does': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'is.': 0.60; 'skip:f 50': 0.65; 'default': 0.69; '<<<': 0.84; "it'd": 0.84; 'received:139': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Neal Becker Subject: Re: argparse feature request Date: Fri, 22 Nov 2013 13:15:34 -0500 References: Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: exa2-in-fw-01-epn.hns.com User-Agent: KNode/4.11.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 56 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1385144149 news.xs4all.nl 15898 [2001:888:2000:d::a6]:52350 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:60247 Robert Kern wrote: > On 2013-11-22 16:52, Neal Becker wrote: >> Robert Kern wrote: >> >>> On 2013-11-22 14:56, Neal Becker wrote: >>>> I use arparse all the time and find it serves my needs well. One thing I'd >>>> like >>>> to see. In the help message, I'd like to automatically add the default >>>> values. > >>>> What I'd like to see is: >>>> >>>> --size SIZE [2000] <<< the default value is displayed >>> >>> Use formatter_class=argparse.ArgumentDefaultsHelpFormatter >>> >>> >>> http://docs.python.org/2/library/argparse#argparse.ArgumentDefaultsHelpFormatter > >> Thanks! Almost perfect. Problem is, I don't usually bother to add >> help='help >> me' options. It seems that ArgumentDefaultsHelpFormatter doesn't do anything >> unless help='blah' option is used. Not sure what to do about that. Since >> python test_freq3.py -h >> produces useful messages without my adding help=... everywhere, it'd be nice >> if ArgumentDefaultsHelpFormatter would work here. > > Patches are welcome, I am sure. Implement a HelpFormatter that does what you > want. _format_action() is where the relevant logic is. Try something like > this, and modify to suit. > > > class BeckerDefaultFormatter(argparse.ArgumentDefaultsHelpFormatter): > > def _format_action(self, action): > monkeypatched = False > if action.default is not None and action.help is None: > # Trick the default _format_action() method into writing out > # the defaults. > action.help = ' ' > monkeypatched = True > formatted = super(BeckerDefaultFormatter, > self)._format_action(action) if monkeypatched: > action.help = None > return formatted > Thanks! Seems to work great. It gave reasonable output for both case where I include help=... and also without. I have no idea how that above code works, but I guess as long as it works...