import click


def parse_rangelist(rli):
    """Parse a range list into a list of integers"""
    try:
        mylist = []
        for nidrange in rli.split(","):
            startstr, sep, endstr = nidrange.partition("-")
            start = int(startstr, 0)
            if sep:
                end = int(endstr, 0)
                if end < start:
                    mylist.extend(range(start, end - 1, -1))
                else:
                    mylist.extend(range(start, end + 1))
            else:
                mylist.append(start)
    except ValueError:
        # pylint: disable=raise-missing-from
        raise click.ClickException("Invalid range list %s" % rli)

    return mylist