summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTian Gao <gaogaotiantian@hotmail.com>2023-09-22 16:55:48 (GMT)
committerGitHub <noreply@github.com>2023-09-22 16:55:48 (GMT)
commit73ccfa28c5e6ff68de15fdbb1321d4773a688e61 (patch)
treeb1cc8dfc638cd22a77fa440d09619b8c2d8a07d9
parent3e8fcb7df74248530c4280915c77e69811f69c3f (diff)
downloadcpython-73ccfa28c5e6ff68de15fdbb1321d4773a688e61.zip
cpython-73ccfa28c5e6ff68de15fdbb1321d4773a688e61.tar.gz
cpython-73ccfa28c5e6ff68de15fdbb1321d4773a688e61.tar.bz2
gh-109164: Replace `getopt` with `argparse` in pdb (#109165)
Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com> Co-authored-by: Victor Stinner <vstinner@python.org>
-rwxr-xr-xLib/pdb.py44
-rw-r--r--Misc/NEWS.d/next/Library/2023-09-08-22-26-26.gh-issue-109164.-9BFWR.rst1
2 files changed, 27 insertions, 18 deletions
diff --git a/Lib/pdb.py b/Lib/pdb.py
index a391bc1..fd62d24 100755
--- a/Lib/pdb.py
+++ b/Lib/pdb.py
@@ -2081,8 +2081,6 @@ def help():
pydoc.pager(__doc__)
_usage = """\
-usage: pdb.py [-c command] ... [-m module | pyfile] [arg] ...
-
Debug the Python program given by pyfile. Alternatively,
an executable module or package to debug can be specified using
the -m switch.
@@ -2097,34 +2095,44 @@ To let the script run up to a given line X in the debugged file, use
def main():
- import getopt
-
- opts, args = getopt.getopt(sys.argv[1:], 'mhc:', ['help', 'command='])
-
- if not args:
- print(_usage)
+ import argparse
+
+ parser = argparse.ArgumentParser(prog="pdb",
+ description=_usage,
+ formatter_class=argparse.RawDescriptionHelpFormatter,
+ allow_abbrev=False)
+
+ parser.add_argument('-c', '--command', action='append', default=[], metavar='command')
+ group = parser.add_mutually_exclusive_group(required=True)
+ group.add_argument('-m', metavar='module')
+ group.add_argument('pyfile', nargs='?')
+ parser.add_argument('args', nargs="*")
+
+ if len(sys.argv) == 1:
+ # If no arguments were given (python -m pdb), print the whole help message.
+ # Without this check, argparse would only complain about missing required arguments.
+ parser.print_help()
sys.exit(2)
- if any(opt in ['-h', '--help'] for opt, optarg in opts):
- print(_usage)
- sys.exit()
-
- commands = [optarg for opt, optarg in opts if opt in ['-c', '--command']]
+ opts = parser.parse_args()
- module_indicated = any(opt in ['-m'] for opt, optarg in opts)
- cls = _ModuleTarget if module_indicated else _ScriptTarget
- target = cls(args[0])
+ if opts.m:
+ file = opts.m
+ target = _ModuleTarget(file)
+ else:
+ file = opts.pyfile
+ target = _ScriptTarget(file)
target.check()
- sys.argv[:] = args # Hide "pdb.py" and pdb options from argument list
+ sys.argv[:] = [file] + opts.args # Hide "pdb.py" and pdb options from argument list
# Note on saving/restoring sys.argv: it's a good idea when sys.argv was
# modified by the script being debugged. It's a bad idea when it was
# changed by the user from the command line. There is a "restart" command
# which allows explicit specification of command line arguments.
pdb = Pdb()
- pdb.rcLines.extend(commands)
+ pdb.rcLines.extend(opts.command)
while True:
try:
pdb._run(target)
diff --git a/Misc/NEWS.d/next/Library/2023-09-08-22-26-26.gh-issue-109164.-9BFWR.rst b/Misc/NEWS.d/next/Library/2023-09-08-22-26-26.gh-issue-109164.-9BFWR.rst
new file mode 100644
index 0000000..b439c14
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2023-09-08-22-26-26.gh-issue-109164.-9BFWR.rst
@@ -0,0 +1 @@
+:mod:`pdb`: Replace :mod:`getopt` with :mod:`argparse` for parsing command line arguments.