Skip to content

Commit 73ccfa2

Browse files
gaogaotiantianAA-Turnervstinner
authored
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>
1 parent 3e8fcb7 commit 73ccfa2

File tree

2 files changed

+27
-18
lines changed

2 files changed

+27
-18
lines changed

Lib/pdb.py

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2081,8 +2081,6 @@ def help():
20812081
pydoc.pager(__doc__)
20822082

20832083
_usage = """\
2084-
usage: pdb.py [-c command] ... [-m module | pyfile] [arg] ...
2085-
20862084
Debug the Python program given by pyfile. Alternatively,
20872085
an executable module or package to debug can be specified using
20882086
the -m switch.
@@ -2097,34 +2095,44 @@ def help():
20972095

20982096

20992097
def main():
2100-
import getopt
2101-
2102-
opts, args = getopt.getopt(sys.argv[1:], 'mhc:', ['help', 'command='])
2103-
2104-
if not args:
2105-
print(_usage)
2098+
import argparse
2099+
2100+
parser = argparse.ArgumentParser(prog="pdb",
2101+
description=_usage,
2102+
formatter_class=argparse.RawDescriptionHelpFormatter,
2103+
allow_abbrev=False)
2104+
2105+
parser.add_argument('-c', '--command', action='append', default=[], metavar='command')
2106+
group = parser.add_mutually_exclusive_group(required=True)
2107+
group.add_argument('-m', metavar='module')
2108+
group.add_argument('pyfile', nargs='?')
2109+
parser.add_argument('args', nargs="*")
2110+
2111+
if len(sys.argv) == 1:
2112+
# If no arguments were given (python -m pdb), print the whole help message.
2113+
# Without this check, argparse would only complain about missing required arguments.
2114+
parser.print_help()
21062115
sys.exit(2)
21072116

2108-
if any(opt in ['-h', '--help'] for opt, optarg in opts):
2109-
print(_usage)
2110-
sys.exit()
2111-
2112-
commands = [optarg for opt, optarg in opts if opt in ['-c', '--command']]
2117+
opts = parser.parse_args()
21132118

2114-
module_indicated = any(opt in ['-m'] for opt, optarg in opts)
2115-
cls = _ModuleTarget if module_indicated else _ScriptTarget
2116-
target = cls(args[0])
2119+
if opts.m:
2120+
file = opts.m
2121+
target = _ModuleTarget(file)
2122+
else:
2123+
file = opts.pyfile
2124+
target = _ScriptTarget(file)
21172125

21182126
target.check()
21192127

2120-
sys.argv[:] = args # Hide "pdb.py" and pdb options from argument list
2128+
sys.argv[:] = [file] + opts.args # Hide "pdb.py" and pdb options from argument list
21212129

21222130
# Note on saving/restoring sys.argv: it's a good idea when sys.argv was
21232131
# modified by the script being debugged. It's a bad idea when it was
21242132
# changed by the user from the command line. There is a "restart" command
21252133
# which allows explicit specification of command line arguments.
21262134
pdb = Pdb()
2127-
pdb.rcLines.extend(commands)
2135+
pdb.rcLines.extend(opts.command)
21282136
while True:
21292137
try:
21302138
pdb._run(target)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:mod:`pdb`: Replace :mod:`getopt` with :mod:`argparse` for parsing command line arguments.

0 commit comments

Comments
 (0)