Skip to content

Commit 8aa9d40

Browse files
authored
gh-90300: split --help output into separate options (#30331)
Make --help output shorter and add new help options. --help-env, --help-xoptions and --help-all command-line options are added to complement --help.
1 parent 132e563 commit 8aa9d40

File tree

6 files changed

+210
-80
lines changed

6 files changed

+210
-80
lines changed

Doc/using/cmdline.rst

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,15 +183,37 @@ automatically enabled, if available on your platform (see
183183
Automatic enabling of tab-completion and history editing.
184184

185185

186+
.. _using-on-generic-options:
187+
186188
Generic options
187189
~~~~~~~~~~~~~~~
188190

189191
.. cmdoption:: -?
190192
-h
191193
--help
192194

193-
Print a short description of all command line options.
195+
Print a short description of all command line options and corresponding
196+
environment variables and exit.
197+
198+
.. cmdoption:: --help-env
199+
200+
Print a short description of Python-specific environment variables
201+
and exit.
202+
203+
.. versionadded:: 3.11
204+
205+
.. cmdoption:: --help-xoptions
194206

207+
Print a description of implementation-specific :option:`-X` options
208+
and exit.
209+
210+
.. versionadded:: 3.11
211+
212+
.. cmdoption:: --help-all
213+
214+
Print complete usage information and exit.
215+
216+
.. versionadded:: 3.11
195217

196218
.. cmdoption:: -V
197219
--version
@@ -212,6 +234,7 @@ Generic options
212234
.. versionadded:: 3.6
213235
The ``-VV`` option.
214236

237+
215238
.. _using-on-misc-options:
216239

217240
Miscellaneous options
@@ -460,6 +483,7 @@ Miscellaneous options
460483
See :ref:`warning-filter` and :ref:`describing-warning-filters` for more
461484
details.
462485

486+
463487
.. cmdoption:: -x
464488

465489
Skip the first line of the source, allowing use of non-Unix forms of
@@ -553,6 +577,7 @@ Miscellaneous options
553577
The ``-X frozen_modules`` option.
554578

555579

580+
556581
Options you shouldn't use
557582
~~~~~~~~~~~~~~~~~~~~~~~~~
558583

Lib/test/test_cmd_line.py

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,32 +25,55 @@ def _kill_python_and_exit_code(p):
2525
returncode = p.wait()
2626
return data, returncode
2727

28+
2829
class CmdLineTest(unittest.TestCase):
2930
def test_directories(self):
3031
assert_python_failure('.')
3132
assert_python_failure('< .')
3233

3334
def verify_valid_flag(self, cmd_line):
34-
rc, out, err = assert_python_ok(*cmd_line)
35+
rc, out, err = assert_python_ok(cmd_line)
3536
self.assertTrue(out == b'' or out.endswith(b'\n'))
3637
self.assertNotIn(b'Traceback', out)
3738
self.assertNotIn(b'Traceback', err)
39+
return out
3840

39-
def test_optimize(self):
40-
self.verify_valid_flag('-O')
41-
self.verify_valid_flag('-OO')
41+
def test_help(self):
42+
self.verify_valid_flag('-h')
43+
self.verify_valid_flag('-?')
44+
out = self.verify_valid_flag('--help')
45+
lines = out.splitlines()
46+
self.assertIn(b'usage', lines[0])
47+
self.assertNotIn(b'PYTHONHOME', out)
48+
self.assertNotIn(b'-X dev', out)
49+
self.assertLess(len(lines), 50)
4250

43-
def test_site_flag(self):
44-
self.verify_valid_flag('-S')
51+
def test_help_env(self):
52+
out = self.verify_valid_flag('--help-env')
53+
self.assertIn(b'PYTHONHOME', out)
54+
55+
def test_help_xoptions(self):
56+
out = self.verify_valid_flag('--help-xoptions')
57+
self.assertIn(b'-X dev', out)
4558

46-
def test_usage(self):
47-
rc, out, err = assert_python_ok('-h')
59+
def test_help_all(self):
60+
out = self.verify_valid_flag('--help-all')
4861
lines = out.splitlines()
4962
self.assertIn(b'usage', lines[0])
63+
self.assertIn(b'PYTHONHOME', out)
64+
self.assertIn(b'-X dev', out)
65+
5066
# The first line contains the program name,
5167
# but the rest should be ASCII-only
5268
b''.join(lines[1:]).decode('ascii')
5369

70+
def test_optimize(self):
71+
self.verify_valid_flag('-O')
72+
self.verify_valid_flag('-OO')
73+
74+
def test_site_flag(self):
75+
self.verify_valid_flag('-S')
76+
5477
def test_version(self):
5578
version = ('Python %d.%d' % sys.version_info[:2]).encode("ascii")
5679
for switch in '-V', '--version', '-VV':
@@ -90,7 +113,7 @@ def get_xoptions(*args):
90113
def test_unknown_xoptions(self):
91114
rc, out, err = assert_python_failure('-X', 'blech')
92115
self.assertIn(b'Unknown value for option -X', err)
93-
msg = b'Fatal Python error: Unknown value for option -X'
116+
msg = b'Fatal Python error: Unknown value for option -X (see --help-xoptions)'
94117
self.assertEqual(err.splitlines().count(msg), 1)
95118
self.assertEqual(b'', out)
96119

@@ -134,7 +157,6 @@ def test_xoption_frozen_modules(self):
134157
}
135158
for raw, expected in tests:
136159
cmd = ['-X', f'frozen_modules{raw}',
137-
#'-c', 'import os; print(os.__spec__.loader.__name__, end="")']
138160
'-c', 'import os; print(os.__spec__.loader, end="")']
139161
with self.subTest(raw):
140162
res = assert_python_ok(*cmd)
@@ -167,7 +189,6 @@ def test_relativedir_bug46421(self):
167189
# Test `python -m unittest` with a relative directory beginning with ./
168190
# Note: We have to switch to the project's top module's directory, as per
169191
# the python unittest wiki. We will switch back when we are done.
170-
defaultwd = os.getcwd()
171192
projectlibpath = os.path.dirname(__file__).removesuffix("test")
172193
with os_helper.change_cwd(projectlibpath):
173194
# Testing with and without ./
@@ -247,7 +268,6 @@ def test_invalid_utf8_arg(self):
247268
#
248269
# Test with default config, in the C locale, in the Python UTF-8 Mode.
249270
code = 'import sys, os; s=os.fsencode(sys.argv[1]); print(ascii(s))'
250-
base_cmd = [sys.executable, '-c', code]
251271

252272
def run_default(arg):
253273
cmd = [sys.executable, '-c', code, arg]
@@ -892,6 +912,7 @@ def test_sys_flags_not_set(self):
892912
PYTHONSAFEPATH="1",
893913
)
894914

915+
895916
class SyntaxErrorTests(unittest.TestCase):
896917
def check_string(self, code):
897918
proc = subprocess.run([sys.executable, "-"], input=code,
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Make ``--help`` output shorter by moving some info to the new
2+
``--help-env`` and ``--help-xoptions`` command-line options.
3+
Also add ``--help-all`` option to print complete usage.

Misc/python.man

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,19 @@ python \- an interpreted, interactive, object-oriented programming language
8484
|
8585
.I never
8686
]
87+
.br
88+
[
89+
.B \--help
90+
]
91+
[
92+
.B \--help-env
93+
]
94+
[
95+
.B \--help-xoptions
96+
]
97+
[
98+
.B \--help-all
99+
]
87100
.br
88101
[
89102
.B \-c
@@ -149,6 +162,16 @@ the behavior of the interpreter.
149162
.B \-h ", " \-? ", "\-\-help
150163
Prints the usage for the interpreter executable and exits.
151164
.TP
165+
.B "\-\-help\-env"
166+
Prints help about Python-specific environment variables and exits.
167+
.TP
168+
.B "\-\-help\-xoptions"
169+
Prints help about implementation-specific \fB\-X\fP options and exits.
170+
.TP
171+
.TP
172+
.B "\-\-help\-all"
173+
Prints complete usage information and exits.
174+
.TP
152175
.B \-i
153176
When a script is passed as first argument or the \fB\-c\fP option is
154177
used, enter interactive mode after executing the script or the
@@ -287,7 +310,7 @@ a regular expression on the warning message.
287310

288311
.TP
289312
.BI "\-X " option
290-
Set implementation specific option. The following options are available:
313+
Set implementation-specific option. The following options are available:
291314

292315
-X faulthandler: enable faulthandler
293316

@@ -332,7 +355,7 @@ Set implementation specific option. The following options are available:
332355
files are desired as well as suppressing the extra visual location indicators
333356
when the interpreter displays tracebacks.
334357

335-
-X frozen_modules=[on|off]: whether or not frozen modules should be used
358+
-X frozen_modules=[on|off]: whether or not frozen modules should be used.
336359
The default is "on" (or "off" if you are running a local build).
337360

338361
.TP

Python/getopt.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,12 @@ static const wchar_t *opt_ptr = L"";
4444
#define SHORT_OPTS L"bBc:dEhiIJm:OPqRsStuvVW:xX:?"
4545

4646
static const _PyOS_LongOption longopts[] = {
47+
/* name, has_arg, val (used in switch in initconfig.c) */
4748
{L"check-hash-based-pycs", 1, 0},
48-
{NULL, 0, 0},
49+
{L"help-all", 0, 1},
50+
{L"help-env", 0, 2},
51+
{L"help-xoptions", 0, 3},
52+
{NULL, 0, -1}, /* sentinel */
4953
};
5054

5155

0 commit comments

Comments
 (0)