How can I constrain a value parsed with argparse (for example, restrict an integer to positive values)?
Question:
I have this code so far:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-g", "--games", type=int, default=162,
help="The number of games to simulate")
args = parser.parse_args()
It does not make sense to supply a negative value for the number of games, but type=int
allows any integer. For example, if I run python simulate_many.py -g -2
, args.games
will be set to -2
and the program will continue as if nothing is wrong.
I realize that I could just explicit check the value of args.games
after parsing arguments. But can I make argparse
itself check this condition? How?
I would prefer it to work that way so that the automatic usage message can explain the requirement to the user. Ideally, the output would look something like:
python simulate_many.py -g -2
usage: simulate_many.py [-h] [-g GAMES] [-d] [-l LEAGUE]
simulate_many.py: error: argument -g/--games: invalid positive int value: '-2'
just as it currently handles arguments that can’t be converted to integer:
python simulate_many.py -g a
usage: simulate_many.py [-h] [-g GAMES] [-d] [-l LEAGUE]
simulate_many.py: error: argument -g/--games: invalid int value: 'a'
Answers:
This should be possible utilizing type
. You’ll still need to define an actual method that decides this for you:
def check_positive(value):
ivalue = int(value)
if ivalue <= 0:
raise argparse.ArgumentTypeError("%s is an invalid positive int value" % value)
return ivalue
parser = argparse.ArgumentParser(...)
parser.add_argument('foo', type=check_positive)
This is basically just an adapted example from the perfect_square
function in the docs on argparse
.
The quick and dirty way, if you have a predictable max as well as min for your arg, is use choices
with a range
parser.add_argument('foo', type=int, choices=xrange(0, 1000))
type
would be the recommended option to handle conditions/checks, as in Yuushi’s answer.
In your specific case, you can also use the choices
parameter if your upper limit is also known:
parser.add_argument('foo', type=int, choices=xrange(5, 10))
Note: Use range
instead of xrange
for python 3.x
A simpler alternative, especially if subclassing argparse.ArgumentParser
, is to initiate the validation from inside the parse_args
method.
Inside such a subclass:
def parse_args(self, args=None, namespace=None):
"""Parse and validate args."""
namespace = super().parse_args(args, namespace)
if namespace.games <= 0:
raise self.error('The number of games must be a positive integer.')
return namespace
This technique may not be as cool as a custom callable, but it does the job.
About ArgumentParser.error(message)
:
This method prints a usage message including the message to the standard error and terminates the program with a status code of 2.
Credit: answer by jonatan
In case someone (like me) comes across this question in a Google search, here is an example of how to use a modular approach to neatly solve the more general problem of allowing argparse integers only in a specified range:
# Custom argparse type representing a bounded int
class IntRange:
def __init__(self, imin=None, imax=None):
self.imin = imin
self.imax = imax
def __call__(self, arg):
try:
value = int(arg)
except ValueError:
raise self.exception()
if (self.imin is not None and value < self.imin) or (self.imax is not None and value > self.imax):
raise self.exception()
return value
def exception(self):
if self.imin is not None and self.imax is not None:
return argparse.ArgumentTypeError(f"Must be an integer in the range [{self.imin}, {self.imax}]")
elif self.imin is not None:
return argparse.ArgumentTypeError(f"Must be an integer >= {self.imin}")
elif self.imax is not None:
return argparse.ArgumentTypeError(f"Must be an integer <= {self.imax}")
else:
return argparse.ArgumentTypeError("Must be an integer")
This allows you to do something like:
parser = argparse.ArgumentParser(...)
parser.add_argument('foo', type=IntRange(1)) # Must have foo >= 1
parser.add_argument('bar', type=IntRange(1, 7)) # Must have 1 <= bar <= 7
The variable foo
now allows only positive integers, like the OP asked.
Note that in addition to the above forms, just a maximum is also possible with IntRange
:
parser.add_argument('other', type=IntRange(imax=10)) # Must have other <= 10
Based on Yuushi’s answer, you can also define a simple helper function that can check if a number is positive for various numeric types:
def positive(numeric_type):
def require_positive(value):
number = numeric_type(value)
if number <= 0:
raise ArgumentTypeError(f"Number {value} must be positive.")
return number
return require_positive
The helper function can be used to annotate any numeric argument type like this:
parser = argparse.ArgumentParser(...)
parser.add_argument("positive-integer", type=positive(int))
parser.add_argument("positive-float", type=positive(float))
Honor Yuushi’s answer:
# helper
import argparse
def make_range_checker(lb, ub):
flb, fub = float(lb), float(ub)
def checker(val):
val = int(val)
if not (flb <= val <= fub):
# error type must be correct
# so argparse module catches the error
raise argparse.ArgumentTypeError(
f'value out of scope: {lb} - {ub}')
return val
return checker
# usage
parser.add_argument("--sleep",
default=5,
type=make_range_checker(1, 'inf'),
help="sleep time in sec, must be postive int")
I have this code so far:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-g", "--games", type=int, default=162,
help="The number of games to simulate")
args = parser.parse_args()
It does not make sense to supply a negative value for the number of games, but type=int
allows any integer. For example, if I run python simulate_many.py -g -2
, args.games
will be set to -2
and the program will continue as if nothing is wrong.
I realize that I could just explicit check the value of args.games
after parsing arguments. But can I make argparse
itself check this condition? How?
I would prefer it to work that way so that the automatic usage message can explain the requirement to the user. Ideally, the output would look something like:
python simulate_many.py -g -2
usage: simulate_many.py [-h] [-g GAMES] [-d] [-l LEAGUE]
simulate_many.py: error: argument -g/--games: invalid positive int value: '-2'
just as it currently handles arguments that can’t be converted to integer:
python simulate_many.py -g a
usage: simulate_many.py [-h] [-g GAMES] [-d] [-l LEAGUE]
simulate_many.py: error: argument -g/--games: invalid int value: 'a'
This should be possible utilizing type
. You’ll still need to define an actual method that decides this for you:
def check_positive(value):
ivalue = int(value)
if ivalue <= 0:
raise argparse.ArgumentTypeError("%s is an invalid positive int value" % value)
return ivalue
parser = argparse.ArgumentParser(...)
parser.add_argument('foo', type=check_positive)
This is basically just an adapted example from the perfect_square
function in the docs on argparse
.
The quick and dirty way, if you have a predictable max as well as min for your arg, is use choices
with a range
parser.add_argument('foo', type=int, choices=xrange(0, 1000))
type
would be the recommended option to handle conditions/checks, as in Yuushi’s answer.
In your specific case, you can also use the choices
parameter if your upper limit is also known:
parser.add_argument('foo', type=int, choices=xrange(5, 10))
Note: Use range
instead of xrange
for python 3.x
A simpler alternative, especially if subclassing argparse.ArgumentParser
, is to initiate the validation from inside the parse_args
method.
Inside such a subclass:
def parse_args(self, args=None, namespace=None):
"""Parse and validate args."""
namespace = super().parse_args(args, namespace)
if namespace.games <= 0:
raise self.error('The number of games must be a positive integer.')
return namespace
This technique may not be as cool as a custom callable, but it does the job.
About ArgumentParser.error(message)
:
This method prints a usage message including the message to the standard error and terminates the program with a status code of 2.
Credit: answer by jonatan
In case someone (like me) comes across this question in a Google search, here is an example of how to use a modular approach to neatly solve the more general problem of allowing argparse integers only in a specified range:
# Custom argparse type representing a bounded int
class IntRange:
def __init__(self, imin=None, imax=None):
self.imin = imin
self.imax = imax
def __call__(self, arg):
try:
value = int(arg)
except ValueError:
raise self.exception()
if (self.imin is not None and value < self.imin) or (self.imax is not None and value > self.imax):
raise self.exception()
return value
def exception(self):
if self.imin is not None and self.imax is not None:
return argparse.ArgumentTypeError(f"Must be an integer in the range [{self.imin}, {self.imax}]")
elif self.imin is not None:
return argparse.ArgumentTypeError(f"Must be an integer >= {self.imin}")
elif self.imax is not None:
return argparse.ArgumentTypeError(f"Must be an integer <= {self.imax}")
else:
return argparse.ArgumentTypeError("Must be an integer")
This allows you to do something like:
parser = argparse.ArgumentParser(...)
parser.add_argument('foo', type=IntRange(1)) # Must have foo >= 1
parser.add_argument('bar', type=IntRange(1, 7)) # Must have 1 <= bar <= 7
The variable foo
now allows only positive integers, like the OP asked.
Note that in addition to the above forms, just a maximum is also possible with IntRange
:
parser.add_argument('other', type=IntRange(imax=10)) # Must have other <= 10
Based on Yuushi’s answer, you can also define a simple helper function that can check if a number is positive for various numeric types:
def positive(numeric_type):
def require_positive(value):
number = numeric_type(value)
if number <= 0:
raise ArgumentTypeError(f"Number {value} must be positive.")
return number
return require_positive
The helper function can be used to annotate any numeric argument type like this:
parser = argparse.ArgumentParser(...)
parser.add_argument("positive-integer", type=positive(int))
parser.add_argument("positive-float", type=positive(float))
Honor Yuushi’s answer:
# helper
import argparse
def make_range_checker(lb, ub):
flb, fub = float(lb), float(ub)
def checker(val):
val = int(val)
if not (flb <= val <= fub):
# error type must be correct
# so argparse module catches the error
raise argparse.ArgumentTypeError(
f'value out of scope: {lb} - {ub}')
return val
return checker
# usage
parser.add_argument("--sleep",
default=5,
type=make_range_checker(1, 'inf'),
help="sleep time in sec, must be postive int")