gh-91818: Use default program name in the CLI of many modules (GH-124867)

As argparse now detects by default when the code was run as a module.

This leads to using the actual executable name instead of simply "python"
to display in the usage message ("usage: python -m ...").
This commit is contained in:
Serhiy Storchaka 2024-10-10 00:20:53 +03:00 committed by GitHub
parent cbfd392479
commit 7d2c39752f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 14 additions and 12 deletions

View File

@ -1743,7 +1743,7 @@ def unparse(ast_obj):
def main(): def main():
import argparse import argparse
parser = argparse.ArgumentParser(prog='python -m ast') parser = argparse.ArgumentParser()
parser.add_argument('infile', nargs='?', default='-', parser.add_argument('infile', nargs='?', default='-',
help='the file to parse; defaults to stdin') help='the file to parse; defaults to stdin')
parser.add_argument('-m', '--mode', default='exec', parser.add_argument('-m', '--mode', default='exec',

View File

@ -205,7 +205,7 @@ def _uninstall_helper(*, verbosity=0):
def _main(argv=None): def _main(argv=None):
import argparse import argparse
parser = argparse.ArgumentParser(prog="python -m ensurepip") parser = argparse.ArgumentParser()
parser.add_argument( parser.add_argument(
"--version", "--version",
action="version", action="version",

View File

@ -6,7 +6,7 @@ import sys
def _main(argv=None): def _main(argv=None):
parser = argparse.ArgumentParser(prog="python -m ensurepip._uninstall") parser = argparse.ArgumentParser()
parser.add_argument( parser.add_argument(
"--version", "--version",
action="version", action="version",

View File

@ -9,10 +9,9 @@ import sys
def main(): def main():
prog = 'python -m json'
description = ('A simple command line interface for json module ' description = ('A simple command line interface for json module '
'to validate and pretty-print JSON objects.') 'to validate and pretty-print JSON objects.')
parser = argparse.ArgumentParser(prog=prog, description=description) parser = argparse.ArgumentParser(description=description)
parser.add_argument('infile', nargs='?', parser.add_argument('infile', nargs='?',
help='a JSON file to be validated or pretty-printed', help='a JSON file to be validated or pretty-printed',
default='-') default='-')

View File

@ -2423,8 +2423,7 @@ To let the script run up to a given line X in the debugged file, use
def main(): def main():
import argparse import argparse
parser = argparse.ArgumentParser(prog="pdb", parser = argparse.ArgumentParser(usage="%(prog)s [-h] [-c command] (-m module | pyfile) [args ...]",
usage="%(prog)s [-h] [-c command] (-m module | pyfile) [args ...]",
description=_usage, description=_usage,
formatter_class=argparse.RawDescriptionHelpFormatter, formatter_class=argparse.RawDescriptionHelpFormatter,
allow_abbrev=False) allow_abbrev=False)

View File

@ -65,7 +65,6 @@ class SqliteInteractiveConsole(InteractiveConsole):
def main(*args): def main(*args):
parser = ArgumentParser( parser = ArgumentParser(
description="Python sqlite3 CLI", description="Python sqlite3 CLI",
prog="python -m sqlite3",
) )
parser.add_argument( parser.add_argument(
"filename", type=str, default=":memory:", nargs="?", "filename", type=str, default=":memory:", nargs="?",

View File

@ -34,7 +34,9 @@ class CommandLineInterface(unittest.TestCase):
def test_cli_help(self): def test_cli_help(self):
out = self.expect_success("-h") out = self.expect_success("-h")
self.assertIn("usage: python -m sqlite3", out) self.assertIn("usage: ", out)
self.assertIn(" [-h] [-v] [filename] [sql]", out)
self.assertIn("Python sqlite3 CLI", out)
def test_cli_version(self): def test_cli_version(self):
out = self.expect_success("-v") out = self.expect_success("-v")

View File

@ -510,7 +510,7 @@ def main():
sys.exit(1) sys.exit(1)
# Parse the arguments and options # Parse the arguments and options
parser = argparse.ArgumentParser(prog='python -m tokenize') parser = argparse.ArgumentParser()
parser.add_argument(dest='filename', nargs='?', parser.add_argument(dest='filename', nargs='?',
metavar='filename.py', metavar='filename.py',
help='the file to tokenize; defaults to stdin') help='the file to tokenize; defaults to stdin')

View File

@ -575,8 +575,7 @@ def create(env_dir, system_site_packages=False, clear=False,
def main(args=None): def main(args=None):
import argparse import argparse
parser = argparse.ArgumentParser(prog=__name__, parser = argparse.ArgumentParser(description='Creates virtual Python '
description='Creates virtual Python '
'environments in one or ' 'environments in one or '
'more target ' 'more target '
'directories.', 'directories.',

View File

@ -0,0 +1,4 @@
The CLI of many modules (:mod:`ast`, :mod:`ensurepip`, :mod:`json`,
:mod:`pdb`, :mod:`sqlite3`, :mod:`tokenize`, :mod:`venv`) now uses the
actual executable name instead of simply "python" to display in the usage
message.