@@ -2081,8 +2081,6 @@ def help():
2081
2081
pydoc .pager (__doc__ )
2082
2082
2083
2083
_usage = """\
2084
- usage: pdb.py [-c command] ... [-m module | pyfile] [arg] ...
2085
-
2086
2084
Debug the Python program given by pyfile. Alternatively,
2087
2085
an executable module or package to debug can be specified using
2088
2086
the -m switch.
@@ -2097,34 +2095,44 @@ def help():
2097
2095
2098
2096
2099
2097
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 ()
2106
2115
sys .exit (2 )
2107
2116
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 ()
2113
2118
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 )
2117
2125
2118
2126
target .check ()
2119
2127
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
2121
2129
2122
2130
# Note on saving/restoring sys.argv: it's a good idea when sys.argv was
2123
2131
# modified by the script being debugged. It's a bad idea when it was
2124
2132
# changed by the user from the command line. There is a "restart" command
2125
2133
# which allows explicit specification of command line arguments.
2126
2134
pdb = Pdb ()
2127
- pdb .rcLines .extend (commands )
2135
+ pdb .rcLines .extend (opts . command )
2128
2136
while True :
2129
2137
try :
2130
2138
pdb ._run (target )
0 commit comments