.. versionadded:: 0.17.0

    Returns the version of pip. Use ``bin_env`` to specify the path to a
    virtualenv and get the version of pip in that virtualenv.

    If unable to detect the pip version, returns ``None``.

    CLI Example:

    .. code-block:: bash

        salt '*' pip.version

def version(bin_env=None):
    '''
    .. versionadded:: 0.17.0

    Returns the version of pip. Use ``bin_env`` to specify the path to a
    virtualenv and get the version of pip in that virtualenv.

    If unable to detect the pip version, returns ``None``.

    CLI Example:

    .. code-block:: bash

        salt '*' pip.version
    '''
    contextkey = 'pip.version'
    if bin_env is not None:
        contextkey = '{0}.{1}'.format(contextkey, bin_env)

    if contextkey in __context__:
        return __context__[contextkey]

    cmd = _get_pip_bin(bin_env)[:]
    cmd.append('--version')

    ret = __salt__['cmd.run_all'](cmd, python_shell=False)
    if ret['retcode']:
        raise CommandNotFoundError('Could not find a `pip` binary')

    try:
        pip_version = re.match(r'^pip (\S+)', ret['stdout']).group(1)
    except AttributeError:
        pip_version = None

    __context__[contextkey] = pip_version
    return pip_version