You can pass any conversion function as type=arg add_argument. Use your own conversion function, which includes additional checks.
def non_negative_int(x):
i = int(x)
if i < 0:
raise ValueError('Negative values are not allowed')
return i
parser.add_argument("-t", "--time",
default=2, type=non_negative_int,
help="Settings up the resolution time")
source
share