def parse_options(arguments):
    """Parse command line arguments.

    The parsing logic is fairly simple.  It can only parse long-style
    parameters of the form::

      --key value

    Several parameters can be defined in the environment and will be used
    unless explicitly overridden with command-line arguments.  The access key,
    secret and endpoint values will be loaded from C{AWS_ACCESS_KEY_ID},
    C{AWS_SECRET_ACCESS_KEY} and C{AWS_ENDPOINT} environment variables.

    @param arguments: A list of command-line arguments.  The first item is
        expected to be the name of the program being run.
    @raises OptionError: Raised if incorrectly formed command-line arguments
        are specified, or if required command-line arguments are not present.
    @raises UsageError: Raised if C{--help} is present in command-line
        arguments.
    @return: A C{dict} with key/value pairs extracted from the argument list.
    """
    arguments = arguments[1:]
    options = {}
    while arguments:
        key = arguments.pop(0)
        if key in ("-h", "--help"):
            raise UsageError("Help requested.")
        if key.startswith("--"):
            key = key[2:]
            try:
                value = arguments.pop(0)
            except IndexError:
                raise OptionError("'--%s' is missing a value." % key)
            options[key] = value
        else:
            raise OptionError("Encountered unexpected value '%s'." % key)

    default_key = os.environ.get("AWS_ACCESS_KEY_ID")
    if "key" not in options and default_key:
        options["key"] = default_key
    default_secret = os.environ.get("AWS_SECRET_ACCESS_KEY")
    if "secret" not in options and default_secret:
        options["secret"] = default_secret
    default_endpoint = os.environ.get("AWS_ENDPOINT")
    if "endpoint" not in options and default_endpoint:
        options["endpoint"] = default_endpoint
    for name in ("key", "secret", "endpoint", "action"):
        if name not in options:
            raise OptionError(
                "The '--%s' command-line argument is required." % name)

    return options
Parse command line arguments.

    The parsing logic is fairly simple.  It can only parse long-style
    parameters of the form::

      --key value

    Several parameters can be defined in the environment and will be used
    unless explicitly overridden with command-line arguments.  The access key,
    secret and endpoint values will be loaded from C{AWS_ACCESS_KEY_ID},
    C{AWS_SECRET_ACCESS_KEY} and C{AWS_ENDPOINT} environment variables.

    @param arguments: A list of command-line arguments.  The first item is
        expected to be the name of the program being run.
    @raises OptionError: Raised if incorrectly formed command-line arguments
        are specified, or if required command-line arguments are not present.
    @raises UsageError: Raised if C{--help} is present in command-line
        arguments.
    @return: A C{dict} with key/value pairs extracted from the argument list.