Skip to content

Commit

Permalink
pythongh-91818: Use default program name in the CLI of many modules
Browse files Browse the repository at this point in the history
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 ...").
  • Loading branch information
serhiy-storchaka committed Oct 1, 2024
1 parent 04bfea2 commit 97400c1
Show file tree
Hide file tree
Showing 10 changed files with 14 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Lib/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -1743,7 +1743,7 @@ def unparse(ast_obj):
def main():
import argparse

parser = argparse.ArgumentParser(prog='python -m ast')
parser = argparse.ArgumentParser()
parser.add_argument('infile', nargs='?', default='-',
help='the file to parse; defaults to stdin')
parser.add_argument('-m', '--mode', default='exec',
Expand Down
2 changes: 1 addition & 1 deletion Lib/ensurepip/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ def _uninstall_helper(*, verbosity=0):

def _main(argv=None):
import argparse
parser = argparse.ArgumentParser(prog="python -m ensurepip")
parser = argparse.ArgumentParser()
parser.add_argument(
"--version",
action="version",
Expand Down
2 changes: 1 addition & 1 deletion Lib/ensurepip/_uninstall.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@


def _main(argv=None):
parser = argparse.ArgumentParser(prog="python -m ensurepip._uninstall")
parser = argparse.ArgumentParser()
parser.add_argument(
"--version",
action="version",
Expand Down
3 changes: 1 addition & 2 deletions Lib/json/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@


def main():
prog = 'python -m json'
description = ('A simple command line interface for json module '
'to validate and pretty-print JSON objects.')
parser = argparse.ArgumentParser(prog=prog, description=description)
parser = argparse.ArgumentParser(description=description)
parser.add_argument('infile', nargs='?',
help='a JSON file to be validated or pretty-printed',
default='-')
Expand Down
3 changes: 1 addition & 2 deletions Lib/pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -2423,8 +2423,7 @@ def help():
def main():
import argparse

parser = argparse.ArgumentParser(prog="pdb",
usage="%(prog)s [-h] [-c command] (-m module | pyfile) [args ...]",
parser = argparse.ArgumentParser(usage="%(prog)s [-h] [-c command] (-m module | pyfile) [args ...]",
description=_usage,
formatter_class=argparse.RawDescriptionHelpFormatter,
allow_abbrev=False)
Expand Down
1 change: 0 additions & 1 deletion Lib/sqlite3/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ def runsource(self, source, filename="<input>", symbol="single"):
def main(*args):
parser = ArgumentParser(
description="Python sqlite3 CLI",
prog="python -m sqlite3",
)
parser.add_argument(
"filename", type=str, default=":memory:", nargs="?",
Expand Down
4 changes: 3 additions & 1 deletion Lib/test/test_sqlite3/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ def expect_failure(self, *args):

def test_cli_help(self):
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):
out = self.expect_success("-v")
Expand Down
2 changes: 1 addition & 1 deletion Lib/tokenize.py
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ def error(message, filename=None, location=None):
sys.exit(1)

# Parse the arguments and options
parser = argparse.ArgumentParser(prog='python -m tokenize')
parser = argparse.ArgumentParser()
parser.add_argument(dest='filename', nargs='?',
metavar='filename.py',
help='the file to tokenize; defaults to stdin')
Expand Down
3 changes: 1 addition & 2 deletions Lib/venv/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -572,8 +572,7 @@ def create(env_dir, system_site_packages=False, clear=False,
def main(args=None):
import argparse

parser = argparse.ArgumentParser(prog=__name__,
description='Creates virtual Python '
parser = argparse.ArgumentParser(description='Creates virtual Python '
'environments in one or '
'more target '
'directories.',
Expand Down
Original file line number Diff line number Diff line change
@@ -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.

0 comments on commit 97400c1

Please sign in to comment.