import attr


def attrib(*args, **kwargs):
    """Extend the attr.ib to include our metadata elements.

    ATM we support additional keyword args which are then stored within
    `metadata`:
    - `doc` for documentation to describe the attribute (e.g. in --help)

    Also, when the `default` argument of attr.ib is unspecified, set it to
    None.
    """
    doc = kwargs.pop('doc', None)
    metadata = kwargs.get('metadata', {})
    if doc:
        metadata['doc'] = doc
    if metadata:
        kwargs['metadata'] = metadata
    return attr.ib(*args, default=kwargs.pop('default', None), **kwargs)