Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #60251

Re: argparse feature request

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 <python-python-list@m.gmane.org>
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; '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; '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; '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; 'figure': 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; 'there': 0.35; 'really': 0.36; '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; 'took': 0.61; 'first': 0.61; 'our': 0.64; 'skip:f 50': 0.65; 'world': 0.66; 'minutes': 0.67; 'believe': 0.68; 'default': 0.69; '<<<': 0.84; 'eco': 0.84; "it'd": 0.84; 'terrible': 0.84; 'tricky': 0.84; 'received:86': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Robert Kern <robert.kern@gmail.com>
Subject Re: argparse feature request
Date Fri, 22 Nov 2013 21:10:24 +0000
References <l6nrar$fss$1@ger.gmane.org> <l6nrrv$olk$1@ger.gmane.org> <l6o23q$96u$1@ger.gmane.org> <l6o4gk$76b$1@ger.gmane.org> <l6o6vs$5ip$1@ger.gmane.org>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host cpc2-cmbg17-2-0-cust347.5-4.cable.virginm.net
User-Agent Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:24.0) Gecko/20100101 Thunderbird/24.1.1
In-Reply-To <l6o6vs$5ip$1@ger.gmane.org>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3056.1385154637.18130.python-list@python.org> (permalink)
Lines 70
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1385154637 news.xs4all.nl 15984 [2001:888:2000:d::a6]:43196
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:60251

Show key headers only | View raw


On 2013-11-22 18:15, Neal Becker wrote:
> 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...

Just take a look at the implementation of HelpFormatter._format_action() and 
look for where my monkeypatch would change the logic. It took me just a few 
minutes to figure out how to do it in the first place. There really isn't 
anything tricky going on.

-- 
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

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: argparse feature request Robert Kern <robert.kern@gmail.com> - 2013-11-22 21:10 +0000

csiph-web