from argparse import ArgumentParser, ArgumentTypeError
import re

def parseNumList(string):
    m = re.match('(\\d+)(?:-(\\d+))?$', string)
if not m:
    raise ArgumentTypeError("'" + string +
        "' is not a range of number. Expected forms like '0-5' or '2'.")
start = m.group(1)
end = m.group(2) or start
return list(range(int(start, 10), int(end, 10) + 1))
parser = ArgumentParser()
parser.add_argument('--range', type=parseNumList)
args = parser.parse_args()
print(args)
Accept a range of numbers in the form of 0-5 using Python's argparse?