def sanitiseList(parser, vals, img, arg):
    """Make sure that ``vals`` has the same number of elements as ``img`` has
    dimensions. Used to sanitise the ``--shape`` and ``--dim`` options.
    """
    if vals is None:
        return vals
    nvals = len(vals)
    if nvals < 3:
        parser.error('At least three values are required for {}'.format(arg))
    if nvals > img.ndim:
        parser.error(
            'Input only has {} dimensions - too many values specified for {}'
            .format(img.ndim, arg))
    if nvals < img.ndim:
        vals = list(vals) + list(img.shape[nvals:])
    return vals
