How can I require my python script's argument to be a float between 0.0-1.0 using argparse?
The type
parameter to add_argument
just needs to be a callable object that takes a string and returns a converted value. You can write a wrapper around float
that checks its value and raises an error if it is out of range.
def restricted_float(x):
try:
x = float(x)
except ValueError:
raise argparse.ArgumentTypeError("%r not a floating-point literal" % (x,))
if x < 0.0 or x > 1.0:
raise argparse.ArgumentTypeError("%r not in range [0.0, 1.0]"%(x,))
return x
p = argparse.ArgumentParser()
p.add_argument("--arg", type=restricted_float)
Here is a method that uses the choices
parameter to add_argument
, with a custom class that is considered "equal" to any float within the specified range:
import argparse
class Range(object):
def __init__(self, start, end):
self.start = start
self.end = end
def __eq__(self, other):
return self.start <= other <= self.end
parser = argparse.ArgumentParser()
parser.add_argument('--foo', type=float, choices=[Range(0.0, 1.0)])
Adding str makes that the boundaries are visuable in the help.
import argparse
class Range(object):
def __init__(self, start, end):
self.start = start
self.end = end
def __eq__(self, other):
return self.start <= other <= self.end
def __contains__(self, item):
return self.__eq__(item)
def __iter__(self):
yield self
def __str__(self):
return '[{0},{1}]'.format(self.start, self.end)
parser = argparse.ArgumentParser()
parser.add_argument('--foo', type=float, choices=Range(0.0, 1.0))
parser.add_argument('--bar', type=float, choices=[Range(0.0, 1.0), Range(2.0,3.0)])