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'