argparse feature request

Neal Becker ndbecker2 at gmail.com
Fri Nov 22 11:52:20 EST 2013


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.
>>
>> For example, here's one of my programs:
>>
>>   python3 test_freq3.py --help
>> usage: test_freq3.py [-h] [--size SIZE] [--esnodB ESNODB] [--tau TAU]
>> [--trials TRIALS]
>>                       [--training TRAINING] [--sps SPS] [--si SI] [--alpha
>>                       [ALPHA] --range RANGE] [--dfunc {gradient,delay}]
>>                       [--mod
>> {gaussian,qpsk,8psk,16apsk,32apsk,32dlr,64apsk,256apsk}]
>>                       [--sym-freq-err SYM_FREQ_ERR] [--calibrate [CALIBRATE]]
>>
>> optional arguments:
>>    -h, --help            show this help message and exit
>>    --size SIZE
>>    --esnodB ESNODB, -e ESNODB
>>    --tau TAU, -t TAU
>>    --trials TRIALS
>>    --training TRAINING
>>    --sps SPS
>>    --si SI
>>    --alpha ALPHA
>>    --range RANGE
>>    --dfunc {gradient,delay}
>>    --mod {gaussian,qpsk,8psk,16apsk,32apsk,32dlr,64apsk,256apsk}
>>    --sym-freq-err SYM_FREQ_ERR
>>    --calibrate [CALIBRATE], --with-calibrate [CALIBRATE], --enable-calibrate
>> [CALIBRATE], --no-calibrate [CALIBRATE], --without-calibrate [CALIBRATE], --
>> disable-calibrate [CALIBRATE]
>>
>> 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
> 
> E.g.
> 
> [git/mpstack]$ cat print_stacks.py
> ...
> def main():
>      import argparse
>      parser = argparse.ArgumentParser(
>          formatter_class=argparse.ArgumentDefaultsHelpFormatter)
>      parser.add_argument('-p', '--percent', action='store_true', help='Show
> percentages.')
>      parser.add_argument('file', help='The sample file.')
> ...
> 
> [git/mpstack]$ python print_stacks.py -h
> usage: print_stacks.py [-h] [-p] file
> 
> positional arguments:
>    file           The sample file.
> 
> optional arguments:
>    -h, --help     show this help message and exit
>    -p, --percent  Show percentages. (default: False)
> 

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.




More information about the Python-list mailing list