Skip to content

Commit 1d37aa0

Browse files
committed
Display env vars for pip options in docs
Ref pypa#8044
1 parent 51afe45 commit 1d37aa0

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

docs/pip_sphinxext.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,20 @@
1313
from sphinx.application import Sphinx
1414

1515
from pip._internal.cli import cmdoptions
16+
from pip._internal.configuration import _normalize_name
1617
from pip._internal.commands import commands_dict, create_command
1718
from pip._internal.req.req_file import SUPPORTED_OPTIONS
1819

20+
def convert_cli_option_to_envvar(opt_name):
21+
undashed_opt_name = _normalize_name(opt_name)
22+
normalized_opt_name = undashed_opt_name.upper().replace("-", "_")
23+
return f"PIP_{normalized_opt_name}"
24+
25+
def convert_cli_opt_names_to_envvars(original_cli_opt_names):
26+
return [
27+
convert_cli_option_to_envvar(opt_name)
28+
for opt_name in original_cli_opt_names
29+
]
1930

2031
class PipNewsInclude(rst.Directive):
2132
required_arguments = 1
@@ -130,7 +141,9 @@ def _format_option(
130141
opt_help = option.help.replace("%default", str(option.default))
131142
# fix paths with sys.prefix
132143
opt_help = opt_help.replace(sys.prefix, "<sys.prefix>")
133-
return [bookmark_line, "", line, "", " " + opt_help, ""]
144+
env_var_names = convert_cli_opt_names_to_envvars(option._long_opts)
145+
env_var_names_src = ", ".join(f"``{env_var}``" for env_var in env_var_names)
146+
return [bookmark_line, "", line, "", " " + opt_help, "", f" (environment variable: {env_var_names_src})", ""]
134147

135148
def _format_options(
136149
self, options: Iterable[optparse.Option], cmd_name: Optional[str] = None

0 commit comments

Comments
 (0)