[Tutor] using ranges with argparse()
Kurt Lieber
kurt+python-tutor at lieber.org
Mon Jan 28 07:16:13 CET 2013
Hi -- brand new to python, but trying to write a simple script that takes
command line arguments. One of the arguments needs to test if a value is a)
an integer and b) within a stated range. I currently have:
parser.add_argument("-f", "--floor", default=6000, help="floor is the
minimum amount of bonus points.",type=int, choices=range(5995, 6001))
This works, but when the user enters an out of bounds value, the help
message is unfriendly:
% ./bonus.py -f 10000
usage: bonus.py [-h] [-f {5995,5996,5997,5998,5999,6000}]
MAM-bonus.py: error: argument -f/--floor: invalid choice: 10000 (choose
from 5995, 5996, 5997, 5998, 5999, 6000)
The problem is my default range is actually 0,10000 -- I changed it above
for brevity's sake. So in the real world, it floods the screen to the point
where it's unreadable.
I can suppress the whole thing with argparse.SUPPRESS, but then I'm left
with no help message at all. Is there a way to suppress the "(choose from
1,2,3,etc.)" part of the help message? Or a cleaner/different way
altogether to accomplish the same thing?
thanks.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20130127/114e5d93/attachment.html>
More information about the Tutor
mailing list