Skip to content

Commit cd43345

Browse files
committed
Add colour to Python help
1 parent a8c9aa9 commit cd43345

File tree

4 files changed

+283
-114
lines changed

4 files changed

+283
-114
lines changed

Doc/whatsnew/3.15.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,11 @@ Other language changes
512512

513513
(Contributed by Adam Turner in :gh:`133711`; PEP 686 written by Inada Naoki.)
514514

515+
* The interpreter help (such as ``python --help``) is now in color.
516+
This can be controlled by :ref:`environment variables
517+
<using-on-controlling-color>`.
518+
(Contributed by Hugo van Kemenade in :gh:`148766`.)
519+
515520
* Unraisable exceptions are now highlighted with color by default. This can be
516521
controlled by :ref:`environment variables <using-on-controlling-color>`.
517522
(Contributed by Peter Bierma in :gh:`134170`.)

Lib/test/test_cmd_line.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ def verify_valid_flag(self, cmd_line):
5757
return out
5858

5959
@support.cpython_only
60+
@support.force_not_colorized
6061
def test_help(self):
6162
self.verify_valid_flag('-h')
6263
self.verify_valid_flag('-?')
@@ -68,6 +69,7 @@ def test_help(self):
6869
self.assertLess(len(lines), 50)
6970

7071
@support.cpython_only
72+
@support.force_not_colorized
7173
def test_help_env(self):
7274
out = self.verify_valid_flag('--help-env')
7375
self.assertIn(b'PYTHONHOME', out)
@@ -81,6 +83,7 @@ def test_help_env(self):
8183
"env vars should be sorted alphabetically")
8284

8385
@support.cpython_only
86+
@support.force_not_colorized
8487
def test_help_xoptions(self):
8588
out = self.verify_valid_flag('--help-xoptions')
8689
self.assertIn(b'-X dev', out)
@@ -89,6 +92,7 @@ def test_help_xoptions(self):
8992
"options should be sorted alphabetically")
9093

9194
@support.cpython_only
95+
@support.force_not_colorized
9296
def test_help_all(self):
9397
out = self.verify_valid_flag('--help-all')
9498
lines = out.splitlines()
@@ -100,6 +104,25 @@ def test_help_all(self):
100104
# but the rest should be ASCII-only
101105
b''.join(lines[1:]).decode('ascii')
102106

107+
@support.cpython_only
108+
@support.force_colorized
109+
def test_help_colorized(self):
110+
rc, out, err = assert_python_ok("--help", FORCE_COLOR="1")
111+
# Check ANSI color codes are present
112+
self.assertIn(b"\x1b[", out)
113+
# Check that key text elements are still present
114+
self.assertIn(b"usage:", out)
115+
self.assertIn(b"-h", out)
116+
self.assertIn(b"--help-all", out)
117+
self.assertIn(b"cmd", out)
118+
self.assertIn(b"Arguments:", out)
119+
120+
@support.cpython_only
121+
@support.force_not_colorized
122+
def test_help_not_colorized(self):
123+
rc, out, err = assert_python_ok("--help")
124+
self.assertNotIn(b"\x1b[", out)
125+
103126
def test_optimize(self):
104127
self.verify_valid_flag('-O')
105128
self.verify_valid_flag('-OO')
@@ -1063,6 +1086,7 @@ def test_argv0_normalization(self):
10631086
self.assertEqual(proc.stdout.strip(), b'0')
10641087

10651088
@support.cpython_only
1089+
@support.force_not_colorized
10661090
def test_parsing_error(self):
10671091
args = [sys.executable, '-I', '--unknown-option']
10681092
proc = subprocess.run(args,
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
The interpreter help (such as ``python --help``) is now in color. Patch by
2+
Hugo van Kemenade.

0 commit comments

Comments
 (0)