Skip to content

Commit fc29a53

Browse files
committed
Add colour to Python help
1 parent ad7d361 commit fc29a53

2 files changed

Lines changed: 277 additions & 112 deletions

File tree

Lib/test/test_cmd_line.py

Lines changed: 28 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,29 @@ 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+
# "usage:" heading in bold blue
114+
self.assertIn(b'\x1b[1;34musage:\x1b[0m', out)
115+
# Short option in bold green
116+
self.assertIn(b'\x1b[1;32m-h\x1b[0m', out)
117+
# Long option in bold cyan
118+
self.assertIn(b'\x1b[1;36m--help-all\x1b[0m', out)
119+
# Label in bold yellow
120+
self.assertIn(b'\x1b[1;33mcmd\x1b[0m', out)
121+
# Heading in bold blue
122+
self.assertIn(b'\x1b[1;34mArguments:\x1b[0m', out)
123+
124+
@support.cpython_only
125+
@support.force_not_colorized
126+
def test_help_not_colorized(self):
127+
rc, out, err = assert_python_ok("--help")
128+
self.assertNotIn(b'\x1b[', out)
129+
103130
def test_optimize(self):
104131
self.verify_valid_flag('-O')
105132
self.verify_valid_flag('-OO')
@@ -1063,6 +1090,7 @@ def test_argv0_normalization(self):
10631090
self.assertEqual(proc.stdout.strip(), b'0')
10641091

10651092
@support.cpython_only
1093+
@support.force_not_colorized
10661094
def test_parsing_error(self):
10671095
args = [sys.executable, '-I', '--unknown-option']
10681096
proc = subprocess.run(args,

0 commit comments

Comments
 (0)