Skip to content

Commit 303fed3

Browse files
authored
Merge pull request #12576 from luismedel/8044-docs-envvars
Display env vars for pip options in docs
2 parents 3682309 + 347fa24 commit 303fed3

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

docs/pip_sphinxext.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,22 @@
1414

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

1920

21+
def convert_cli_option_to_envvar(opt_name: str) -> str:
22+
undashed_opt_name = _normalize_name(opt_name)
23+
normalized_opt_name = undashed_opt_name.upper().replace("-", "_")
24+
return f"PIP_{normalized_opt_name}"
25+
26+
27+
def convert_cli_opt_names_to_envvars(original_cli_opt_names: List[str]) -> List[str]:
28+
return [
29+
convert_cli_option_to_envvar(opt_name) for opt_name in original_cli_opt_names
30+
]
31+
32+
2033
class PipNewsInclude(rst.Directive):
2134
required_arguments = 1
2235

@@ -130,7 +143,18 @@ def _format_option(
130143
opt_help = option.help.replace("%default", str(option.default))
131144
# fix paths with sys.prefix
132145
opt_help = opt_help.replace(sys.prefix, "<sys.prefix>")
133-
return [bookmark_line, "", line, "", " " + opt_help, ""]
146+
env_var_names = convert_cli_opt_names_to_envvars(option._long_opts)
147+
env_var_names_src = ", ".join(f"``{env_var}``" for env_var in env_var_names)
148+
return [
149+
bookmark_line,
150+
"",
151+
line,
152+
"",
153+
f" {opt_help}",
154+
"",
155+
f" (environment variable: {env_var_names_src})",
156+
"",
157+
]
134158

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

news/12576.doc.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Document the environment variables that correspond with CLI options.

0 commit comments

Comments
 (0)