def _extract_args(cmd_line, args):
    """
    Extract specified arguments and sorts them. Also removes them from the
    command line.

    :param cmd_line: Array of command line arguments.
    :param args: List of arguments to extract.

    :returns: A tuple. The first element is a list of key, value tuples representing each arguments.
        The second element is the cmd_line list without the arguments that were
        extracted.
    """
    # Find all instances of each argument in the command line
    found_args = []
    for cmd_line_token in cmd_line:
        for arg in args:
            if cmd_line_token.startswith("--%s=" % arg):
                found_args.append(cmd_line_token)

    # Strip all these arguments from the command line.
    new_cmd_line = [
        argument for argument in cmd_line if argument not in found_args
    ]

    arguments = []

    # Create a list of tuples for each argument we found.
    for arg in found_args:
        pos = arg.find("=")
        # Take everything after the -- up to but not including the =
        arg_name = arg[2:pos]
        arg_value = arg[pos + 1:]
        arguments.append((arg_name, arg_value))

    return arguments, new_cmd_line