10000 gh-113548: Allow CLI arguments to `pdb -m` (#113557) · python/cpython@b3e8c78 · GitHub
[go: up one dir, main page]

Skip to content

Commit b3e8c78

Browse files
gh-113548: Allow CLI arguments to pdb -m (#113557)
1 parent 48c0b05 commit b3e8c78

File tree

3 files changed

+25
-9
lines changed

3 files changed

+25
-9
lines changed

Lib/pdb.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2250,15 +2250,19 @@ def main():
22502250
import argparse
22512251

22522252
parser = argparse.ArgumentParser(prog="pdb",
2253+
usage="%(prog)s [-h] [-c command] (-m module | pyfile) [args ...]",
22532254
description=_usage,
22542255
formatter_class=argparse.RawDescriptionHelpFormatter,
22552256
allow_abbrev=False)
22562257

2257-
parser.add_argument('-c', '--command', action='append', default=[], metavar='command')
2258-
group = parser.add_mutually_exclusive_group(required=True)
2259-
group.add_argument('-m', metavar='module')
2260-
group.add_argument('pyfile', nargs='?')
2261-
parser.add_argument('args', nargs="*")
2258+
# We need to maunally get the script from args, because the first positional
2259+
# arguments could be either the script we need to debug, or the argument
2260+
# to the -m module
2261+
parser.add_argument('-c', '--command', action='append', default=[], metavar='command', dest='commands',
2262+
help='pdb commands to execute as if given in a .pdbrc file')
2263+
parser.add_argument('-m', metavar='module', dest='module')
2264+
parser.add_argument('args', nargs='*',
2265+
help="when -m is not specified, the first arg is the script to debug")
22622266

22632267
if len(sys.argv) == 1:
22642268
# If no arguments were given (python -m pdb), print the whole help message.
@@ -2268,11 +2272,13 @@ def main():
22682272

22692273
opts = parse 10000 r.parse_args()
22702274

2271-
if opts.m:
2272-
file = opts.m
2275+
if opts.module:
2276+
file = opts.module
22732277
target = _ModuleTarget(file)
22742278
else:
2275-
file = opts.pyfile
2279+
if not opts.args:
2280+
parser.error("no module or script to run")
2281+
file = opts.args.pop(0)
22762282
target = _ScriptTarget(file)
22772283

22782284
target.check()
@@ -2284,7 +2290,7 @@ def main():
22842290
# changed by the user from the command line. There is a "restart" command
22852291
# which allows explicit specification of command line arguments.
22862292
pdb = Pdb()
2287-
pdb.rcLines.extend(opts.command)
2293+
pdb.rcLines.extend(opts.commands)
22882294
while True:
22892295
try:
22902296
pdb._run(target)

Lib/test/test_pdb.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3057,6 +3057,15 @@ def test_module_is_run_as_main(self):
30573057
stdout, stderr = self.run_pdb_module(script, commands)
30583058
self.assertTrue(any("SUCCESS" in l for l in stdout.splitlines()), stdout)
30593059

3060+
def test_run_module_with_args(self):
3061+
commands = """
3062+
continue
3063+
"""
3064+
self._run_pdb(["calendar", "-m"], commands, expected_returncode=2)
3065+
3066+
stdout, _ = self._run_pdb(["-m", "calendar", "1"], commands)
3067+
self.assertIn("December", stdout)
3068+
30603069
def test_breakpoint(self):
30613070
script = """
30623071
if __name__ == '__main__':
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:mod:`pdb` now allows CLI arguments to ``pdb -m``.

0 commit comments

Comments
 (0)
0