mirror of
https://github.com/python/cpython.git
synced 2025-11-11 14:44:57 +00:00
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>
This commit is contained in:
parent
3e8fcb7df7
commit
73ccfa28c5
2 changed files with 25 additions and 16 deletions
40
Lib/pdb.py
40
Lib/pdb.py
|
|
@ -2081,8 +2081,6 @@ def help():
|
||||||
pydoc.pager(__doc__)
|
pydoc.pager(__doc__)
|
||||||
|
|
||||||
_usage = """\
|
_usage = """\
|
||||||
usage: pdb.py [-c command] ... [-m module | pyfile] [arg] ...
|
|
||||||
|
|
||||||
Debug the Python program given by pyfile. Alternatively,
|
Debug the Python program given by pyfile. Alternatively,
|
||||||
an executable module or package to debug can be specified using
|
an executable module or package to debug can be specified using
|
||||||
the -m switch.
|
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():
|
def main():
|
||||||
import getopt
|
import argparse
|
||||||
|
|
||||||
opts, args = getopt.getopt(sys.argv[1:], 'mhc:', ['help', 'command='])
|
parser = argparse.ArgumentParser(prog="pdb",
|
||||||
|
description=_usage,
|
||||||
|
formatter_class=argparse.RawDescriptionHelpFormatter,
|
||||||
|
allow_abbrev=False)
|
||||||
|
|
||||||
if not args:
|
parser.add_argument('-c', '--command', action='append', default=[], metavar='command')
|
||||||
print(_usage)
|
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)
|
sys.exit(2)
|
||||||
|
|
||||||
if any(opt in ['-h', '--help'] for opt, optarg in opts):
|
opts = parser.parse_args()
|
||||||
print(_usage)
|
|
||||||
sys.exit()
|
|
||||||
|
|
||||||
commands = [optarg for opt, optarg in opts if opt in ['-c', '--command']]
|
if opts.m:
|
||||||
|
file = opts.m
|
||||||
module_indicated = any(opt in ['-m'] for opt, optarg in opts)
|
target = _ModuleTarget(file)
|
||||||
cls = _ModuleTarget if module_indicated else _ScriptTarget
|
else:
|
||||||
target = cls(args[0])
|
file = opts.pyfile
|
||||||
|
target = _ScriptTarget(file)
|
||||||
|
|
||||||
target.check()
|
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
|
# 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
|
# 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
|
# changed by the user from the command line. There is a "restart" command
|
||||||
# which allows explicit specification of command line arguments.
|
# which allows explicit specification of command line arguments.
|
||||||
pdb = Pdb()
|
pdb = Pdb()
|
||||||
pdb.rcLines.extend(commands)
|
pdb.rcLines.extend(opts.command)
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
pdb._run(target)
|
pdb._run(target)
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
:mod:`pdb`: Replace :mod:`getopt` with :mod:`argparse` for parsing command line arguments.
|
||||||
Loading…
Add table
Add a link
Reference in a new issue