Path: csiph.com!usenet.pasdenom.info!news.etla.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed1.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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '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; 'underlying': 0.16; 'wrote:': 0.18; 'options.': 0.19; 'subject:request': 0.19; 'seems': 0.21; '>>>': 0.22; 'header :User-Agent:1': 0.23; 'interpret': 0.24; 'skip:_ 20': 0.27; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; "doesn't": 0.30; 'robert': 0.30; 'that.': 0.31; 'usually': 0.31; 'produces': 0.31; 'values.': 0.31; 'class': 0.32; 'thanks!': 0.32; 'option': 0.32; 'url:python': 0.33; 'skip:_ 10': 0.34; "i'd": 0.34; 'problem': 0.35; 'something': 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; 'is.': 0.60; 'our': 0.64; 'skip:f 50': 0.65; 'world': 0.66; 'believe': 0.68; 'default': 0.69; '<<<': 0.84; 'eco': 0.84; "it'd": 0.84; 'terrible': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Robert Kern Subject: Re: argparse feature request Date: Fri, 22 Nov 2013 17:33:19 +0000 References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: 213.1.240.226 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:24.0) Gecko/20100101 Thunderbird/24.1.1 In-Reply-To: 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: 52 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1385141612 news.xs4all.nl 15949 [2001:888:2000:d::a6]:38443 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:60244 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 -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco