Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #60251 > unrolled thread
| Started by | Robert Kern <robert.kern@gmail.com> |
|---|---|
| First post | 2013-11-22 21:10 +0000 |
| Last post | 2013-11-22 21:10 +0000 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: argparse feature request Robert Kern <robert.kern@gmail.com> - 2013-11-22 21:10 +0000
| From | Robert Kern <robert.kern@gmail.com> |
|---|---|
| Date | 2013-11-22 21:10 +0000 |
| Subject | Re: argparse feature request |
| Message-ID | <mailman.3056.1385154637.18130.python-list@python.org> |
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 top | Article view | comp.lang.python
csiph-web