Quick extraction of a parameter from the command line argument list.

    In some cases we need to parse a few arguments before the official
    arg-parser starts.

    Returns parameter value, or None if not present.

def param_extract(args, short_form, long_form, default=None):
    """
    Quick extraction of a parameter from the command line argument list.

    In some cases we need to parse a few arguments before the official
    arg-parser starts.

    Returns parameter value, or None if not present.

    """
    val = default
    for i, a in enumerate(args):
        # Long form may use "--xyz=foo", so need to split on '=', but it
        # doesn't necessarily do that, can also be "--xyz foo".
        elems = a.split("=", 1)
        if elems[0] in [short_form, long_form]:
            # At least make sure that an actual name was specified
            if len(elems) == 1:
                if i + 1 < len(args) and not args[i + 1].startswith("-"):
                    val = args[i + 1]
                else:
                    val = ""  # Invalid value was specified
            else:
                val = elems[1]
            break

    return val