import click


def soft_nprocs(soft, nprocs):
    """Reduce the number of ranks to the largest acceptable soft value"""
    # If no soft specification given, use -n value
    if not soft:
        return nprocs

    # Filter to values between 1 and nprocs
    try:
        return max([x for x in soft if 0 < x <= nprocs])
    except ValueError:
        # pylint: disable=raise-missing-from
        raise click.UsageError("No soft values found between 1 and %d" % nprocs)