["python","argparse"]
How can I constrain a value parsed with argparse (for example, restrict an integer to positive values)?

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.